Reputation: 149
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.
Upvotes: 2
Views: 815