Programming the Cache API
namespace Microsoft.ApplicationServer.Caching { public sealed class DataCacheFactory : IDisposable { public DataCacheFactory(); public DataCacheFactory( DataCacheFactoryConfiguration configuration); public void Dispose(); public DataCache GetCache( string cacheName); public DataCache GetDefaultCache(); }
DataCacheFactoryConfiguration
element --> <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version= , Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
public DataCacheItemVersion Add(string key, object value); public DataCacheItemVersion Add(string key, object value, TimeSpan timeout); public DataCacheItemVersion Put(string key, object value); public DataCacheItemVersion Put(string key, object value, TimeSpan timeout); Parameterdescription keyKey of item in the distributed cache valueValue of object stored in the cache (serialized) timeoutHow long before the item should expire
//create DataCacheFactory based on config file var dcf = new DataCacheFactory(); //get the cache named "TestCache" var cache = dcf.GetCache("TestCache"); //Add an item called "Test" - throws if exists cache.Add("Test", new Data {TheData = "Test" }); //Put an item called "Test2" - overwrites if exists cache.Put("Test2", new Data { TheData = "Test2" }); //Get "Test3" - add if not in cache (cache-aside) var test3 = cache.Get("Test3") as Data; if (test3 == null) { test3 = new Data {TheData = "Test3" }; cache.Add("Test3", test3); }
element --> <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version= , Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />