Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between MemoryCache vs ObjectCache in .net 4.0?

Tags:

c#

.net

.net-4.0

What is difference between .NET framework 4.0 MemoryCache vs ObjectCache? Where to use which object?

like image 612
Abdul Saboor Avatar asked Jan 09 '13 09:01

Abdul Saboor


People also ask

What is ObjectCache C#?

The ObjectCache type is the primary type for the in-memory object cache. To develop a custom cache implementation, you derive from the ObjectCache class. The ObjectCache class is new as of the . NET Framework 4.

What is ObjectCache?

Object caching is a process that stores database query results in order to quickly bring them back up next time they are needed. The cached object will be served promptly from the cache rather than sending multiple requests to a database. This is more efficient and reduces massive unnecessary loads on your server.

What is MemoryCache C#?

In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.

Is MemoryCache shared?

MemoryCache does not allow you to share memory between processes as the memory used to cache objects is bound to the application pool. That's the nature of any in-memory cache implementation you'll find. The only way to actually use a shared cache is to use a distributed cache.


2 Answers

ObjectCache is the abstract class that demonstrates how you should build a Cache that adheres to the rules the person who wrote ObjectCache wants you to obey. You cannot instantiate ObjectCache directly as it is abstract.

MemoryCache is an actual implementation of ObjectCache.

From the documentation:

ObjectCache

Represents an object cache and provides the base methods and properties for accessing the object cache.

MemoryCache

Represents the type that implements an in-memory cache.

Looking at the declaration for MemoryCache:

public class MemoryCache : ObjectCache,      IEnumerable, IDisposable 

We can see that MemoryCache inherits from ObjectCache - that is, it's an cache for objects that uses Memory as its storage - this is therefore an implementation of ObjectCache.

You could write your own; for example, DatabaseCache, which could also inherit from ObjectCache but instead it would use a database as the backing storage.

For everyday use, provided it met your needs, you would use and consume a MemoryCache. If you wanted to write your own, you could inherit from ObjectCache and implement the required methods and properties. However, in reality, there is probably little practical benefit to doing this as Microsoft already make several other caching solutions available, as do many other vendors.

like image 174
dash Avatar answered Sep 21 '22 13:09

dash


From MSDN;

The ObjectCache type is the primary type for the in-memory object cache. The built-in MemoryCache class derives from the ObjectCache class. The MemoryCache class is the only concrete object cache implementation in the .NET Framework 4 that derives from the ObjectCache class.

public class MemoryCache : ObjectCache,      IEnumerable, IDisposable 

MemoryCache inherits from ObjectCache.

You can get a reference to the default MemoryCache instance like this;

public static ObjectCache cache = MemoryCache.Default; 
like image 24
Soner Gönül Avatar answered Sep 25 '22 13:09

Soner Gönül