0

I'm working in a system that uses a pricing list from a REST API, which we pay for every request. So, as these prices don't change very often I keep them in memory cache. My problem is that this cache is not refreshing, for them to refresh I have to recycle the application pool. Below is the function I use to get the prices:

public static List<PricingModel> GetItemPrices()
{
    var cache = MemoryCache.Default;
    string key = "ItemPrices";
    var val = cache[key] as List<PricingModel>;
    if (val == null)
    {
        val = new List<PricingModel>();
        val = apiProxy.GetPrices();
        foreach (var price in val)
        {
            price.Price *= 100;
        }
        CacheItemPolicy policy = new CacheItemPolicy();
        policy.AbsoluteExpiration = DateTimeOffset.Now.AddHours(1);
        cache.Set(new CacheItem(key, val), policy);
    }
    return val;
}

Am I missing something in the cache policy to make it actually refresh? What am I doing wrong?

Thanks for any help

André Luiz
  • 6,642
  • 9
  • 55
  • 105

1 Answers1

0

Can you check whether any other place in your code the same key & value is assigned? because doing so will overwrite the police & make it not expiry.

<cache disableExpiration="true"/> - is not set in your web.config?
Immanuel
  • 167
  • 1
  • 8
  • @Immanueal in the web.config there is nothing about cache. I'm going to put it forcing to false and I'll check the keys to make sure that's not the case – André Luiz Jul 18 '17 at 19:27
  • duplicate keys is not a problem I also forced disabled expiration to false. it didn't work – André Luiz Jul 18 '17 at 19:57
  • ok. though no much related to this, check this [link](https://stackoverflow.com/questions/16972641/expiring-a-cached-item-via-cacheitempolicy-in-net-memorycache) which mentions about UTC and duration to have a look at – Immanuel Jul 19 '17 at 12:26