Asp.net caching limitation?

Possible duplicate:
Maximum ASP.NET Cache Size

I cache quite a lot of data using asp.net caching (overlay code):

HttpContext.Current.Cache.Insert(GlobalVars.Current.applicationID + "_" + cacheName, itemToCache, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(240)); 

However, I think that the cache on the server is full and it needs to retrieve data from the database again. Is there a limit on the amount of data that can be cached on the server or any IIS settings that can be configured?

+6
source share
3 answers

There is a way to update the limit, but I would highly recommend using a different caching system (more on this below).

.NET cache

To learn more about the limitation of .NET caching, read this excellent answer from the Microsoft.NET Team Member .

If you want to see the current limits of .NET Cache, you can try:

 var r = new Dictionary<string, string>(); using (var pc = new PerformanceCounter("ASP.NET Applications", "Cache % Machine Memory Limit Used", true)) { pc.InstanceName = "__Total__"; r.Add("Total_MachineMemoryUsed", String.Concat(pc.NextValue().ToString("N1"), "%")); } using (var pc = new PerformanceCounter("ASP.NET Applications", "Cache % Process Memory Limit Used", true)) { pc.InstanceName = "__Total__"; r.Add("Total_ProcessMemoryUsed", String.Concat(pc.NextValue().ToString("N1"), "%")); } using (var pc = new PerformanceCounter("ASP.NET Applications", "Cache API Entries", true)) { pc.InstanceName = "__Total__"; r.Add("Total_Entries", pc.NextValue().ToString("N0")); } using (var pc = new PerformanceCounter("ASP.NET Applications", "Cache API Misses", true)) { pc.InstanceName = "__Total__"; r.Add("Total_Misses", pc.NextValue().ToString("N0")); } using (var pc = new PerformanceCounter("ASP.NET Applications", "Cache API Hit Ratio", true)) { pc.InstanceName = "__Total__"; r.Add("Total_HitRatio", String.Concat(pc.NextValue().ToString("N1"), "%")); } using (var pc = new PerformanceCounter("ASP.NET Applications", "Cache API Trims", true)) { pc.InstanceName = "__Total__"; r.Add("Total_Trims", pc.NextValue().ToString()); } 

Memcached

I am currently using Memcached , and if you host your site somewhere, you can use a paid service, for example:

Or, if you use your own server, you can download Couchbase Community Edition and host your own.

Here you will find more questions about using MemCache, for example:

Make room for any cache system

To use a different caching system without changing the code, you can use to create an interface, for example

 public interface ICacheService { T Get<T>(string cacheID, Func<T> getItemCallback) where T : class; void Clear(); } 

then you are using .NET Cache, your implementation will be something like

 public class InMemoryCache : ICacheService { private int minutes = 15; public T Get<T>(string cacheID, Func<T> getItemCallback) where T : class { T item = HttpRuntime.Cache.Get(cacheID) as T; if (item == null) { item = getItemCallback(); HttpRuntime.Cache.Insert( cacheID, item, null, DateTime.Now.AddMinutes(minutes), System.Web.Caching.Cache.NoSlidingExpiration); } return item; } public void Clear() { IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator(); while (enumerator.MoveNext()) HttpRuntime.Cache.Remove(enumerator.Key.ToString()); } } 

and you will use it like:

 string cacheId = string.Concat("myinfo-", customer_id); MyInfo model = cacheProvider.Get<MyInfo>(cacheId, () => { MyInfo info = db.GetMyStuff(customer_id); return info; }); 

if you are using Memcached, all you have to do is create a new class that implements ICacheService and select the class you want using IoC or a direct call like:

 private ICacheService cacheProvider; protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (cacheProvider == null) cacheProvider = new InMemoryCache(); base.Initialize(requestContext); } 
+11
source

The cache uses memory allocation for the workflow. By default, a workflow can get 60 percent of the machine memory to do its job.

According to the link, this can be changed to allow more machine memory to be used by the workflow by editing the machine.config file. Presumably you have a built-in cache that already refreshes when it detects that the data is out of date, so this should allow you to put more objects in the cache.

+2
source

When inserting an item into the cache, add the CacheItemRemovedCallback method.

In the callback log, the reason the item was deleted. So you see that it is a memory pressure or something else.

 public static void OnRemove(string key, object cacheItem, System.Web.Caching.CacheItemRemovedReason reason) { AppendLog("The cached value with key '" + key + "' was removed from the cache. Reason: " + reason.ToString()); } 

http://msdn.microsoft.com/en-us/library/aa478965.aspx

+2
source

All Articles