.NET缓存如何可调过期的工作? [英] .NET Caching how does Sliding Expiration work?

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

问题描述

如果我使用一个ObjectCache,并添加像这样一个项目:

If I use an ObjectCache and add an item like so:

ObjectCache cache = MemoryCache.Default;
string o = "mydata";
cache.Add("mykey", o, DateTime.Now.AddDays(1));

据我所知,对象将在1天内到期。但是,如果该对象被访问1/2天以后使用:

I understand the object will expire in 1 day. But if the object is accessed 1/2 a day later using:

object mystuff = cache["mykey"];

这是否重置计时器,所以它现在是1天,因为用钥匙的myKey条目的最后一次访问,或仍1/2的一天,直到到期?

Does this reset the timer so it's now 1 day since the last access of the entry with the key "mykey", or it still 1/2 a day until expiry?

如果答案是否定的,有一个办法做到这一点,我很想知道。

If the answer is no and there is a way to do this I would love to know.

感谢。

推荐答案

<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.cacheitempolicy.absoluteexpiration.aspx">AbsoluteExpiration将到期的时间设定量后的条目。

AbsoluteExpiration will expire the entry after a set amount of time.

<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.cacheitempolicy.slidingexpiration.aspx">SlidingExpiration如果它尚未在一个设定的时间量访问将过期的条目。

SlidingExpiration will expire the entry if it hasn't been accessed in a set amount of time.

您可以使用。该ObjectCache添加你使用,将其视为绝对过期()超载,所以你需要使用其他的一个<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.add.aspx">overloads

You can use either. The ObjectCache Add() overload you're using treats it as an absolute expiration, so you'll need to use one of the other overloads

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

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