Question

in Windows Azure Shared Cache:
suppose I do the following:

try
{
  mCache.Remove(key);
  Trace.WriteLine("removed successfully from Azure Shared Cache");
}
catch (DataCacheException e)
{
  WorkerRole.log.Info(e.ToString());
}

is it right to say that if the code reached to the Trace.WriteLine command, then the operation completed successfully? (otherwise, it would be throwing DataCacheException.

I know I can register to the event CacheOperationCompleted, but is my code can be a good alternative to test operation success? (for Put/Remove methods).

thanks

Was it helpful?

Solution

Remove method returns boolean flag indicating whether an item identified by the given keyhas been removed (true) or not (false).

So if you want to check operation result I would suggest following approach:

if(mCache.Remove(key))
{
    Trace.WriteLine("removed successfully from Azure Shared Cache");
}

As far as Put method is concerned, this might be a bit more complicated.

Basically, if your cache.Put(key, value) method completes successfully (no exceptions), you can assume your item has been added to your cache.

However, Azure cache items can be evicted from the cache (that depends on the cache size and item expiration time - default item expiration time is 48 hours for shared caching)

So in order to avoid any "surprises" I would recommend cache usage pattern as follows:

  • Get item from cache by a key
  • If cache return Null then create that item and put it into the cache
  • Perform operation on the item

Also as a side note, I would recommend using in-role caching instead of shared caching service (mostly because it is cheaper, has more features etc.).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top