Windows Server AppFabric Cache Patterns of use
Outline Reference data Activity data Resource data Priming Batching
Classify your data When using the cache Classification can help organize Caches Regions Remember that availability, expiration, and eviction are set on a per cache basis Reference Activity Resource Primary Read Only Read-Write Not shared Read-Write, Shared Catalog Data Shopping Cart Auction Data/Seat Assignment
Reference data Data that doesn’t change often (or at all) List of countries, catalog items, descriptions, images Excellent candidate for caching Since it doesn’t change frequently also a good candidate for using with local cache Think about turning off expiration and eviction
Activity Data Typically data relating to one client Important data Shopping cart Important data Configure high availability (HA) Can also use Session provider from ASP.NET
Resource data Data that is contentious Need to use locking Seating assignment for flights or concerts Need to use locking Optimistic Pessimistic
Using Optimistic concurrency Clients must retrieve DataCacheItemVersion Pass to overload of DataCache.Put If newer version is already in the cache Put throws an exception Use DataCache.GetCacheItem to retreive the DataCacheItemVersion DataCache.Get does have overload with an out param DataCache.GetIfNewer to avoid full round trip
Concurrency APIs public DataCacheItemVersion Put(string key, object value, DataCacheItemVersion oldVersion); public DataCacheItemVersion Put(string key, object value, DataCacheItemVersion oldVersion, string region); public DataCacheItemVersion Put(string key, object value, DataCacheItemVersion oldVersion, TimeSpan timeout); public DataCacheItemVersion Put(string key, object value, DataCacheItemVersion oldVersion, TimeSpan timeout, string region); Parameter description key Key of item in the distributed cache value Value of object stored in the cache (serialized) timeout How long before the item should expire oldVersion DataCacheItemVersion held by client region Region to place item
Using optimistic concurrency var version = cache.Add("NewKey", "NewValue"); try { cache.Put("NewKey", "OtherValue", version); } catch (DataCacheException ex) Console.WriteLine(ex.ToString()); //some other process or thread cache.Put("NewKey", "ChangedValue");
On demand This is the most typical pattern of cache usage Application looks for cached data If not found pulls from real data source Puts the data in the cache AKA Cache-aside pattern
On demand object data = null; var key = "DataKey"; data = cache.Get(key); if (data == null) { data = GetData(); cache.Add(key, data); }
Priming Priming is a pattern where you pull data into the cache before the first request for it Typically done on application start Priming is especially useful with Reference data
Batching Batching in general is always more effective Best used with reference data Used with Tags or Regions in AppFabric Cache DataCache.BulkGet List<string> keys = GetKeys(); var batchItems = cache.BulkGet(keys, "BulkRegion");
Summary You need to classify the data you are storing in the cache to be able to choose the correct features DataCacheVersion enables optimistic concurrency pattern (resource data)
Questions?