ASP.net缓存绝对过期不工作 [英] ASP.net Cache Absolute Expiration not working

查看:212
本文介绍了ASP.net缓存绝对过期不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储HttpContext.Cache一个整数值从现在的5分钟绝对过期时间。然而,等待6分钟(或更长)后,将整数值仍然在高速缓存(即,它永远不会被删除,即使绝对过期已过)。这里是code我使用的:

I am storing a single integer value in HttpContext.Cache with an absolute expiration time of 5 minutes from now. However, after waiting 6 minutes (or longer), the integer value is still in the Cache (i.e. it's never removed even though the absolute expiration has passed). Here is the code I am using:

public void UpdateCountFor(string remoteIp)
{
    // only returns true the first time its run
    // after that the value is still in the Cache
    // even after the absolute expiration has passed
    // so after that this keeps returning false
    if (HttpContext.Current.Cache[remoteIp] == null)
    {
        // nothing for this ip in the cache so add the ip as a key with a value of 1
        var expireDate = DateTime.Now.AddMinutes(5);
        // I also tried:
        // var expireDate = DateTime.UtcNow.AddMinutes(5); 
        // and that did not work either.
        HttpContext.Current.Cache.Insert(remoteIp, 1, null, expireDate, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
    }
    else
    {
        // increment the existing value
        HttpContext.Current.Cache[remoteIp] = ((int)HttpContext.Current.Cache[remoteIp]) + 1;
    }
}

我第一次跑UpdateCountFor(127.0.0.1),并将其插入到1与关键127.0.0.1和5分钟从现在预期绝对过期缓存。以后每运行然后递增缓存中的价值。然而,等待10分钟后继续递增在缓存中的值。该值永远不会过期,永远不会从缓存中删除。这是为什么?

The first time I run UpdateCountFor("127.0.0.1") it inserts 1 into the cache with key "127.0.0.1" and an absolute expiration of 5 minutes from now as expected. Every subsequent run then increments the value in the cache. However, after waiting 10 minutes it continues to increment the value in the Cache. The value never expires and never gets removed from the Cache. Why is that?

这是我的理解是绝对过期时间意味着该项目将得到当时约拆除。难道我做错了什么?我误解的东西吗?

It's my understanding that an absolute expiration time means the item will get removed approximately at that time. Am I doing something wrong? Am I misunderstanding something?

我期待值5分钟的时间里之后被从缓存中删除,但是它停留在那里,直到我重新生成项目。

I'm expecting the value to be removed from the Cache after 5 minutes time, however it stays in there until I rebuild the project.

这是所有在.NET 4.0我的本地机器上运行。

This is all running on .NET 4.0 on my local machine.

推荐答案

原来,这行:

HttpContext.Current.Cache[remoteIp] = ((int)HttpContext.Current.Cache[remoteIp]) + 1;

删除previous价值,并与没有绝对或滑动过期时间重新插入值。为了解决这个问题,我不得不创建一个辅助类,并使用它像这样:

removes the previous value and re-inserts the value with NO absolute or sliding expiration time. In order to get around this I had to create a helper class and use it like so:

public class IncrementingCacheCounter
{
    public int Count;
    public DateTime ExpireDate;
}

public void UpdateCountFor(string remoteIp)
{
    IncrementingCacheCounter counter = null;
    if (HttpContext.Current.Cache[remoteIp] == null)
    {
        var expireDate = DateTime.Now.AddMinutes(5);
        counter = new IncrementingCacheCounter { Count = 1, ExpireDate = expireDate };
    }
    else
    {
        counter = (IncrementingCacheCounter)HttpContext.Current.Cache[remoteIp];
        counter.Count++;
    }
    HttpContext.Current.Cache.Insert(remoteIp, counter, null, counter.ExpireDate, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}

这会得到解决的问题,并让柜台绝对正确的时间到期,同时还使更新它。

This will get around the issue and let the counter properly expire at the absolute time while still enabling updates to it.

这篇关于ASP.net缓存绝对过期不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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