Entlib Cache.Contains NULL problem

Topics: Caching Application Block
Mar 24, 2009 at 8:17 AM

Hi,

I have a combined authorization and menustructure system on our backend. For performance reasons EntLib caching is used in the frontend client (MVC rel 1.0 website, IIS 5.1 local, IIS 6.0 server, no cluster).

Sometimes 'Cache.Contains' will return true, but the contents of the cache is NULL. I know for certain that I filled it correctly, so what can be the problem here?

EDIT: when I set the cache to 1 minute and add the cacheKey 'A_Key', I will see the key coming back when inspecting the CurrentCacheState. When I view CurrentCacheState after 2 minutes, the key is still there. When I execute 'contains', true is returned. When I execute 'contains' again, the cacheKey is gone! Synchronization problem??

Regards, Michel

---

if (IntranetCaching.Cache.Contains(cacheKey))
{
    menuItems
= (List<BoMenuItem>)IntranetCaching.Cache[cacheKey];
}
else
{
   
using (AuthorizationServiceProxyHelper authorizationServiceProxyHelper = new AuthorizationServiceProxyHelper())
   
{
        menuItems
= authorizationServiceProxyHelper.Proxy.SelectMenuByUserAndApplication(APPNAME, userName, AuthorizationType.ENUM_LOGIN);
       
IntranetCaching.Add(cacheKey, menuItems);
   
}
}

---

public static class IntranetCaching
{
   
public static ICacheManager Cache { get; private set; }

   
static IntranetCaching()
   
{
       
Cache = CacheFactory.GetCacheManager();
   
}

   
public static void Add(string key, object value)
   
{
       
Cache.Add(
            key
           
, value
           
, CacheItemPriority.Normal

           
, null
           
, new Microsoft.Practices.EnterpriseLibrary.Caching.Expirations.AbsoluteTime(TimeSpan.FromMinutes(10)));
   
}
}

---

Mar 24, 2009 at 9:04 AM
Most likely, the BackgroundScheduler hasn't performed its sweeping yet.  If you're going to examine the source code, the Contains method only checks if the specific key is present in the inmemory cache hashtable while on the GetData method, the code first checks if the item has expired, if it has, the item is removed from the cache.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 24, 2009 at 9:40 AM
Sarah, that seems to work, thank you.

What is the added value of 'Contains' when its outcome cannot be used in a sensible way?
Shouldn't the same code be executed like 'GetData' does?
Mar 24, 2009 at 12:00 PM

I can't answer design questions but if you feel the need that the two needs to both check for expirations, you can always log it in the issue tracker and get people to vote.

Just a thought,  even if you perform checking for expirations in the Contains method, it's entirely possible that in that split nanosecond between calls from Contains and GetData that that
item will expire. :)


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 24, 2009 at 1:08 PM
True, I'll add it to the issuetracker (hiding or removing 'Contains').

Thanks