Simon
Simon

Reputation: 8347

MemoryCache absoluteExpiration and memory limit

I am coding a MVC 5 internet application, and am using the MemoryCache object for caching objects. I see that using the MemoryCache.Set method, an absoluteExpiration can be specified.

If I use the following way to add and retrieve an object from the MemoryCache, what is the absoluteExpiration set to:

cache['cacheItem'] = testObject;

TestObject testObject = cache['cacheItem'] as TestObject;

Also, when using the MemoryCache in an MVC internet application, should I set the amount of memory that can be used for the MemoryCache, or is the default implementation safe enough for an Azure website?

Thanks in advance.

Upvotes: 1

Views: 4232

Answers (1)

Adrian Hofman
Adrian Hofman

Reputation: 1451

Your code is equivalent to calling Add, like below:

cache.Add("cacheItem", testObject, null);

The added entry would have the default expiration time, which is infinite (i.e., it doesn't expire). See the MSDN on CacheItemPolicy.AbsoluteExpiration for details.

To answer the question about memory usage: (from CacheMemoryLimitMegabytes Property):

The default is zero, which indicates that MemoryCache instances manage their own memory based on the amount of memory that is installed on the computer.

I would say that it's safe to let the MemoryCache defaults decide how much memory to use, unless you're doing something really fancy.

Upvotes: 4

Related Questions