Try our conversational search powered by Generative AI!

Cache dependencies and content expiration

Vote:
 

I can use IContentCacheKeyCreator to create a cache key for some content, and then cache a item using ISynchronizedObjectInstanceCache. If I would change that content via the admin interface, the cached item would be invalidated. However, if the same content would expire (StopPublish is passed) - the cached item is not invalidated. Does anyone know how to handle this?

#209184
Nov 10, 2019 15:41
Vote:
 

What you can do is create an IInitializableModule that attaches to the contentEvent PublishedContent

You can then check the type of the content expirying and then clear your cache

public class ClearCacheEvents : IInitializableModule
{
        public void Initialize(InitializationEngine context)
        {
            var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            contentEvents.PublishedContent += ClearCache;
        }

        private void ClearCache(object sender, EPiServer.ContentEventArgs e)
        {
            if (e.Content is SomeContent)
            {
				//Logic to clear cache
			}
		}
}
#209193
Nov 11, 2019 10:38
Andreas J - Nov 11, 2019 10:42
I don't think the published content event will be triggered neither for expiration or "activation".
Stuart - Nov 11, 2019 10:49
For content that expires I think all you can do is to run a scheduled job that scans for expired content and removes it from the cache
Vote:
 

I don't think there is a nice solution for that. What "content" are you caching? Is it possible to check for expired content when you get that cache?

#209203
Nov 11, 2019 11:03
Andreas J - Nov 11, 2019 11:12
A combination of CMS content and commerce promotions, which would be costly to fetch everytime. And if I need to check if the content is expired - I think that kind of eliminates the nice benefits of the cache dependencies.
Vote:
 

You could check the StopPublish on the cached item. If the StopPublish date is changed, I believe your cache is invalidated.

#209207
Nov 11, 2019 11:30
Andreas J - Nov 11, 2019 12:34
I'm not sure I understand. If StopPublish is changed, I would also assume that the cache is invalidated. That's basically what I wrote to begin with. However, what happens when StopPublish is passed and that content expires?
Vote:
 

Hi Andreas

A simple fix could be to just set the expiration date and time of your cached object, to the known StopPublish property value (if it is not null).

As Tomas points out, if an editor changes the StopPublish date to something earlier it would be removed from cache anyway. Then next time someone requests it, it is cached again with the new StopPublish value.

This way you don't need to implement any custom content event handlers, at all.

#209208
Nov 11, 2019 12:01
Andreas J - Nov 11, 2019 12:39
So if I would have some dependencies, I would need to find out the effective stop publish date for every dependency and select the "soonest"? Sure, that would probably work but I don't really like that kind of micro management of the publish dates, which otherwise is handled by the framework.
Stefan Holm Olsen - Nov 11, 2019 12:49
Yes, that is the idea. If you have a list of IVersionable instances, you find the earliest StopPublish date and use that. If there is none such values in the list, you decide a default expiration.
You cannot count on Episerver to clear the content object from the cache on content expiration. Being published and being in cache are two different things. Sometimes we need to load the content, no matter if it is published or not. And then it would be great if it was not suddenly removed because a piece of content was scheduled for unpublish.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.