我正在尝试让实体框架4与二级缓存配合使用。Jarek Kowalski制作的“EF Provider Wrappers”(http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747)效果不错,但我遇到的问题是,只要对表进行更新,所有缓存条目都会失效。这是故意的吗?还是我的实现有误?
如果这是故意的,那么它在经常有更新的表上完全没用。有没有什么方法可以解决这个问题?
这是我对ICache接口的实现,使用ScaleOut StateServer作为缓存:
如果这是故意的,那么它在经常有更新的表上完全没用。有没有什么方法可以解决这个问题?
这是我对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);
}
}