asp.net缓存限制?

dha*_*rdy 7 c# windows sql-server asp.net iis-7

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

我使用asp.net缓存(浮动代码)缓存了很多数据表:

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

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

bal*_*dre 12

有一种方法可以升级限制,但我强烈建议你使用其他类型的缓存系统(更多关于此内容).

.NET缓存

要了解更多关于.NET缓存限制,请阅读这个伟大的答案微软的.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());
}
Run Code Online (Sandbox Code Playgroud)

Memcached的

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

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

您将在此处找到有关MemCache使用的更多问题,例如:

为任何Cache系统腾出空间

要在不更改代码的情况下使用其他缓存系统,您可以采用创建接口,例如

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

那么你是在使用.NET Cache,你的实现会是这样的

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

你会用它作为:

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

如果你正在使用Memcached,你需要做的就是创建一个新的类,ICacheService通过使用IoC或直接调用来实现和选择你想要的类:

private ICacheService cacheProvider;

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

    base.Initialize(requestContext);
}
Run Code Online (Sandbox Code Playgroud)


小智 4

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

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