asp.net的caching限制?

可能重复:
ASP.NETcaching的最大大小

我使用asp.netcaching(浮动代码)caching了很多数据表:

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

但是我认为服务器上的caching越来越大,不得不从数据库中重新获取数据表中的数据。 可以caching在服务器上的数据量或可以调整的任何IIS设置是否有限制?

有一种方法来升级限制,但我强烈建议您使用其他类型的缓存系统(更多关于这个下面)。

.NET缓存

要了解有关.NET缓存限制的更多信息,请阅读Microsoft .NET团队成员的 这个出色答案 。

如果你想看看.NET Cache的当前限制,你可以尝试:

 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的

我目前正在使用Memcached ,如果您在某个地方托管您的网站,则可以使用以下付费服务:

或者,如果您使用自己的服务器,则可以下载Couchbase社区版并托管我们自己的服务器。

您将在这里找到更多关于MemCache使用的问题,例如:

  • 您使用哪个.NET Memcached客户端,EnyimMemcached与BeITMemcached?
  • 如何开始与memcached

腾出任何缓存系统的空间

要使用其他缓存系统而不更改代码,可以采用创建一个接口

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

那么你是使用.NET缓存,你的实现将是类似的

 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()); } } 

你可以使用它:

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

如果您使用的是Memcached,那么您只需创建一个实现ICacheService的新类,并通过使用IoC或直接调用来选择所需的类:

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

将项目插入缓存时,添加一个CacheItemRemovedCallback方法。

在回调日志中删除项目的原因。 这样你就可以看到内存压力还是其他的东西。

 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

高速缓存使用工作进程的内存分配。 默认情况下,工作进程被允许获得60%的机器内存 ,以完成工作。

根据链接,可以通过编辑machine.config文件来更改此参数,以允许工作进程使用更多的机器内存。 据推测,当你检测到数据已经过时时,你已经建立了已经更新的缓存,所以这应该允许你把更多的对象放到缓存中。