C# asp.net Web服务缓存延长到期时间
我什么也没找到,所以我不相信我想要的是可能的。我想在每次访问缓存变量时重置它的滑动过期C# asp.net Web服务缓存延长到期时间,c#,asp.net,asp.net-cache,asp.net-caching,C#,Asp.net,Asp.net Cache,Asp.net Caching,我什么也没找到,所以我不相信我想要的是可能的。我想在每次访问缓存变量时重置它的滑动过期 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 w
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.
}
}
实际上,缓存项是聊天室中的消息列表。当添加一条消息时,我希望聊天室“保持活力”的时间再长一点。也欢迎使用其他方法。缓存对象的设计将在每次访问时自动重置过期时间。您所要做的就是尝试获取对象,如果对象为null,则像现在一样使用滑动过期设置它,如果不是null,则返回它。只要得到它,过期时间就会重置。 像这样
缓存对象按设计将在每次访问时自动重置过期时间。您所要做的就是尝试获取对象,如果对象为null,则像现在一样使用滑动过期设置它,如果不是null,则返回它。只要得到它,过期时间就会重置。
public static object CachedItem
{
get
{
string key = "item11"; // users share the object at this key
object o = Cache[key];
if (o == null)
{
o = {Get from some source};
Cache.Add(key, o, null, Cache.NoAbs..., new TimeSpan(0,5,0), High, Removed);
}
return o;
}
}