Search code examples
cachingazureazure-caching

windows azure: shared cache - delete all?


I'm using Windows Azure Shared Caching. I encountered a few problems:

  1. How to know what keys are present in the cache? Is there something like a GetAllKeys() method?
  2. Is it possible to call clearAll()?
  3. Why can't I use regions?

Thanks.


Solution

  • This section applies to Windows Azure Caching

    Windows Azure provides two types of cache modes:

    • Dedicated Role caching - The role instances are used exclusively for caching (there is no other code running in that instance).
    • Co-located Role caching - The cache shares the VM resources (bandwidth, CPU, and memory) with the application.

    How to know what is in the cache? Is there something like "GetAllKeys()" method?

    Do you need that information for your application of more for reporting / auditing? I think, Microsoft did not provide that method for one good reason: the information it returned could be obsolete shortly after. See, cache items may expire any time (depends on expiration time and time of adding item to cache) so information you would receive from GetAllKeys() method could be invalid seconds or even milliseconds later.

    Cache usage standard pattern would be

    • Get item from cache by a key
    • If cache return Null then create that item and put / add into the cache
    • Perform operation on the item (either taken from cache or recreated)

    Co-located Role caching

    Is it possible to clearAll()?

    I do not think you should worry about purging your cache. If you set the cache eviction policy to LRU (Last Recently Used) then the least recently used items are discarded first. So you will never get anything like "no space in cache".

    Why can't I use regoins?

    You can but only with cache locate on the same instance. Dedicated Role caching does not support it.


    This section applies to Windows Azure Shared Caching

    Windows Azure Shared Caching is very similar to Windows Azure Caching (described above) from client side point of view and all of the explanations applies to Shared Caching too.

    There is a small change to items eviction: In Shared Caching, items without a specific expiration time will expire after 48 hours. However, you can add items to the cache (via various overloads of the Add and Put methods) with an explicit expiration time, such as X minutes or Y days.

    When you exceed the size of your cache (cache sizes you chose during creation), the caching service will start "evict items" in the cache until the memory issue is resolved (you have enough memory to add new cache items). During "eviction" LRU mechanism is used - the least recently used items in the cache are removed.

    Get, check, and recreate approach (described above) of dealing with cache items will work for Shared Caching too.

    I hope that will help you to better understand Azure Caching and Shared Caching.