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 issue with LastEdited


Go to End


5 Posts   881 Views

Avatar
BenWu

Community Member, 97 Posts

25 June 2013 at 1:37am

Edited: 25/06/2013 1:38am

Hello,

inside the SS template i got

<% cached Children.max(LastEdited) %> 
  <% loop Children %> 
   <h3>$Title</h3>
   $Thumbnail 
  <% end_loop %> 
<% end_cached %> 

Each sub page got a thumb-nail image attached. In CMS, if i change the thumb-nail image via the UploadField, click 'Save and Publish', the 'LastEdited' field doesn't change (probably because i did not change the content of it?) and thus the change is not reviewed on the front pages. Is this a SS bug that the field 'LastEdited' not changed even though i click on the 'Save and Publish" ?

How to get around this partial caching problem?

Avatar
kinglozzer

Community Member, 187 Posts

25 June 2013 at 3:50am

One solution is to subclass Image, then your page can have a relation to that class instead of a relation to image. So:

class MyImage extends Image {
}

class MyPage extends Page {
    public static $has_one = array(
         'Image'=>'MyImage'
    );
}

Then in your template, you can do <% cached Children.Max(LastEdited), List(MyImage).Max(LastEdited) %>

Hope this helps

Avatar
BenWu

Community Member, 97 Posts

25 June 2013 at 4:35am

I guess List(MyImage).Max(LastEdited) will force SS to search all 'MyImage' objects in database and get the lastest LastEdited field value as part of the cache key.

The problem is: MyPage and MyImage is a many-many relation. The LastEdited field will not work because the image itself is not edited. the image is linked to another page. we just built a new link.

I guess I have to write a function to find out all the related images of the sub page and work out a key, and pass that function to SS template

class MySubPage {
   static $many_many = array('Images' =>'MyImage')
}

}

Avatar
kinglozzer

Community Member, 187 Posts

25 June 2013 at 8:10pm

Yep, you can calculate the cache key in your controller: http://doc.silverstripe.org/framework/en/trunk/reference/partial-caching#cache-key-calculated-in-controller

I don't really know the best way to approach it, though. Perhaps you could fetch all records from the join table (e.g 'MyImage_MySubPage'), implode it to a string and md5 hash it?

Avatar
BenWu

Community Member, 97 Posts

25 June 2013 at 9:28pm

That's what i thought, but then how do you 'aggregate' all the md5 strings? using max,min,avg, or sum is not going to work. I think I just concatenate all the md5 string together to build a large key.