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

查看:155
本文介绍了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团队成员的rel =nofollow noreferrer>这个很棒的答案

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 ,如果您在某个地方托管您的网站,您可以使用付费服务,例如:

MemCached

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

  • http://www.memcachier.com/

或者,如果您使用自己的服务器,可以下载 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:

  • Which .NET Memcached client do you use, EnyimMemcached vs. BeITMemcached?
  • how to start with memcached

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

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 Cache,你的实现会像

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,你需要做的就是创建一个实现<$的新类c $ c> 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天全站免登陆