asp.net 缓存限制? [英] asp.net caching limit?

查看:18
本文介绍了asp.net 缓存限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I'm caching quite a lot of datatables using asp.net caching (the floowing code):

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

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

However I think that the cache on the server is getting full and having to re-obtain the datatable data from the database. Is there any limit to the amount of data that can be cached on the server or any IIS settings that can be tweaked?

推荐答案

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

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

这篇关于asp.net 缓存限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