Caching in C#/.Net

68,315

Solution 1

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.

Solution 2

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

Solution 3

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)

Solution 4

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

Solution 5

You can use the ObjectCache.

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

Share:
68,315
Sebastian Müller
Author by

Sebastian Müller

Financial Engineer, Software Engineer, Product Management, Mathematics, Finance, Insurance, IT Architect

Updated on August 20, 2020

Comments

  • Sebastian Müller
    Sebastian Müller almost 4 years

    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?