Base solution for your next web application
Open Closed

Cache - am I missing something? #2856


User avatar
0
joemo created

I am calling the following every 10 seconds as a test:-

var cache = _cacheManager.GetCache("PendingTasksCache");
                return _cacheManager
                                  .GetCache("PendingTasksCache")
                                  .Get("pendingtaskscount", () =>
                                            GetPendingTasksCountQuery());

Which in turn calls this:-

private int GetPendingTasksCountQuery()
        {
            return _ticketRepository.Count(x => x.Status == "Pending") 
                   + _changeRequestRepository.Count(x => x.Status == "Pending")
                   + _contractRepository.Count(x => x.Status == "Pending")
                   + _orderRepository.Count(x => x.Status == "Pending");
        }

And the cache itself is set like this for a 30 second window ( as a test):-

Configuration.Caching.Configure("PendingTasksCache", cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromSeconds(30);
            });

The first time it is called, it fetches the data successfully. But I would expect it to refresh the cache and rebuild the data every 30 seconds. But it never does and never calls GetPendingTasksCountQuery again. Where am I going wrong or what have I misunderstood?


5 Answer(s)
  • User Avatar
    0
    joemo created

    Anyone? I'll give you a chocolate biscuit

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Did you solve your problem ? Your usage seems fine but why do you have the first line ?

    var cache = _cacheManager.GetCache("PendingTasksCache");
    
  • User Avatar
    0
    nicolas33 created

    Hi,

    Isn't the definition of a "sliding cache" is that it doesn't recalculate data until it hasn't been accessed for the "slidingTimeExpiration" ?

    If it's set to 30s and you're accessing it every 10s, it will never recalculate data.

  • User Avatar
    0
    joemo created

    Ah ok - so I am missing something. I was hoping to use it to store that data for an amount of time and prevent frequent slow queries during that time, retrieving from cache instead. Therefore, I guess it is the AbsoluteExpireTime I should be using instead?

  • User Avatar
    0
    nicolas33 created

    I guess it suits your needs more than the Sliding type. Remember that the cache will expire the entry after the amount of time you setted, even if no access happend.