ASP.NET Web服务缓存延长过期时间
我还没有找到任何东西,所以我不相信我想要的东西是可能的。我想在每次访问缓存变量时重置它的滑动过期时间。
public class MyCache
{
public static object CachedItem
{
get
{
string key = "item11"; // users share the object at this key
object o = Cache[key];
//re-set the timer janky way
//triggers callback, which I dont want
o = (o == null) ? new object() : Cache.Remove(key);
Cache.Add(key, o, null, Cache.NoAbs..., new TimeSpan(0,5,0), High, Removed);
return o;
}
}
private static void Removed(string key, object value, CacheItemRemovedReason reason)
{
// audit MySql table
// no good because Cache.Remove is getting called manually a lot.
}
}
实际上,缓存项是聊天室中的消息列表。添加消息后,我希望聊天室“保持活力”更长一点。也欢迎其他方法。
I haven't found anything so I dont believe what I want is possible. I want to reset the sliding expiration of a cache variable every time it is accessed.
public class MyCache
{
public static object CachedItem
{
get
{
string key = "item11"; // users share the object at this key
object o = Cache[key];
//re-set the timer janky way
//triggers callback, which I dont want
o = (o == null) ? new object() : Cache.Remove(key);
Cache.Add(key, o, null, Cache.NoAbs..., new TimeSpan(0,5,0), High, Removed);
return o;
}
}
private static void Removed(string key, object value, CacheItemRemovedReason reason)
{
// audit MySql table
// no good because Cache.Remove is getting called manually a lot.
}
}
In practice the cache item is a list of messages in a chat room. When a message is added I want the chatroom to "stay alive" a little bit longer. alternate methods also welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓存对象的设计会在每次访问时自动重置过期时间。您应该做的就是尝试获取该对象,如果它为空,则像现在一样使用滑动过期设置它,如果不为空则返回它。只需获取它,过期时间就会被重置。
像这样
The cache object by design will automatically reset the expiration each time it is accessed. All you should have to do is try to get the object, if its null, set it with the sliding expiration like you have now, if not null return it. Just by getting it, the expiration is reset.
Like so