I'm trying to get 2nd level caching to work with entity framework 4. The "EF Provid开发者_JAVA百科er Wrappers" made by Jarek Kowalski (http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747) works pretty good, the problem i have is that all cached entries from a table is invalidated as soon as an update is made to the table. Is this intended, or have i made an error in my implementation?
If this is intended, it makes it completely useless on tables which have a lot of updates. Are there any way to remedy this?
This is my implementation of the ICache interface, using ScaleOut StateServer as cache:
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);
}
}
Yes it is intentional. The author has mentioned it in the same link that you have shared.
"EFCachingProvider is a bit more complex. It uses external caching implementation and caches results of all queries queries that are executed in DbCommand.ExecuteReader(). Whenever update is detected (either UPDATE, INSERT or DELETE) the provider invalidates affected cache entries by evicting all cached queries which were dependent on any of the updated tables."
I am not sure what a clean solution would be for your case. But if your table is very frequently updated you better not cache entries of that table. You can use "CustomCachingPolicy" to exclude that table from being cached.
"CustomCachingPolicy – includes user-configurable list of tables that should and should not be cached, as well as expiration times and result size limits."
精彩评论