我还没有找到任何东西,所以我不相信我想要的东西是可能的。我想在每次访问缓存变量时重置它的滑动到期。
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.
}
}
实际上,缓存项是聊天室中的消息列表。添加消息后,我希望聊天室“保持活力”更长一点。也欢迎替代方法。