asp.net缓存限制

本文关键字:缓存 net asp | 更新日期: 2023-09-27 17:49:40

可能重复:
ASP。NET缓存最大大小

我使用asp.net缓存(floowing代码(缓存了相当多的数据表:

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

然而,我认为服务器上的缓存已满,必须从数据库中重新获取数据表数据。可以缓存在服务器上的数据量或任何可以调整的IIS设置是否有任何限制?

asp.net缓存限制

有一种方法可以升级限制,但我强烈建议使用其他类型的缓存系统(更多信息请参阅下文(。

NET缓存

了解有关的更多信息。NET缓存限制,请阅读微软的这篇精彩回答。NET团队成员。

如果您想查看的当前限制。NET缓存,您可以尝试:

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,如果你在某个地方托管你的网站,你可以使用付费服务,比如:

  • http://www.memcachier.com/

或者,如果你使用自己的服务器,你可以下载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);
}

缓存使用工作进程的内存分配。默认情况下,工作进程可以获得60%的机器内存来完成其工作。

根据链接,可以通过编辑machine.config文件来更改这一点,以允许工作进程使用更多的机器内存。假设您已经构建了缓存,以便在检测到数据过期时进行更新,因此这应该允许您将更多对象放入缓存。

将项插入缓存时,添加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