尽管设置了滑动到期时间,但存储在ASP.NET缓存中的对象永不到期的可能原因有哪些? [英] What are some possible reasons for objects stored in the ASP.NET Cache never expiring, despite having a sliding expiration time set?

查看:81
本文介绍了尽管设置了滑动到期时间,但存储在ASP.NET缓存中的对象永不到期的可能原因有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ASP.NET缓存使用以下代码缓存了一个值:

I've cached a value using the ASP.NET Cache, with the following code:

Cache.Insert("TEST_VALUE", 150, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(120));

据我了解,这意味着如果120秒钟内没有任何对象访问该对象,它将过期并返回null。

As I understand it, this should mean that if nothing accesses that object for 120 seconds, it will expire and return null.

但是,如果10分钟后我运行了一个页面,该页面从缓存中写出了该值,则该页面仍然存在(实际上一整个小时后仍然存在) 。我知道没有别的东西可以访问它了,因为它们都在我机器上的本地服务器上。

However, if after 10 minutes I run a page which writes out that value from the cache, it's still there (and indeed still after a whole hour). I know there's nothing else accessing it, because this is all on a local server on my machine.

有人可以告诉我为什么这些值没有从缓存中清除吗?

Can anyone tell me why these values aren't getting purged from the cache?

谢谢,我明白你的意思了,但是我的HttpModule在将任何内容插入高速缓存之前正在检查请求的类型。 ,所以它只会在表单上传中发生。

Thanks, I see what you mean, but my HttpModule is checking the type of request before inserting anything into the cache, so it will only occur on form uploads.

无论如何,我都尝试了您的建议,刷新显示缓存的页面时,断点永远不会被命中值。

I've tried what you suggested anyway, and the breakpoint never gets hit when I am refreshing the page that displays the cached value.

我假设即使从缓存中读取也可以延长对象的寿命,所以我在刷新调试页面之前将其保留了5-10分钟,但是值仍然存在!

I am assuming that even reading from the cache should extend the lifespan of the object, so I am leaving it 5-10 minutes before refreshing the 'debugging' page, but the value is still there!

推荐答案

好的,我想我已经知道了,但是我不确定为什么这是:

OK, I think I've figured it out, but I'm not sure exactly why this is:

我按照你们俩的建议做了-绝对没有从缓存中读取内容,因此我编写了一个回调,该回调将删除时间写入了日志文件并订阅了它到到期事件。我还将每个项目添加到缓存的时间写到同一日志中,这样我就可以看到东西在其中停留了多长时间。

I did as you both suggested - there was definitely nothing reading from the cache, so I wrote a callback which wrote the removal time to a log file and subscribed it to the expiry event. I also wrote the time each item was added to the cache into the same log, so I could see how long things were staying in there.

在分配了最初在代码中将值赋给Cache,然后出现一个循环,在该循环中,我使用以下每次迭代将新值分配给同一对象:

After I'd assigned the value to the Cache initially in the code, there was then a loop where I assigned a new value to the same object on each iteration, using:

Cache["TEST_VALUE"] = counter;

但是,使用此语法时,日志文件显示该项目已从缓存,即使仍然可以通过写出缓存对象值的单独页面访问它?

However with this syntax, as soon as this happened the log file showed the item had been removed from the cache, even though it was still accessible by the separate page that just wrote out the values of the cached objects?

我不明白为什么会这样-我知道上面的语法只会更新缓存中对象的值,但是也许我有这个错误吗?

I don't understand why this should be the case - I understood that the above syntax would just update the value of the object in the cache, but maybe I have this wrong?

请问一下,我更改了代码,以便循环上方的一行现在显示为:

Messing around, I changed the code so that the above line in the loop now read:

Cache.Insert("TEST_VALUE", newvalue, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(120));

现在在日志中,我得到相同的删除通知,紧接着是另一个添加通知,并且项目在120(ish)秒后正确到期,并且无法再从其他页面访问。

And now in the log I get the same removal notification, followed immediately by another addition notification, and the item expires correctly after 120(ish) seconds and is no longer accessible from other pages.

我很困惑,但是感谢您的帮助:)

I'm very confused, but thanks for your help :)

这篇关于尽管设置了滑动到期时间,但存储在ASP.NET缓存中的对象永不到期的可能原因有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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