Custom cache dependencies for parent page

Ji Pattison-Smith asked on May 10, 2019 12:04

I've got a situation where the data entered in the form tab of a particular page type is used to populate a web part on all of its child pages. The child pages have cache keys like "currentdocument|sitename|/parent-page/child-page" with dependencies of "node|sitename|/parent-page/child-page" and "nodes|sitename|childpagetype|all". What would be the standard approach for clearing the cache for the child documents when the parent document is changed?

Note that the child pages may be several levels deep, and could be one of 2 different page types, and a single page template. There are other child pages which are of a different type & template that don't need their cache clearing.

The only way I've been able to get this working is to use global events - so on document update, if the page type is the parent page type, get all of its descendants which fit the criteria and touch all of those cache keys one by one.

What would be really good is if I could say that pages of a the child types have a new cache dependency on their nearest ancestor of the parent page type - but I don't think you can add these dynamically, it looks like they're all fixed.

Recent Answers


David te Kloese answered on May 10, 2019 12:41

Cant you do it the other way around? Not sure how your logic is set up, but add a cache dependency on the child page that get's touched automatically when the parent is updated?

0 votesVote for this answer Mark as a Correct answer

Ji Pattison-Smith answered on May 10, 2019 12:49

David - yes that is what I'm wanting to do, apologies for the confusing explanation! But my question is, how do I add cache dependencies to pages/page types?

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on May 10, 2019 12:57

Depending on what elements you want to clear you can set it on the page, Web part or even in custom code.

docs.kentico.com/.../configuring-caching/setting-cache-dependencies

there is a section giving solutions for

  • Cached data in custom code
  • Full output cache of pages (add the Output cache dependencies web part onto the page)
  • Partial output cache of web parts or controls (Partial cache dependencies property)
  • Content cache of web parts or controls (Cache dependencies property)

Think you're looking for option 2 or 3.

EG: on a Web Part you could set:

Image Text

0 votesVote for this answer Mark as a Correct answer

Ji Pattison-Smith answered on May 10, 2019 13:14

Thanks David. I have already looked through the documentation and tried this partial cache dependency approach, but unfortunately the only thing that works is clearing the "currentdocument|sitename|/parent-page/child-page" cache key (or one of its dependencies). Adding a partial cache dependency to the web part doesn't seem to add a dependency to the document's cache key, and therefore touching the dependency doesn't clear the cache for the document.

I am confused why the web part gets cached along with the document if it has partial cache dependencies set up - I expected that somehow Kentico would know that this particular part of the output shouldn't get cached?

I did also try the output cache dependencies web part on the child page template but this had the same effect (i.e. it didn't clear the cache).

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on May 10, 2019 14:01

Do you also have "full page output caching" enabled?

You can disable it for a particular page (breaking inheritance) on the general properties.

Image Text

0 votesVote for this answer Mark as a Correct answer

Ji Pattison-Smith answered on May 10, 2019 15:13

I've just checked and output cache is switched off. However, I think this is the content cache rather than the output cache that is causing the problem - but I don't want to switch that off globally of course.

0 votesVote for this answer Mark as a Correct answer

Ji Pattison-Smith answered on May 10, 2019 16:47

OK so I've found where the issue is coming from. The web part code is using DocumentContext.CurrentDocument, which uses the cached version not only of the current document but also of any documents you access through that property - for example, Parent or DocumentsOnPath. Since I was using this method to get the data from the parent page, it was using the cached version. Of course, I could use a different method of getting the page (i.e. use Parent/DocumentsOnPath to get the ID of the parent and then do TreeProvider.SelectSingleNode) but that's a query I don't need to do every time. This would still be solved if I could add a custom content cache dependency to a document, but it looks like I'll have to stick with the global events method for now.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.