【问题标题】:asp.net caching limit? [duplicate]asp.net 缓存限制? [复制]
【发布时间】:2012-12-12 10:14:25
【问题描述】:

可能重复:
ASP.NET cache maximum size

我正在使用 asp.net 缓存(流式代码)缓存大量数据表:

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

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

【问题讨论】:

  • 是的,我已经读过,我有 16GB RAM 和 4GB 正在使用中,所以不要认为这是原因,所以调查 IIS 路由...
  • 请根据您对相关问题的理解更新您的问题。 IE。您的问题未指定您的工作进程是使用 32 还是 64 版本...

标签: c# asp.net sql-server windows iis-7


【解决方案1】:

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

.NET 缓存

要了解有关 .NET 缓存限制的更多信息,请阅读Microsoft .NET Team member 中的this great answer

如果你想查看当前.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,如果您将网站托管在某个地方,您可以使用以下付费服务:

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

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

为任何缓存系统腾出空间

要在不改变代码的情况下使用其他缓存系统,你可以采用创建一个接口,比如

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

【讨论】:

    【解决方案2】:

    缓存使用工作进程的内存分配。 By default the worker process is allowed to get 60 percent of the machine memory 为了做好它的工作。

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

    【讨论】:

      【解决方案3】:

      向缓存中插入项目时,添加一个 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

      【讨论】:

        猜你喜欢
        • 2015-08-14
        • 2010-09-26
        • 2013-05-22
        • 2013-06-13
        • 1970-01-01
        • 1970-01-01
        • 2013-01-02
        • 1970-01-01
        • 2019-07-28
        相关资源
        最近更新 更多