Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Partial Caching in navigation, problem with $LinkingMode


Go to End


9 Posts   4060 Views

Avatar
WebSpilka

Community Member, 89 Posts

14 May 2011 at 1:21am

hi I have next code

<% cached 'navigation', Aggregate(Page).Max(LastEdited) %>
			<% control Menu(1) %>
				<td class="tabs"><a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML</a></td>
			<% end_control %>
<% end_cached %>

This code cashe my menu block for all pages
And if I go to "home" page. I get next html code
<a class="current" title="home" href="">Home</a>
<a class="link" title="contact-us" href="contact-us/">contact-us</a>

If I go to "contact-us/" page. I get next code
<a class="current" title="home" href="">Home</a>
<a class="link" title="contact-us" href="contact-us/">contact-us</a>

I always get home active. how to solve this problem?

Avatar
Willr

Forum Moderator, 5523 Posts

14 May 2011 at 1:18pm

For partial caching the caching is global and not specific to a user / session. In this case it's probably not even worth caching the navigation as the most expensive part of that operation will be determining the linking mode - which in turn needs to be uncached. You could write your own cache key which takes the url of the page + url of the user + last edited information to form a string but unless all your visitors are hitting one page you'll more likely have cache misses then hits.

Avatar
WebSpilka

Community Member, 89 Posts

16 May 2011 at 10:32pm

Edited: 16/05/2011 10:37pm

thank you
I solved the problem with $LinkingMode
But another problem appeared. How to reduce the number of database queries? Where are the 8 queries to the database if I cache the whole template?

In my Page.ss I add next

<% cached 'page', Aggregate(Page).Max(LastEdited), CacheSegment %><!DOCTYPE html>
<html lang="$ContentLocale">
<head>
...
...
</html><% end_cached %>

In mysite/code/Page.php I add a new method in the Controller:

protected function CacheSegment(){
return $_SERVER["REQUEST_URI"];
}

in config.php I have next code
Director::set_environment_type("test");

if(Director::isDev()){ SS_Cache::set_cache_lifetime('any', 3600, 100);
} else { SS_Cache::set_cache_lifetime('any', 3600, 100);
}

Object::add_extension('SiteTree', 'Translatable');
Translatable::set_default_locale('uk_UA');

//global $allowed_locales;
$allowed_locales = array(
'uk_UA',
'ru_RU',
'en_GB'
);
Translatable::set_allowed_locales($allowed_locales);

// enable nested URLs for this site (e.g. page/sub-page/)
SiteTree::enable_nested_urls();
SortableDataObject::add_sortable_classes(array('Foto','Tender','Book','BookNoImg','Slider'));

After these changes the number of queries in the database has decreased to 20 Although I thought that the whole page is cached.
I continued testing and adding in template files next code
SideBar.ss

<% cached 'SideBar', Aggregate(Page).Max(LastEdited), URLSegment %><div class="w220" id="LeftBox">
...
...
</div><% end_cached %>

TopMenu.ss
<% cached 'menu', Aggregate(Page).Max(LastEdited), URLSegment %><div id="TopMenu">
..
..
</div><% end_cached %>

