Invalidate cached results of "StaticallyCacheFor"

Dan
Dan
Vote:
 

Just say I have follwoing to fetch data

var result = client.Search()
    .StaticallyCacheFor(TimeSpan.FromMinutes(10))
    .GetResult();

After 6 minutes (when I do a change in BlogPost model), how can I invalidate this statically cached data and get data with cahnges

#131950
Aug 10, 2015 17:17
Vote:
 

If you do want to invalidate search results cache as soon as blog is updated, you can use cache dependency. Let's say you store some value in cache using key dependencyKey = "BlogUpdatedKey" every time when blog is updated.

Create CacheDependency for that key:

var cacheDependency = new CacheDependency(null, new[] {dependencyKey});


And use this dependency when you cache search results:

var searchRequest = client.Search<BlogPost>()
    .StaticallyCacheFor(TimeSpan.FromMinutes(10), cacheDependency)
    .GetResult();


In this case search results cache is invalidated after 10 minutes or when you update/remove another cached value using dependencyKey "BlogUpdatedKey".

#131960
Aug 10, 2015 18:58
Dan
Vote:
 

Thanks Dmytro for your reply.

So, what you saying is, every time when I create a new instance of cache dependency object like follows (using same dependencyKey)

var cacheDependency = new CacheDependency(null, new[] {dependencyKey});

and use that in the "StaticallyCacheFor" will invalidate cache (on the other hand, using the same instance will not invalidate the cache) ?

I'm bit new to Find service and Episerver cache and would like to clarify this bit more.

#131961
Edited, Aug 10, 2015 20:19
Vote:
 

No, cached search results are invalidated when dependencyKey value is invalidated.

When you cache your BlogPost search results with that cacheDependency, you define that these search results should be automatically removed from the cache after 10 minutes or when dependencyKey and corresponding value is expired, removed or inserted in the cache.

var cacheDependency = new CacheDependency(null, new[] {dependencyKey});

var searchRequest = client.Search<BlogPost>()
    .StaticallyCacheFor(TimeSpan.FromMinutes(10), cacheDependency)
    .GetResult();

Each of the following code lines invalidates cached search results:

HttpRuntime.Cache.Remove(dependentKey); // dependentKey value is removed from the cache

HttpRuntime.Cache.Insert(dependentKey, newValue); // dependentKey is replaced with new value


It's not so much Find-specific, this is basic ASP.NET caching functionality.

#131978
Edited, Aug 11, 2015 14:36
Dan
Vote:
 

Thanks Dmytro for your explanation. Yes, I had some reading about ASP.NET caching and system around it and got to know this is much related to ASP.NET caching

So, I did following

var dependencyKey = "BlogUpdatedKey";

CacheManager.Insert(dependencyKey, Guid.NewGuid()); //This is to avoid null value cache key is returning HasChanged="true" in a CacheDependency
var cacheDependency = new CacheDependency(null, new[] {dependencyKey});

Now, cacheDependency.HasChanged is "false". So, when "BlogPost" model changed, I do following 

CacheManager.Insert(dependencyKey, Guid.NewGuid());

Then, cacheDependency.HasChanged will turn in to "true" and it is last for ever in that state. But I want it to be turn it to "false" after "StaticallyCacheFor" query has executed. So do I need to re instantiate the cacheDependency object using following code again ?

cacheDependency = new CacheDependency(null, new[] {dependencyKey});
#131980
Edited, Aug 11, 2015 15:49
Dan
Vote:
 

Any thoughts regarding my approach ?

#174762
Feb 03, 2017 12:38
* 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.