Entity Framework and Layer 2 Caching with EF Provider Wrappers

I am trying to get second-level caching for working with entity framework 4. "EF provider wrappers" made by Yarek Kowalski ( http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747 ) works pretty well , the problem is that all cached records from the table are invalid as soon as the update is done in the table. Is this the goal, or have I made a mistake in my implementation?

If it is intended, this makes it completely useless for tables that contain many updates. Is there any way to fix this?

This is my implementation of the ICache interface using ScaleOut StateServer as a 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); } } 
+4
source share
1 answer

Yes, this is intentional. The author mentioned this in the same link that you shared.

"EFCachingProvider is a bit more complicated: it uses an external caching implementation and caches the results of all query requests that are executed in DbCommand.ExecuteReader (). Whenever an update is detected (either UPDATE, INSERT, or DELETE), the provider will invalidate the affected cache entries, causing all cached queries that depend on any of the updated tables. "

I'm not sure what a clean solution will be for your case. But if your table is updated very often, it is better not to cache the records of this table. You can use "CustomCachingPolicy" to exclude caching of this table.

"CustomCachingPolicy - includes a user-configurable list of tables that should and should not be cached , as well as the expiration time and the size limits of the result."

+3
source

Source: https://habr.com/ru/post/1338111/


All Articles