Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache with timeout using .Net 4 & Concurrent Collections

How would you implement a cache class that supports timeouts using the new Concurrent Collections of .Net 4?

The cache class will be typically holding hundreds of thousands of entries. A very large part of the cache may expire simultaneously.

Unlike a typical web cache, which may shrink due to memory pressure, this class should only automatically remove objects if they time out.

like image 681
Teleo Avatar asked Feb 27 '23 18:02

Teleo


1 Answers

Why do you need to implement a custom caching class? Isn't the default implementation enough? There's a whole new assembly dedicated to caching in .NET 4.0. Example of caching a value for 10 minutes:

var cache = MemoryCache.Default;
cache.Add("key", new object(), DateTime.Now.AddMinutes(10));
like image 164
Darin Dimitrov Avatar answered Mar 01 '23 12:03

Darin Dimitrov