and similarly in all the template files ...
but I will still have 8 database queries
188 Query	SET CHARACTER SET 'utf8'
		  188 Query	SET NAMES 'utf8'
		  188 Init DB	antisnid
		  188 Query	SET sql_mode = 'ANSI'
		  188 Query	SHOW TABLES
		  188 Query	SELECT "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."Locale", "SiteTree"."ParentID", "Page"."ShowSubMenu", "Page"."ShowParentPageLink", "Page"."IntroText", "Page"."HtmlVideoContent", "Page"."HtmlPidmenuLink", "Page"."PhotoID", "NewsPage"."Date", "NewsPage"."NotShowOnHomePage", "SubscriptionPage"."Fields", "SubscriptionPage"."Required", "SubscriptionPage"."CustomisedLables", "SubscriptionPage"."CustomisedErrors", "SubscriptionPage"."NewsletterTypes", "SubscriptionPage"."SubmissionButtonText", "SubscriptionPage"."SendNotification", "SubscriptionPage"."NotificationEmailSubject", "SubscriptionPage"."NotificationEmailFrom", CASE WHEN "SiteTree"."ClassName" IN ('SubscriptionPage') THEN "SubscriptionPage"."OnCompleteMessage" WHEN "SiteTree"."ClassName" IN ('UserDefinedForm', 'SubscribeForm') THEN "UserDefinedForm"."OnCompleteMessage" ELSE NULL END AS "OnCompleteMessage", "ErrorPage"."ErrorCode", "RedirectorPage"."RedirectionType", "RedirectorPage"."ExternalURL", "RedirectorPage"."LinkToID", "VirtualPage"."VersionID", "VirtualPage"."CopyContentFromID", "UserDefinedForm"."SubmitButtonText", "UserDefinedForm"."ShowClearButton", "UserDefinedForm"."DisableSaveSubmissions", "SubscribeForm"."Subscribe", "SubscribeForm"."AllNewsletters", "SubscribeForm"."Subject", "SubscribeForm"."NewletterListTitle", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree" LEFT JOIN "Page" ON "Page"."ID" = "SiteTree"."ID" LEFT JOIN "NewsPage" ON "NewsPage"."ID" = "SiteTree"."ID" LEFT JOIN "SubscriptionPage" ON "SubscriptionPage"."ID" = "SiteTree"."ID" LEFT JOIN "ErrorPage" ON "ErrorPage"."ID" = "SiteTree"."ID" LEFT JOIN "RedirectorPage" ON "RedirectorPage"."ID" = "SiteTree"."ID" LEFT JOIN "VirtualPage" ON "VirtualPage"."ID" = "SiteTree"."ID" LEFT JOIN "UserDefinedForm" ON "UserDefinedForm"."ID" = "SiteTree"."ID" LEFT JOIN "SubscribeForm" ON "SubscribeForm"."ID" = "SiteTree"."ID" WHERE ("HomepageForDomain" LIKE '%localhost%') AND ("SiteTree"."Locale" = 'uk_UA') ORDER BY "Sort"
		  188 Query	SELECT "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."Locale", "SiteTree"."ParentID", "Page"."ShowSubMenu", "Page"."ShowParentPageLink", "Page"."IntroText", "Page"."HtmlVideoContent", "Page"."HtmlPidmenuLink", "Page"."PhotoID", "NewsPage"."Date", "NewsPage"."NotShowOnHomePage", "SubscriptionPage"."Fields", "SubscriptionPage"."Required", "SubscriptionPage"."CustomisedLables", "SubscriptionPage"."CustomisedErrors", "SubscriptionPage"."NewsletterTypes", "SubscriptionPage"."SubmissionButtonText", "SubscriptionPage"."SendNotification", "SubscriptionPage"."NotificationEmailSubject", "SubscriptionPage"."NotificationEmailFrom", CASE WHEN "SiteTree"."ClassName" IN ('SubscriptionPage') THEN "SubscriptionPage"."OnCompleteMessage" WHEN "SiteTree"."ClassName" IN ('UserDefinedForm', 'SubscribeForm') THEN "UserDefinedForm"."OnCompleteMessage" ELSE NULL END AS "OnCompleteMessage", "ErrorPage"."ErrorCode", "RedirectorPage"."RedirectionType", "RedirectorPage"."ExternalURL", "RedirectorPage"."LinkToID", "VirtualPage"."VersionID", "VirtualPage"."CopyContentFromID", "UserDefinedForm"."SubmitButtonText", "UserDefinedForm"."ShowClearButton", "UserDefinedForm"."DisableSaveSubmissions", "SubscribeForm"."Subscribe", "SubscribeForm"."AllNewsletters", "SubscribeForm"."Subject", "SubscribeForm"."NewletterListTitle", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree" LEFT JOIN "Page" ON "Page"."ID" = "SiteTree"."ID" LEFT JOIN "NewsPage" ON "NewsPage"."ID" = "SiteTree"."ID" LEFT JOIN "SubscriptionPage" ON "SubscriptionPage"."ID" = "SiteTree"."ID" LEFT JOIN "ErrorPage" ON "ErrorPage"."ID" = "SiteTree"."ID" LEFT JOIN "RedirectorPage" ON "RedirectorPage"."ID" = "SiteTree"."ID" LEFT JOIN "VirtualPage" ON "VirtualPage"."ID" = "SiteTree"."ID" LEFT JOIN "UserDefinedForm" ON "UserDefinedForm"."ID" = "SiteTree"."ID" LEFT JOIN "SubscribeForm" ON "SubscribeForm"."ID" = "SiteTree"."ID" WHERE ("URLSegment" = 'home' AND "ParentID" = 0) AND ("SiteTree"."Locale" = 'uk_UA') ORDER BY "Sort" LIMIT 1
		  188 Query	SELECT "TranslationGroupID" FROM "SiteTree_translationgroups" WHERE "OriginalID" = 1
		  188 Query	SELECT "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."Locale", "SiteTree"."ParentID", "Page"."ShowSubMenu", "Page"."ShowParentPageLink", "Page"."IntroText", "Page"."HtmlVideoContent", "Page"."HtmlPidmenuLink", "Page"."PhotoID", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree" LEFT JOIN "Page" ON "Page"."ID" = "SiteTree"."ID" LEFT JOIN "SiteTree_translationgroups" ON "SiteTree_translationgroups"."OriginalID" = "SiteTree"."ID" WHERE ("SiteTree"."ClassName" IN ('HomePage')) AND ("SiteTree_translationgroups"."TranslationGroupID" = 1 AND "SiteTree"."Locale" = 'uk_UA') GROUP BY "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."Locale", "SiteTree"."ParentID", "Page"."ShowSubMenu", "Page"."ShowParentPageLink", "Page"."IntroText", "Page"."HtmlVideoContent", "Page"."HtmlPidmenuLink", "Page"."PhotoID", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END ORDER BY "Sort"
