Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to enforce a size limit of MemoryCache in System.Runtime.Caching?

Tags:

I'm using .net 4 Memory Cache. I would like to limit the size of the cache say to 10mb because I don't want my application to be abusing what goes in there.

I would also like to know how much memory my cache is at any given time. How can I tell at run time?

like image 312
Jonathon Kresner Avatar asked Apr 05 '11 03:04

Jonathon Kresner


People also ask

Is MemoryCache per user?

The ASP.NET Session object is per user key/value storage, whereas MemoryCache is an application level key/value storage (values are shared among all users).

Is MemoryCache a singleton?

Note that the MemoryCache is a singleton, but within the process. It is not (yet) a DistributedCache. Also note that Caching is Complex(tm) and that thousands of pages have been written about caching by smart people. This is a blog post as part of a series, so use your head and do your research.

Should I dispose MemoryCache?

MemoryCache implements IDisposable so you should call Dispose before replacing the old instance.

Is MemoryCache set thread safe?

MemoryCache is threadsafe. Multiple concurrent threads can read and write a MemoryCache instance. Internally thread-safety is automatically handled to ensure the cache is updated in a consistent manner.


2 Answers

You can specify the maximum amount of physical memory dedicated to the MemoryCache in the application config file using the namedCaches element, or by passing in the setting when you create your MemoryCache instance via the NameValueCollection passed into the constructor by putting an entry in the collection with a key of cacheMemoryLimitMegabytes and a value of 10.

Here is an example of the namedCaches configuration element:

<configuration>   <system.runtime.caching>     <memoryCache>       <namedCaches>         <add name="Default"            cacheMemoryLimitMegabytes="10"            physicalMemoryLimitPercentage="0"           pollingInterval="00:05:00" />       </namedCaches>     </memoryCache>   </system.runtime.caching> </configuration> 

And here is how you can configure the MemoryCache during creation:

//Create a name / value pair for properties var config = new NameValueCollection(); config.Add("pollingInterval", "00:05:00"); config.Add("physicalMemoryLimitPercentage", "0"); config.Add("cacheMemoryLimitMegabytes", "10");  //instantiate cache var cache = new MemoryCache("CustomCache", config); 

This blog post details just about all of the ways to configure the MemoryCache object, and some examples were adapted from this source.

like image 193
arcain Avatar answered Oct 03 '22 20:10

arcain


You can do this in configuration... for example...

<system.runtime.caching>    <memoryCache>       <namedCaches>          <add name="Default"               cacheMemoryLimitMegabytes="52"               physicalMemoryLimitPercentage="40"               pollingInterval="00:04:01" />       </namedCaches>    </memoryCache> </system.runtime.caching> 

To do this in code see... this msdn page

like image 42
John Sobolewski Avatar answered Oct 03 '22 22:10

John Sobolewski