C# .NET 缓存如何滑动过期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13637856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET Caching how does Sliding Expiration work?
提问by Kelly
If I use an ObjectCache and add an item like so:
如果我使用 ObjectCache 并添加这样的项目:
ObjectCache cache = MemoryCache.Default;
string o = "mydata";
cache.Add("mykey", o, DateTime.Now.AddDays(1));
I understand the object will expire in 1 day. But if the object is accessed 1/2 a day later using:
我了解该对象将在 1 天内过期。但是,如果一天后使用以下方法访问对象 1/2:
object mystuff = cache["mykey"];
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?
这是否会重置计时器,以便自上次使用密钥“mykey”访问条目以来现在是 1 天,还是每天 1/2 直到到期?
If the answer is no and there is a way to do this I would love to know.
如果答案是否定的,并且有办法做到这一点,我很想知道。
Thanks.
谢谢。
采纳答案by mfanto
There are two types of cache policies you can use:
您可以使用两种类型的缓存策略:
CacheItemPolicy.AbsoluteExpirationwill expire the entry after a set amount of time.
CacheItemPolicy.AbsoluteExpiration将在设定的时间后使条目过期。
CacheItemPolicy.SlidingExpirationwill expire the entry if it hasn't been accessed in a set amount of time.
CacheItemPolicy.SlidingExpiration如果在设定的时间内未访问该条目,则该条目将过期。
The ObjectCache Add()overload you're using treats it as an absolute expiration, which means it'll expire after 1 day, regardless of how many times you access it. You'll need to use one of the other overloads. Here's how you'd set a sliding expiration (it's a bit more complicated):
ObjectCache Add()您使用的重载将其视为绝对过期,这意味着它会在 1 天后过期,无论您访问它多少次。您需要使用其他重载之一。以下是设置滑动到期时间的方法(有点复杂):
CacheItem item = cache.GetCacheItem("item");
if (item == null) {
CacheItemPolicy policy = new CacheItemPolicy {
SlidingExpiration = TimeSpan.FromDays(1)
}
item = new CacheItem("item", someData);
cache.Set(item, policy);
}
You change the TimeSpan to the appropriate cache time that you want.
您将 TimeSpan 更改为所需的适当缓存时间。
回答by zinal
The SlidingExpiration property value is set using the slidingExpiration attribute of the configuration element. Sliding expiration resets the expiration time for a valid authentication cookie if a request is made and more than half of the timeout interval has elapsed. If the cookie expires, the user must re-authenticate. Setting the SlidingExpiration property to false can improve the security of an application by limiting the time for which an authentication cookie is valid, based on the configured timeout value. We recommend that if you configure requireSSL as false, you also configure slidingExpiration as false, to reduce the amount of time for which a ticket is valid.
SlidingExpiration 属性值是使用配置元素的slidingExpiration 属性设置的。如果发出请求并且超过一半的超时间隔已过,滑动过期将重置有效身份验证 cookie 的过期时间。如果 cookie 过期,用户必须重新进行身份验证。将 SlidingExpiration 属性设置为 false 可以根据配置的超时值限制身份验证 cookie 的有效时间,从而提高应用程序的安全性。我们建议,如果您将 requireSSL 配置为 false,您还可以将 slideExpiration 配置为 false,以减少票证有效的时间量。

