Developer forum

Forum » Development » Set SlidingExpiration time in MemoryCache

Set SlidingExpiration time in MemoryCache

Anders Ebdrup
Reply

Dear DynamicWeb,

 

How can I define the SlidingExpiration time, when I implement MemoryCache in a custom class that implements this interface ICacheStorage? For now it seems to be hardcoded to 10 minutes.

 

Best regards, Anders


Replies

 
Theodor Perrier
Reply

Hey Anders.

From the source code, it seems that the MemoryCache class doesn't implement a way to override the setting which is hardcoded in the class CacheItemPolicy static prop DefaultStoragePolicy.

I hope this clearifies your options :)

Best regards, Theodor

 
Anders Ebdrup
Reply

Hi Theodor,

 

Thanks for the reply, that was the same conclusion from my side, but hopefully I am missing something obvious about how to implement a difference policy.

 

Best regards, Anders

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

You can override default expiration - for all memory caches.

CacheItemPolicy.DefaultStoragePolicy.SlidingExpiration = TimeSpan.FromMinutes(30);

(please don't)

We can add an option to overrule the default of specific instances to using e.g. SetExpiration().

But please do not set it to something crazy - TimeSpan.FromMinutes(300000)

It will happen and server will cry and die.... :-)

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

You can override default expiration - for all memory caches.

CacheItemPolicy.DefaultStoragePolicy.SlidingExpiration = TimeSpan.FromMinutes(30);

(please don't)

We can add an option to overrule the default of specific instances to using e.g. SetExpiration().

But please do not set it to something crazy - TimeSpan.FromMinutes(300000)

It will happen and server will cry and die.... :-)

 
Anders Ebdrup
Reply

Dear Nicolai,

 

Yes, thank you, that will be very nice to be able to override the Expiration or maybe even better; to override the policy itself?

 

Best regards, Anders

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

I have added a suggestion to add two new methods. MemoryCache.SetExpiration and MemoryCache.SetCustomCacheItemPolicy

https://doc.dynamicweb.dev/documentation/fundamentals/dw10release/releasenotes/workiteminfo.html?workitemid=26659

This has not been discussed with architect team yet, so consider it a proposal for now.

Votes for this answer: 1
 
Anders Ebdrup
Reply

Very nice, Nicolai! Thank you so much! :-)

 

You must be logged in to post in the forum