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, Ed, biapar, Willr, Ingo, swaiba

Do DataObjectSets get cached?

Go to End

14 Posts   3100 Views


Forum Moderator, 1391 Posts

23 February 2010 at 10:32pm

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;


Community Member, 244 Posts

24 February 2010 at 12:18am

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


Community Member, 253 Posts

25 February 2010 at 10:13am

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.


Community Member, 244 Posts

25 February 2010 at 10:40am

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.


Community Member, 89 Posts

25 February 2010 at 10:47am

Edited: 25/02/2010 10:48am

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?



Community Member, 244 Posts

25 February 2010 at 11:04am

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.

Go to Top