2

I am not very familiar with exactly how the cache works, so I hope I'm including enough information.

Basically, my problem is that we're caching items, and they appear to be added to the cache all right, but they don't seem to expire, or they expire whenever they feel like it. I am not using sliding expiration.

The item I am testing with currently has an expiration of 300 (5 minutes) but tested 30 minutes later, the item is still being returned from the cache. I have had items supposed to expire after a day still showing up 7 days later. Therefore, our changes/edits are not showing on the site.

Following is the code we have tried.

    public static void AddtoCache(object objToCache, CacheLevel cacheLevel, string cacheKey)
    {
        if (objToCache == null)
        {
            return;
        }

        if (HttpContext.Current.Cache != null)
        {
            HttpContext.Current.Cache.Insert(cacheKey, objToCache, null, DateTime.Now.AddSeconds((double)cacheLevel), System.Web.Caching.Cache.NoSlidingExpiration);
        }
    }

and also:

    public static void AddtoCache(object objToCache, CacheLevel cacheLevel, string cacheKey)
    {
        if (HttpContext.Current.Cache != null)
        {
            if (HttpContext.Current.Cache[cacheKey] == null)
            {
                HttpContext.Current.Cache.Add(cacheKey, objToCache, null, DateTime.UtcNow.AddSeconds((double)cacheLevel), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
            }
        }
    }

This happens both in development and production. We're using C# on Windows Server 2008 R2 Sp1 with IIS 7.5

UPDATE: This is the code we use to retrieve the item for display, in case that might be the problem.

public static T GetFromCache<T>(string cacheKey)
{
    object cachedObj = HttpContext.Current.Cache[cacheKey];

    if (cachedObj != null)
    {
        return (T)cachedObj;
    }
    return default(T);
}

UPDATE #2: The items that are being cached are all being displayed with .NET user controls (ascx) in case that matters. We are using Ektron as our CMS.

UPDATE 04/16 I've used the CacheItemRemovedCallback delegate and logged items getting removed from the cache on my dev site. When I refresh the page even 20 minutes after initial load (with cache expiry set to 5 minutes), the same timestamp appears and nothing is added to my log. There are a few other items in the log with "Reason: Removed". There is only one item that ever appears with "Reason: Expired" but it isn't the menu I'm testing. So it seems that at least one control is working properly, but not the others.

user0474975
  • 149
  • 3
  • 13
  • 1
    Are you sure it's the same object and not a different object that has identical values? – Matthew Apr 07 '15 at 19:34
  • Print `objToCache.GetHashCode()` to the page. Maybe you'll find the object instance change but the values to stay the same as Matthew suggested. – usr Apr 07 '15 at 19:39
  • @Matthew I am not sure how to tell that. When I refresh the page, the same item (a menu, in my test case) appears with the timestamp I added from when the page was initially loaded. The initial loading takes a few seconds longer, so I can tell it's not "rebuilding" the item, but rather getting it from the cache. And of course the timestamp is a dead giveaway :) – user0474975 Apr 07 '15 at 19:40
  • @usr Okay I will try that now, but it will take a few minutes to test. – user0474975 Apr 07 '15 at 19:44
  • 1
    Are you able to make a short, self contained example of the issue. If you actually store a `DateTime` with an expiry in the very near future, can you see if in fact it does not clear the cache when you expect? – Matthew Apr 07 '15 at 19:45
  • 1
    @usr - the hash is showing as a number 20928884 - but it hasn't changed in 12 minutes. – user0474975 Apr 07 '15 at 20:09
  • @Matthew For testing purposes, I am appending a DateTime.UtcNow.ToString() to each item in the menu before it's cached. So the DateTime updates each time the menu is re-cached. I am seeing that the DateTime stays the same for far longer than expected. It will update if I remove the item from the cache or if I recycle the application pool. Is that what you mean? – user0474975 Apr 07 '15 at 20:20
  • How far is your timezone away from UTC? Maybe try this with simply `DateTime.Now.AddSeconds(...)`. – H H Apr 07 '15 at 20:28
  • Hank, we are -4 from UTC. However, originally we had DateTime.Now.Add and the UTC was just one of the things we tried to see if it would help the item expire. Sadly, it did not. – user0474975 Apr 07 '15 at 20:30
  • 1
    Do you have other caching such as view caching? Print the cache item using Debug.WriteLine. – usr Apr 07 '15 at 20:30
  • Use UTC in any case. If not your cache will misbehave twice a year due to daylight savings. Always use UTC for internal values. – usr Apr 07 '15 at 20:30
  • @usr - I am really new to caching. How do I know if I have view caching? I have looked at the user control I'm using for testing and I don't see anything explicit, nor have I noticed anything in any of our other controls. Unless .NET automatically caches all user controls with some setting? – user0474975 Apr 08 '15 at 12:42
  • All of that is not important. Use Debug.WriteLine to make sure – usr Apr 08 '15 at 12:52
  • So I tried Debug.WriteLine. I placed it in a for loop in the Page_Load just before databinding the menu (which is in a repeater). It cycles through the object that was returned and displays the same text in the Output window as the repeater does in the browser. I hope that helps. – user0474975 Apr 08 '15 at 15:59
  • Yes, good. That rules out huge amounts of potential problems. – usr Apr 08 '15 at 20:46
  • Why don't you used the Remove delegate to find out if the object is actually being evicted or not... – Erik Funkenbusch Apr 10 '15 at 15:09
  • @ErikFunkenbusch - Can you provide a link or example on how to do that and I can try it? – user0474975 Apr 10 '15 at 15:41
  • Something wrong with the documentation? https://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcallback(v=vs.110).aspx – Erik Funkenbusch Apr 10 '15 at 15:49
  • @ErikFunkenbusch - you didn't specify "CacheItemRemovedCallback Delegate" - so my Google search for "Remove delegate" didn't turn up anything very useful. Thanks for the link to the documentation, I will try it. – user0474975 Apr 10 '15 at 17:38

0 Answers0