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.

General Questions

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

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

Do DataObjectSets get cached?


14 Posts   2136 Views


23 February 2010 at 10:32pm Forum Moderator, 1091 Posts

Not so clear after all :-(
If I understand this correctly from Ingo's comments, calls within template controls are cached in the template layer, so <% if RelatedPages %><% control RelatedPages %>... is OK.

With a bit of luck, resultsets are cached in MySQL, depending on hosting... But to up performance within the model/controller we should provide our own caching? So to access a resultset from throughout code, we should do something like this:

protected cachedRelatedPages = null;

function getRelatedPages() {

if (empty ($this->cachedRelatedPages)) {
$this->cachedRelatedPages = $this->RelatedPages();
return $this->cachedRelatedPages;


24 February 2010 at 12:18am Community Member, 244 Posts

mateuzs was quite right after all, my mistake for not checking the code more closely. Sorry!


25 February 2010 at 10:13am Community Member, 243 Posts

Well, I just got off the phone to the SilverStripe guys and was told that DataObjects get cached as do relationships. So the database isn't repeatedly hit for the same data over and over if you do <% if Children %> and <% control Children %>.

If you do ?showqueries in the URL of a page, you will see that there are not identical queries one after the other where you do this in your template either.

Hope that helps.


25 February 2010 at 10:40am Community Member, 244 Posts

Actually the Children() method isn't a standard relationship - it's a custom method in the Hierarchy extension which has its own custom caching code.


25 February 2010 at 10:47am (Last edited: 25 February 2010 10:48am), Community Member, 89 Posts

So if I understand correctly there is a cache in the templating engine which grabs everything.

This leaves us with:
- many_manys and has_manys are cached in templates, but not in the php code
- has_ones are cached both in templates and code (by the virtue of having a dedicated cache)
- Children are also cached in both (dedicated cache too)

Is that about right?



25 February 2010 at 11:04am Community Member, 244 Posts

Actually I'm pretty sure that template variables are only cached under certain conditions - one is that an <% if %> statement will cache the variable, but a plain $Foo will not.