Caching in C#/.Net

I wanted to ask you what is the best approach to implement a cache in C#? Is there a possibility by using given .NET classes or something like that? Perhaps something like a dictionary that will remove some entries, if it gets too large, but where whose entries won't be removed by the garbage collector?

Answers


If you're using ASP.NET, you could use the Cache class (System.Web.Caching).

Here is a good helper class: c-cache-helper-class

If you mean caching in a windows form app, it depends on what you're trying to do, and where you're trying to cache the data.

We've implemented a cache behind a Webservice for certain methods (using the System.Web.Caching object.).

However, you might also want to look at the Caching Application Block. (See here) that is part of the Enterprise Library for .NET Framework 2.0.


If you are using .NET 4 or superior, you can use MemoryCache class.


MemoryCache in the framework is a good place to start, but you might also like to consider the open source library LazyCache because it has a simpler API than memory cache and has built in locking as well as some other developer friendly features. It is also available on nuget.

To give you an example:

// Create our cache service using the defaults (Dependency injection ready).
// Uses MemoryCache.Default as default so cache is shared between instances
IAppCache cache = new CachingService();

// Declare (but don't execute) a func/delegate whose result we want to cache
Func<ComplexObjects> complexObjectFactory = () => methodThatTakesTimeOrResources();

// Get our ComplexObjects from the cache, or build them in the factory func 
// and cache the results for next time under the given key
ComplexObject cachedResults = cache.GetOrAdd("uniqueKey", complexObjectFactory);

I recently wrote this article about getting started with caching in dot net that you may find useful.

(Disclaimer: I am the author of LazyCache)


You can use the ObjectCache.

See http://msdn.microsoft.com/en-us/library/system.runtime.caching.objectcache.aspx


The cache classes supplied with .NET are handy, but have a major problem - they can not store much data (tens of millions+) of objects for a long time without killing your GC. They work great if you cache a few thousand objects, but the moment you move into millions and keep them around until they propagate into GEN2 - the GC pauses would eventually start to be noticeable when you system comes to low memory threshold and GC needs to sweep all gens.

The practicality is this - if you need to store a few hundred thousand instances - use MS cache. Does not matter if your objects are 2-field or 25 field - its about the number of references.

On the other hand there are cases when large RAMs, which are common these days, need to be utilized, i.e. 64 GB. For that we have created a 100% managed memory manager and cache that sits on top of it.

Our solution can easily store 300,000,000 object in-memory in-process without taxing GC at all - this is because we store data in large (250 mb) byte[] segments.

Here is the code: NFX Pile (Apache 2.0)

And video: NFX Pile Cache - Youtube


For Local Stores

Your question needs more clarification. C# is a language not a framework. You have to specify which framework you want to implement the caching. If we consider that you want to implement it in ASP.NET it is still depends completely on what you want from Cache. You can decide between in-process cache (which will keep the data inside the heap of your application) and out-of-process cache (in this case you can store the data in other memory than the heap like Amazon Elastic cache server). And there is also another decision to make which is between client caching or serve side caching. Usually in solution you have to develop different solution for caching different data. Because base on four factors (accessibility, persistency, size, cost) you have to make decision which solution you need.


If you are looking to Cache something in ASP.Net then I would look at the Cache class. For example

Hashtable menuTable = new Hashtable(); 
menuTable.add("Home","default.aspx"); 
Cache["menu"] = menuTable; 

Then to retrieve it again

Hashtable menuTable = (Hashtable)Cache["menu"];

You could use a Hashtable

it has very fast lookups, no key collisions and your data will not garbage collected


Need Your Help

Using bitwise operators for Booleans in C++

c++ boolean bitwise-operators

Is there any reason not to use the bitwise operators &amp;, |, and ^ for "bool" values in C++?

Cannot provide arguments when creating an instance of generic type

c# json generics encapsulation

I have an object that I want to have read only after it is created... namely because the properties in the constructor must be used in GetHashCode, and therefore can't change once created.