110516 13:28:17  188 Query	SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Priority", "SiteTree_Live"."Locale", "SiteTree_Live"."ParentID", "Page_Live"."ShowSubMenu", "Page_Live"."ShowParentPageLink", "Page_Live"."IntroText", "Page_Live"."HtmlVideoContent", "Page_Live"."HtmlPidmenuLink", "Page_Live"."PhotoID", "NewsPage_Live"."Date", "NewsPage_Live"."NotShowOnHomePage", "SubscriptionPage_Live"."Fields", "SubscriptionPage_Live"."Required", "SubscriptionPage_Live"."CustomisedLables", "SubscriptionPage_Live"."CustomisedErrors", "SubscriptionPage_Live"."NewsletterTypes", "SubscriptionPage_Live"."SubmissionButtonText", "SubscriptionPage_Live"."SendNotification", "SubscriptionPage_Live"."NotificationEmailSubject", "SubscriptionPage_Live"."NotificationEmailFrom", CASE WHEN "SiteTree_Live"."ClassName" IN ('SubscriptionPage') THEN "SubscriptionPage_Live"."OnCompleteMessage" WHEN "SiteTree_Live"."ClassName" IN ('UserDefinedForm', 'SubscribeForm') THEN "UserDefinedForm_Live"."OnCompleteMessage" ELSE NULL END AS "OnCompleteMessage", "ErrorPage_Live"."ErrorCode", "RedirectorPage_Live"."RedirectionType", "RedirectorPage_Live"."ExternalURL", "RedirectorPage_Live"."LinkToID", "VirtualPage_Live"."VersionID", "VirtualPage_Live"."CopyContentFromID", "UserDefinedForm_Live"."SubmitButtonText", "UserDefinedForm_Live"."ShowClearButton", "UserDefinedForm_Live"."DisableSaveSubmissions", "SubscribeForm_Live"."Subscribe", "SubscribeForm_Live"."AllNewsletters", "SubscribeForm_Live"."Subject", "SubscribeForm_Live"."NewletterListTitle", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "NewsPage_Live" ON "NewsPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "SubscriptionPage_Live" ON "SubscriptionPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "ErrorPage_Live" ON "ErrorPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "RedirectorPage_Live" ON "RedirectorPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "VirtualPage_Live" ON "VirtualPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "UserDefinedForm_Live" ON "UserDefinedForm_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "SubscribeForm_Live" ON "SubscribeForm_Live"."ID" = "SiteTree_Live"."ID" WHERE ("URLSegment" = 'home' AND "ParentID" = 0) ORDER BY "Sort" LIMIT 1
		  188 Query	SELECT "SiteConfig"."ClassName", "SiteConfig"."Created", "SiteConfig"."LastEdited", "SiteConfig"."Title", "SiteConfig"."Tagline", "SiteConfig"."Theme", "SiteConfig"."CanViewType", "SiteConfig"."CanEditType", "SiteConfig"."CanCreateTopLevelType", "SiteConfig"."ID", CASE WHEN "SiteConfig"."ClassName" IS NOT NULL THEN "SiteConfig"."ClassName" ELSE 'SiteConfig' END AS "RecordClassName" FROM "SiteConfig" LIMIT 1
		  188 Quit

where are these requests? for which they are formed, if I try to cache the entire page?

Avatar
Willr

Forum Moderator, 5523 Posts

17 May 2011 at 12:02am

Using partial caching doesn't avoid all database hits. Aggregate functions still do database lookups to ensure the pages / dataobjects haven't been updated but the benefit of this is it uses a very simple query and avoids expensive queries like joins, sorts and building large datasets. SilverStripe will still also have checks for permission.

Partial caching is designed to augment a fully working site and caching specific expensive operations, not replace all operations. By the sounds of it you would be better suited to static caching which will not require *any* database queries. The pages will be recached when you publish them and that's it. The rest of the time with static caching is pure html serving which is as fast as you'll get. When deciding which method to use (or you can use a combination of both) you've got to weigh up the pros and cons of each and decide on what you really need.

Avatar
WebSpilka

Community Member, 89 Posts

17 May 2011 at 2:29am

Edited: 17/05/2011 2:31am

Yes I try Static Publisher, but I have problem with it
If publish the page from the admin part, it works correctly (it create my page.html in cache folder)

But if I run http://localhost/antisnid/dev/buildcache first time show eror, that he needs to execute more than 30 seconds
after I edit php.ini and start /dev/buildcache it return blank page

what could be the problem?

Avatar
Willr

Forum Moderator, 5523 Posts

17 May 2011 at 2:41pm

But if I run http://localhost/antisnid/dev/buildcache first time show eror, that he needs to execute more than 30 seconds
after I edit php.ini and start /dev/buildcache it return blank page

what could be the problem?

Check your server apache error log. You may also want to run the buildcache via sake which will return errors.

Avatar
vwd

Community Member, 166 Posts

20 May 2012 at 12:14am

Edited: 20/05/2012 12:47am

Hi Fiord,

How did you solve the $LinkingMode issue - did you add URLSegment to the nav block cache key?

Thanks.
VWD.

Avatar
purplespider

Community Member, 89 Posts

29 July 2013 at 12:15am

Would also be interested to know the solution to the LinkingMode issue.

Go to Top