【发布时间】:2011-06-20 01:16:34
【问题描述】:
我正在尝试让二级缓存与实体框架 4 一起使用。Jarek Kowalski (http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747) 制作的“EF Provider Wrappers”效果很好,我遇到的问题是表中的所有缓存条目一旦对表进行更新,就会失效。这是有意为之,还是我在实施过程中犯了错误?
如果这是有意的,那么它在有很多更新的表上完全没用。有没有办法解决这个问题?
这是我实现的 ICache 接口,使用 ScaleOut StateServer 作为缓存:
public class SossCache : ICache
{
private readonly NamedCache SossCache;
public SossCache(string cacheName)
{
this.SossCache = CacheFactory.GetCache(cacheName);
}
public bool GetItem(string key, out object value)
{
value = this.SossCache.Get(key);
return value != null;
}
public void PutItem(string key, object value, IEnumerable<string> dependentEntitySets, TimeSpan slidingExpiration, DateTime absoluteExpiration)
{
bool isAbsoluteTimeout = slidingExpiration == TimeSpan.Zero;
TimeSpan timeout = isAbsoluteTimeout ? absoluteExpiration.Subtract(DateTime.Now) : slidingExpiration;
CreatePolicy createPolicy = new CreatePolicy(timeout, isAbsoluteTimeout, ObjectPreemptionPriority.Normal, dependentEntitySets.ToArray(), true);
this.SossCache.Insert(key, value, createPolicy, true, false);
}
public void InvalidateItem(string key)
{
this.SossCache.Remove(key);
}
public void InvalidateSets(IEnumerable<string> entitySets)
{
foreach (string key in entitySets)
InvalidateItem(key);
}
}
【问题讨论】:
标签: entity-framework