Windows Server AppFabric Cache—Custom Cache Provider Programming
Join the DZone community and get the full member experience.Join For Free
The primary advantage with Custom Cache Provider is to enable Read-Through and Write-Behind patterns and have control in the way items are stored and fetched from AppFabric Cache. Further these custom cache providers reduce multiple calls made to SQL Server repository and it takes care of reading and writing to database internally. Hence the cache client is abstracted from making explicit calls to storage repository.
Custom Cache Provider
1. Implement your custom cache class that inherits from Microsoft.ApplicationServer.Caching.DataCacheStoreProvider
2. Reference the following assemblies inside your project library:
3. Your custom cache class should have a constructor that takes arguments as string cacheName, Dictionary<string, string> config.
4. These values are assigned as and when this custom cache assembly is loaded through New-Cache command from Powershell administration console. The primary intention of these variables is to store any configurable items that are passed through Powershell console. An example would be to pass a unique name to Custom Cache assembly or even connection string that you would consume inside your cache assembly.
5. Override the following methods inside your custom cache class:
public override void Delete(System.Collections.ObjectModel.Collection<DataCacheItemKey> keys) public override void Delete(DataCacheItemKey key) protected override void Dispose(bool disposing) public override void Read(System.Collections.ObjectModel.ReadOnlyCollection<DataCacheItemKey> keys, IDictionary<DataCacheItemKey, DataCacheItem> items) public override DataCacheItem Read(DataCacheItemKey key) public override void Write(IDictionary<DataCacheItemKey, DataCacheItem> items) public override void Write(DataCacheItem item)
6. The Read method is invoked when a call to Get method from Cache client is triggered by passing key and region name.
7. Items are retrieved from cache and returned back to caller when one exists. Otherwise, you should route the call to your custom method that would facilitate to retrieve value from external storage repository.
8. Similarly, the Write method is invoked when a call to Put method from Cache client is triggered by passing key, region and value.
9. In the case of Write-behind pattern, these values are first persisted in cache and written to external data storage repository at later point in time. The default time to write back is 60 seconds which an adjusted through Powershell administration console.
Opinions expressed by DZone contributors are their own.