Table Caching Clearing not working

Mar 29, 2016 at 4:08 PM
Edited Mar 30, 2016 at 8:01 AM
Hello

i got an MVC 5.2.3, EF6.1.3 and Ninject. Im doing a Database First approach. For one Table "Teilnehmer" i dont want the cache eg. want to delete it when someone access the page. So i tried a lot, nothing worked. Tried every Discussion here, with no luck so far. Not sure if normale, Cache.Count() is allways 0 but the cache works, the database is only accessed when needed. I tried many thing, nothing worked:
public class CacheDBConfig : DbConfiguration
{
    public CacheDBConfig()
    {
        var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
        AddInterceptor(transactionHandler);
        var cachingPolicy = new MyCachingPolicy();

        Loaded +=
          (sender, args) => args.ReplaceService<DbProviderServices>(
            (s, _) => new CachingProviderServices(s, transactionHandler,
              cachingPolicy));
    }

public class MyCachingPolicy : CachingPolicy
{
    protected override void GetExpirationTimeout(System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntitySetBase> affectedEntitySets, out TimeSpan slidingExpiration, out DateTimeOffset absoluteExpiration)
    {
        absoluteExpiration = DateTimeOffset.MaxValue;
        slidingExpiration = TimeSpan.MaxValue;
    }

    protected override bool CanBeCached(System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntitySetBase> affectedEntitySets, string sql, IEnumerable<KeyValuePair<string, object>> parameters)
    {
        if (affectedEntitySets.Any(s => s.Name == "Teilnehmer"))
        {
            return false;
        }
        return base.CanBeCached(affectedEntitySets, sql, parameters);
    }

    protected override void GetCacheableRows(System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntitySetBase> affectedEntitySets, out int minCacheableRows, out int maxCacheableRows)
    {
        base.GetCacheableRows(affectedEntitySets, out minCacheableRows, out maxCacheableRows);
    }
}
}
Trying disable Cache
        internal static readonly InMemoryCache Cache = new InMemoryCache();

        List<string> entities = new List<string>();
        entities.Add("SnowWhiteEntities.Teilnehmer");
        entities.Add("SnowWhiteEntities.Teilnehmers");
        entities.Add("Teilnehmer");
        entities.Add("Teilnehmers");
        entities.Add("dbo.Teilnehmers");
        entities.Add("dbo.Teilnehmer");

        Cache.InvalidateSets(entities);
Is there a Problem with EF6.1.3. I know its not the correct way, but its just for Testinig. Table is "dbo.Teilnehmer"
Mar 29, 2016 at 7:40 PM
How do you setup your cache and what is Cache? Is it the same instance as the one that is passed to EF when registering the caching provider? Btw. the right way to prevent from caching specific entity sets is to derive from CachingPolicy and overwrite the CanBeCached method to return false for entity sets you don't want to cache. You need to pass an instance of your caching policy to the CachingProviderServices when you setup caching.

Thanks,
Pawel
Mar 30, 2016 at 8:03 AM
Edited Mar 30, 2016 at 8:58 AM
If i use my own CachingProvider it works. Is this the correct wait to exlude?

But im still wondering what i have done wrong when i use Cach.InvalidSets() isnt working in
Apr 4, 2016 at 8:00 AM
Aren't you creating one instance of InMemoryCache here:
 var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
and then try to clean the one that you create here:
internal static readonly InMemoryCache Cache = new InMemoryCache();
?

As I said above - if you need to clear the cache you need to invoke InvalidateSets on the same instance that is being used by EF.


In your caching policy - I don't think you need to override methods if you just call base.

Pawel