I\'m doing a document viewer for some document format. To make it easier, let\'s say this is a PDF viewer, a Desktop application. One requirement for the s
There is an efficient, open sourced RAM virtualizer that uses MRU algorithm to keep freshest referenced objects in-memory and uses a fast, lightweight backing store (on Disk) for "paging".
Here is link in Code Project for a mini-article about it: http://www.codeproject.com/Tips/827339/Virtual-Cache
I hope you find it useful.
There's patterns & practices Enterprise Library (more specifically, Caching Application Block), but it IMO tends to be over-engineered and overly complex.
To addition to rsbarro's answer to use MemoryCache I recommend to use PostSharp AOP as described in
http://www.codeproject.com/Articles/493971/Leveraging-MemoryCache-and-AOP-for-expensive-calls
A classic trade-off situation. Keeping everything in memory will be fast at the cost of massively increased memory consumption, whilst retrieving from disc decreases memory consumption, but isn't as performant. However, you already know all this!
The built-in System.Web.Caching.Cache class is great, and I've used it to good effect many times myself in my ASP.NET applications (although mostly for database record caching), however, the drawback is that the cache will only run on one machine (typically a sole web server) and cannot be distributed across multiple machines.
If it's possible to "throw some hardware" at the problem, and it doesn't necessarily need to be expensive hardware, just boxes with plenty of memory, you could always go with a distributed caching solution. This will give you much more memory to play with whilst retaining (nearly) the same level of performance.
Some options for a distributed caching solution for .NET are:
Memcached.NET
indeXus.Net
or even Microsoft's own Velocity project.
How are you implementing your cache?
You can use the Cache class from System.Web.Caching, even in non-web applications, and it will purge items on an LRU basis if/when it needs the memory.
In a non-web application you'll need to use HttpRuntime.Cache to access the Cache
instance.
Note that the documentation states that the Cache
class isn't intended to be used outside of ASP.NET, although it's always worked for me. (I've never relied on it in any mission-critical app though.)
For .NET 4.0, you can also use the MemoryCache
from System.Runtime.Caching
.
http://msdn.microsoft.com/en-us/library/system.runtime.caching.aspx