c# - asp.net 缓存限制?

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

<分区>

Possible Duplicate:
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 设置是否有任何限制?

最佳答案

有一种方法可以提高限制,但我会强烈建议您使用其他类型的缓存系统(下面会详细介绍)。

.NET 缓存

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

如果你想查看.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使用的问题,比如:

为任何缓存系统腾出空间

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

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

关于c# - asp.net 缓存限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14047619/

相关文章:

asp.net - 在 asp.net 中使用内联 css 是不好的做法吗?

.net - 使用 ServiceHost 在单元测试中托管 WCF 服务

sql-server - 在 SSIS 脚本任务中循环记录集目标

asp.net - 如何为 IIS 应用程序池帐户授予数据库权限?

C# 格式 DateTime 没有年份

c# - 如何覆盖帐户管理 mvc 部分的 dotnet core 中由某些内容生成的默认 "site.css"

asp.net - 需要按年份将日期列拆分为多个列

c# - 使用我的应用程序 c# 安装 sql server 数据库

c# - WCF SQL 插入 GUID 转换错误

c# - 使用 iText7 和 ASP.NET C# 在 PDF 文件的页脚中添加页码