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.

Template Questions /

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

Where to put Partial Caching and how?


Go to End


4 Posts   1862 Views

Avatar
programmerswe

Community Member, 8 Posts

25 September 2015 at 8:57pm

Hi!
Im a bit lost in the world of Partial Caching. The thing is that I got a page that rearly contains anything. It just shows a few links that directs the user to the place they want to go. I got this kind of page for different systems kind of. For example, we got the countries Sweden, Finland, Russia. When i press Finland, it takes about 2 seconds of "wait" before it loads the page. I figured it have to be something in silverstripe that builds every time i go to this kind of page. Do you guys think that Partial Caching might remove some of that time, because it shouldn't take that long to load nothing.
I've checked the documentation for the Partial Caching but I still feel a bit lost (guess it's friday).
And if there is some other nice ways to speed up the page, please shear!

Avatar
Devlin

Community Member, 344 Posts

1 October 2015 at 12:10am

Partial Caching is mostly used in templates.

So if you've something like this:

<html>
<body>
<% loop $Menu(1) %>
$Title
<% end_loop %>
</body>
</html>

... $Menu(1) will always be executed on each request.

If you put a <% cached %> tag around this, $Menu(1) will be executed only once to build the cache.

<% cached 'Menu1' %>
<html>
<body>
<% loop $Menu(1) %>
$Title
<% end_loop %>
</body>
<% end_cached %>
</html>

But you want eventually to invalidate the cache in case SiteTree got somehow changed. You do this by adding parameters to the cache tag.

<% cached 'Menu1', $List('SiteTree').max('LastEdited'), $List('SiteTree').count() %>
<html>
<body>
<% loop $Menu(1) %>
$Title
<% end_loop %>
</body>
<% end_cached %>
</html>

"$List('SiteTree').max('LastEdited')" gets the latest 'LastEdited' date from the database while "$List('SiteTree').count()" gets the quantity of objects in the SiteTree.
So, if one of these parameters get changed, $Menu(1) will be executed once again to rebuild the cache.

You can do this with any other DataObject too.

class Page extends SiteTree() {
	public function getMyCustomList() {
		return ExtremelyLargeDataObject::get();
	}
}

<% cached 'Menu1', $List('ExtremelyLargeDataObject').max('LastEdited'), $List('ExtremelyLargeDataObject').count() %>
<% loop $MyCustomList %>
$Title
<% end_loop %>
<% end_cached %>

https://docs.silverstripe.org/en/3.1/developer_guides/performance/partial_caching/

Avatar
programmerswe

Community Member, 8 Posts

1 October 2015 at 8:19pm

Thanks mate!

Avatar
Bigfork

Community Member, 23 Posts

1 October 2015 at 11:01pm

The Cache Include module is also worth a look, I find it a bit more user friendly than the native partial caching.