Instance of Cache throughout the application

Mar 14, 2016 at 12:55 PM
Hey guys, I am upgrading from old EFCachingProvider to EF6Cache. Could anyone please suggest a way to accomplish following with the new EFCache:

EFCachingProviderConfiguration.DefaultCache.InvalidateSets(...)

How do I get an instance of Cache at a later time in EF6 version? Can I create a new instance of InMemoryCache and use something it like obj.InvalidateSets(<setNames>)? OR do I need to exclusively maintain their instance throught the request lifetime?

Note that I have multiple ICache implementations which are pluggable based on Web.config settings.

Cheers!
Coordinator
Mar 15, 2016 at 10:43 PM
You create your cache when to register it. If you want to invalidate the cache outside of EF pipeline you need to hold on to this instance and call the .InvalidateSets() method on this instance. If you create a new instance of the cache it will be different from what EF is using and therefore any calls you make on the newly created instance will have no effect on what EF is using. Note that the actual cache implementations must implement the ICache interface which exposes the .InvalidateSets() method so, as soon as the method is implemented correctly you don't need to know what kind of implementation you are using.

Thanks,
Pawel
Marked as answer by SiteAdmin on 3/15/2016 at 9:28 PM
Mar 16, 2016 at 5:27 AM
moozzyk wrote:
You create your cache when to register it. If you want to invalidate the cache outside of EF pipeline you need to hold on to this instance and call the .InvalidateSets() method on this instance. If you create a new instance of the cache it will be different from what EF is using and therefore any calls you make on the newly created instance will have no effect on what EF is using. Note that the actual cache implementations must implement the ICache interface which exposes the .InvalidateSets() method so, as soon as the method is implemented correctly you don't need to know what kind of implementation you are using.

Thanks,
Pawel
Appreciate your response Pawel.

My cache classes are implementing ICache. Would you recommend having a singleton or static object to maintain instance of ICache in that case?

Thanks
Coordinator
Mar 18, 2016 at 12:52 AM
I don't know the architecture of your application so it is hard to answer. I personally would avoid a static object or a singleton if there are other options but it might be fine if other options increase complexity etc.

Thanks,
Pawel