shaair
shaair

Reputation: 985

HttpRuntime.Cache is not expiring?

I have a problem with the following code. I have the following code,

            if (HttpRuntime.Cache[cacheKey] == null)
            {
                AddTask(cacheKey, JsonConvert.SerializeObject(result), 60 * 30);
            }
            r = JsonConvert.DeserializeObject<Result>(HttpRuntime.Cache[cacheKey].ToString());
            HttpRuntime.Cache[cacheKey] = JsonConvert.SerializeObject(r); 


           private static void AddTask(string key, string value, int seconds)
            {
                HttpRuntime.Cache.Insert(key, value, null,
                    DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration,
                    CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemoved));
            }

    public static void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
    {
    }

What I am doing is simply adding and updating(if exist) the cache. Then after 10 seconds I wanna to check something. But my CacheItemRemoved callback never called.

Upvotes: 2

Views: 1113

Answers (1)

shaair
shaair

Reputation: 985

When ever update the cache using

HttpRuntime.Cache[cacheKey] = JsonConvert.SerializeObject(r);

then cache time reset to the default time. Now instead of using string now i used object instance without updating cache.its worked

notification = HttpRuntime.Cache[cacheKey] as Notification;
HttpRuntime.Cache.Insert(key, notificationResult , null,
                DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration,
                CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemoved));

Upvotes: 1

Related Questions