他们有什么缺点在CacheItemUpdateCallback中有一个逻辑,特别是我在里面调用WCF服务吗?
我希望我的应用程序在缓存中查找并让更新回调执行更新我昂贵的缓存项目的工作。这是一个好方法吗?
这是我的示例代码:
public class CacheItemRepository : ICacheItemRepository
{
private readonly IItemService _service;
private readonly ICacheManager _cacheManager;
private static string CacheItemKey = "_itemkey_";
public CacheItemRepository(IItemService service, ICacheManager cacheManager)
{
_service = service;
_cacheManager = cacheManager;
}
public IList<Item> GetAllItem()
{
IList<Item> item = null;
if (_cacheManager.TryGet(CacheItemKey, out item))
return item;
item = _service.GetAllItem();
// Assumming I have CachePolicy here that expire every 15 secs.
_cacheManager.Add(CacheItemKey, item, CachePolicy, CacheItemUpdateCallback );
return item;
}
private void CacheItemUpdateCallback(string key, object value, CacheItemRemovedReason removedReason)
{
var itemList = value as IList<Item>;
var items = _service.GetAllItem(); // Assume this return IList<Item>
// Compare itemlist to items and if the two is not the same
// Assuming I have comparer here
// Remove old cache
_cacheManager.Remove(CacheItemKey);
// And add new one and assumming I have CachePolicy here that expire every 15 secs.
_cacheManager.Add(CacheItemKey, item, CachePolicy, CacheItemUpdateCallback );
}
}