且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net 缓存限制?

更新时间:2023-02-25 09:13:20

有办法升级限制,但我强烈建议你使用其他类型的缓存系统(更多关于这个).

There is a way to upgrade the limit but I would strongly recommend that you use other kind of Caching System (more about this below).

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

To know more about the .NET Caching limitation, please read this great answer from a Microsoft .NET Team member.

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

If you want to see the current limits of .NET Cache, you can try:

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

I'm currently using Memcached, and if you're hosting your site somewhere, you can use a paid service like:

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

Or, if you're using your own server, you can download Couchbase Community Edition and hosted our own.

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

You will find more questions here about the use of MemCache, such as:

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

To use other cache system without changing your code, you could adopt to create an interface, like

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

那么您是否正在使用 .NET 缓存,您的实现将类似于

then is you're using .NET Cache, your implementation would be something like

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 或直接调用来选择所需的类:

if you're using Memcached, all you need to do is create a new class that implement ICacheService and select the class you want, either by using IoC or direct call as:

private ICacheService cacheProvider;

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

    base.Initialize(requestContext);
}