Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Invalidate epi find cache in StaticallyCacheFor (CMS 12)

Vote:
 

Hi all,

I am not able to find any deeper information about how to invalidate cached results of "StaticallyCacheFor" in CMS 12.

It seems that I can use instance of IChangeToken. So I expected that just set the property HasChanged will be enough, but it is not working. I am still getting the cached result.

This is the method I am trying to use:

public static ISearch<TResult> StaticallyCacheFor<TResult>(this ISearch<TResult> search, TimeSpan cacheDuration, IChangeToken token, string cacheDependencyKey = null)

Do you have any advice how to use the IChangeToken and invalidate the cache result?

#330566
Sep 26, 2024 14:24
Vote:
 

what you can do is to set the cacheDependencyKey and then remove that once you want to invalidate the cache

#330613
Sep 27, 2024 11:58
Vote:
 

Hi Quan

Thank you for your replay and time.

So the process should look something like this?

  1. add the key with some empty value? 
    • CacheManager.Insert("cacheDependencyKey", new object());
  2. call search with the key so the result is cached (and the change token is null then)
    • search.StaticallyCacheFor(TimeSpan.FromMinutes(10), null, "cacheDependencyKey")
  3. remove the key when something happaned (creating a page event...)
    • CacheManager.Remove("cacheDependencyKey");
  4. call search still with the key 
    • search.StaticallyCacheFor(TimeSpan.FromMinutes(10), null, "cacheDependencyKey")

and if I understand it correctly, the best time to add the key to cache would be just right after I get the search result, right?

Petr

#330723
Sep 30, 2024 7:02
Vote:
 

No you should add they cache dependency before you get the search result. So you got it almost right already, just think it should be like this on cache invalidation

    • CacheManager.Remove("cacheDependencyKey");
      CacheManager.Add("cacheDependencyKey", new object());

Also if you want to nuke all the Find search result cache, you can remove CacheUtils.MasterKey 

#330724
Sep 30, 2024 7:26
Vote:
 

oh, now I get it! (and test it)  This really invalidated the cache:

CacheManager.Remove("cacheDependencyKey");
CacheManager.Add("cacheDependencyKey", new object());

search.StaticallyCacheFor(TimeSpan.FromMinutes(10), null, "cacheDependencyKey")

 Another question (which I can probable test) is if I set an expiration of that cachekey to let's say 5 minutes

new CacheEvictionPolicy(new TimeSpan(0, 5, 0), CacheTimeoutType.Absolute);

but the StaticallyCacheFor(TimeSpan.FromMinutes(10) is 10 minutes cache. I guess the cache will expire after 5 minutes, correct. The lowest time will just expire the cache here.

Thank you Quan!

#330726
Sep 30, 2024 9:56
Vote:
 

IN theory, yes. the dependency key will expire after 5 minutes and the dependent caches will be removed too. My memory is a bit rusty but I think the general approach is to have the cache key object to live indefinitely (just like you add with just CacheManager.Add("cacheDependencyKey", new object()); ), so you only need to care about your StaticallyCacheFor time out. 

#330727
Sep 30, 2024 9:59
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.