Question

I wish to have a concurrent dictionary whereby the items expire if they haven't been used for a certain period of time.

Now this seems like a fairly simple class to create but I have done some searching and come across MemoryCache. It seems like exactly what I want (see CacheItemPolicy.SlidingExpiration). I am wondering if that does what I want - or if there are any blaring disadvantages for my use case?

This is a client-side WPF application.

Was it helpful?

Solution

MemoryCache is indeed thread safe, which is suitable for your needs, but one thing to consider is that if you intend to create multiple instances of it, it might not be the best choice, as MSDN mentions:

MemoryCache is not a singleton, but you should create only a few or potentially only one MemoryCache instance and code that caches items should use those instances.

For more details, you can see my question here: Using multiple instances of MemoryCache

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top