To be succinct, what SIMPLE In-Memory Caches exist in the .Net ecosystem?
What I am looking for is:
- No-configuration (other than simple API calls). I do not want to mess with external configuration files as they just complicate deployment.
- Same-process (not some external process or server). Preferrably created as easily as
var myCache = new SimpleCache(1024 * 1024 * 100); // 100 MB
- Specified Memory limit
- Items cleared from cache based on least usage
- Time-based expiration (not required, but makes it potentially useful in other scenarios)
- Works with .Net 3.5
I have already reasearched these options:
- ASP.Net System.Web.Caching
- Its API does not support any sort of control over the size of the cache, nor priority based on usage. Consequently you are entirely at its mercy as to when it decides there is enough memory pressure to empty out the cache.
- System.Runtime.Caching
- Not an option as this is was added in .Net 4.0, and this post seems to indicate it has poor performance: Performance of system.runtime.caching
- Microsoft Enterprise Library - Caching Block
- Other than having a reputation of being heavyweight, I do not like its configuration using XML files or app.config. Furthermore, while it supports limiting the size of the cache based on NUMBER of objects stored, it does not have mechanisms for limiting the SIZE of those objects.
- NCache
- Probably overkill for the use case I want, but most importantly it is a paid product which is not something I want to do deal with (compared with me just writing one in a day or two). As usual, its express edition has usage limitations discouraging its use for any production purposes.
- MemCacheD
- Exact opposite of what I want (External distributed process)
I am working with Google Protocol Buffers (protobuf-net), so I DO have a relatively accurate estimation of the memory footprint of each item. I am caching data returned from database access, but I have no desire to use a formal ORM (I am actually using PetaPoco, but that is beside the point).
At this stage I am planning on implementing my own cache, using a double linked list and hash (dictionary) to provide for dropping items that are least recently used from the cache once the cache limit is reached. However, I wanted to check to see if anyone knew of any suitable options before I rolled my own.