C# .NET 4 ObjectCache-我们可以挂接到一个;“缓存已过期”;事件
我缓存了一个简单的对象,如下所示:C# .NET 4 ObjectCache-我们可以挂接到一个;“缓存已过期”;事件,c#,caching,.net-4.0,objectcache,C#,Caching,.net 4.0,Objectcache,我缓存了一个简单的对象,如下所示: _myCache.Add(someKey, someObj, policy); var policy = new CacheItemPolicy { Priority = CacheItemPriority.Default, SlidingExpiration = TimeSpan.FromHours(1) }; 其中\u myCache声明为ObjectCache(但通过DI注入为MemoryCache.Default),someObj
_myCache.Add(someKey, someObj, policy);
var policy = new CacheItemPolicy
{
Priority = CacheItemPriority.Default,
SlidingExpiration = TimeSpan.FromHours(1)
};
其中\u myCache
声明为ObjectCache
(但通过DI注入为MemoryCache.Default
),someObj
是我要添加的对象,policy
是CacheItemPolicy
如果我有这样一个CacheItemPolicy
:
_myCache.Add(someKey, someObj, policy);
var policy = new CacheItemPolicy
{
Priority = CacheItemPriority.Default,
SlidingExpiration = TimeSpan.FromHours(1)
};
这意味着它将在1小时后过期。酷
但是将会发生的是,倒霉的第一个用户在一小时后将不得不等待点击
是否有任何方法可以挂接到“过期”事件/委托并手动刷新缓存
我看到有人提到了CacheEntryChangeMonitor
,但在我的示例中找不到任何关于如何利用它的有意义的文档/示例
另外,我知道我可以使用CacheItemPriority.NotRemovable
并手动使其过期,但在我当前的示例中,我不能这样做,因为缓存的数据有点太复杂(例如,我需要在代码中的10个不同位置“作废”)
有什么想法吗?在
CacheItemPolicy
上有一个名为RemovedCallback
的属性,类型为:CacheEntryRemovedCallback
。不知道他们为什么不走标准的活动路线,但这应该可以满足你的需要
这次聚会迟到了,但我刚刚注意到CacheItemUpdate和CacheItemRemove回调之间有一个有趣的区别 特别是这一评论: 与CacheItemRemovedReason枚举不同,此枚举不会 不包括已删除或未充分使用的值。可更新的缓存项 不可移除,因此无法通过 ASP.NET,即使需要释放内存
这是我在缓存过期时使用
CacheRemovedCallback
事件的方法
我和你一样关心谁
public static void SetObjectToCache<T>(string cacheItemName, T obj, long expireTime)
{
ObjectCache cache = MemoryCache.Default;
var cachedObject = (T)cache[cacheItemName];
if (cachedObject != null)
{
// remove it
cache.Remove(cacheItemName);
}
CacheItemPolicy policy = new CacheItemPolicy()
{
AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds(expireTime),
RemovedCallback = new CacheEntryRemovedCallback(CacheRemovedCallback)
};
cachedObject = obj;
cache.Set(cacheItemName, cachedObject, policy);
}
public static void CacheRemovedCallback(CacheEntryRemovedArguments arguments)
{
var configServerIpAddress = Thread.CurrentPrincipal.ConfigurationServerIpAddress();
long configId = Thread.CurrentPrincipal.ConfigurationId();
int userId = Thread.CurrentPrincipal.UserId();
var tagInfoService = new TagInfoService();
string returnCode = string.Empty;
if (arguments.CacheItem.Key.Contains("DatatableTags_"))
{
// do what's needed
Task.Run(() =>
{
});
}
}
publicstaticvoid SetObjectToCache(字符串cacheItemName,T obj,long expireTime)
{
ObjectCache=MemoryCache.Default;
var cachedObject=(T)cache[cacheItemName];
if(cachedObject!=null)
{
//移除它
cache.Remove(cacheItemName);
}
CacheItemPolicy policy=新的CacheItemPolicy()
{
AbsoluteExpiration=DateTimeOffset.Now.AddMillimes(expireTime),
RemovedCallback=新建CacheEntryRemovedCallback(CacheRemovedCallback)
};
cachedObject=obj;
Set(cacheItemName、cachedObject、policy);
}
公共静态void CacheRemovedCallback(CacheEntryRemovedArguments参数)
{
var configServerIpAddress=Thread.CurrentPrincipal.ConfigurationServerIpAddress();
long configId=Thread.CurrentPrincipal.ConfigurationId();
int userId=Thread.CurrentPrincipal.userId();
var tagInfoService=new tagInfoService();
string returnCode=string.Empty;
if(arguments.CacheItem.Key.Contains(“DatatableTags”))
{
//做需要做的事
Task.Run(()=>
{
});
}
}
这个链接不包含代码示例-事实上,MSDN非常缺少用于运行时缓存的代码示例。我终于弄明白了,但你可以给我举个例子来节省一个小时。“在.NET 4.0中使用MemoryCache”甚至在聚会之后,但我刚刚注意到您指的是System.Web.Caching
命名空间中的类型,而问题是System.Runtime.Caching
命名空间。@StevenLiekens,他们将缓存移动到.NET 4中的System.RunTime,因为它非常有用。您仍然可以在.NET 4和up.btw中使用System.Web.caching
,无需设置Priority=CacheItemPriority.Default,因为此默认值是默认设置的:)