2011-05-25 68 views
1

我还没有找到任何东西,所以我不相信我想要的是可能的。我想在每次访问时重置高速缓存变量的滑动过期。asp.net webservice缓存延长到期

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. 
} 
} 

实际上,缓存项目是聊天室中的消息列表。当添加消息时,我希望聊天室能够“活得更久”。替代方法也欢迎。

+1

设计缓存对象会在每次访问时自动重置过期。所有你应该做的就是尝试获取对象,如果它为null,则使用滑动过期来设置它,如果不是null,则返回它。只要得到它,到期将被重置。 – Jay 2011-05-25 21:19:43

回答

1

设计中的缓存对象会在每次访问时自动重置到期日。所有你应该做的就是尝试获取对象,如果它为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; 
    } 
}