How to Control Cache data

Aug 28, 2015 at 12:20 PM
Hi,

I use Caching Policy like that;

protected override void GetExpirationTimeout(System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntitySetBase> affectedEntitySets, out TimeSpan slidingExpiration, out DateTimeOffset absoluteExpiration)
    {

          absoluteExpiration = DateTimeOffset.Now;
        slidingExpiration = TimeSpan.FromMinutes(2);
    }

    // Kendi seçelim hangi tablo cachelesin
    protected override bool CanBeCached(System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntitySetBase> affectedEntitySets, string sql, IEnumerable<KeyValuePair<string, object>> parameters)
    {
        {
            //TODO Test 2 todo
            if (affectedEntitySets.Any(x => x.Name.Contains("CODE")))
            {
                return true;
            }

            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)
    {
        // Cachelenecek Min ve Max Row Sayısını
        minCacheableRows = 0;
        maxCacheableRows = 100;
        base.GetCacheableRows(affectedEntitySets,out minCacheableRows,out maxCacheableRows);
    }

I develop Mvc project and I want to control cache object from controller.And if object will be cached passed the database query and show cahed object.Does it possible?
Aug 31, 2015 at 6:44 AM
Hi BoraOtekin,

It seems odd to me that you want to control EF cache from the controller given that the cache sits between EF and the database. You do create a cache when you register the caching provider so you should be able to somehow store it and then be able to access it if you absolutely must. I would recommend however to think about the architecture of your application and a caching strategy (i.e. what you want/need to cache and why) to see if it is really necessary.

One think I noticed it that you set absoluteExpiration = DateTimeOffset.Now; - this way you never cache anything since all the cache entries you create become expired immediately after they are created.

Hope this helps,
Pawel