I am currently researching the integration of AppFabirc caching into my .net c# application and looking for some code examples of such. Are there any open source or code samples available off AppFabric caching available that I can look at?
Cache Operations
The first object to create when dealing with AppFabric caching is a DataCacheFactory
. This can be created either with hard-coded configuration data that tells the factory how to contact the cache server, or with no configuration in which case it reads the configuration from your web.config/app.config file. My recommendation is that you keep your config information in your .config file, otherwise when you want to change something in your cache setup, you'll need to re-compile and re-distribute your application. The important thing to remember about the DataCacheFactory is it is expensive to create - you definitely don't want to create one of these for every cache operation. Consider using the Singleton pattern - see this question for more details.
// Create a factory reading the config info from the .config file
DataCacheFactory factory = new DataCacheFactory();
The main interface to a cache is through the Cache
object. A Cache is obtained from the DataCacheFactory's GetCache
method, passing in the name of the cache:
DataCache myCache = factory.GetCache("myCache");
Adding An Item to the Cache
Each item in a cache has a key, which is a string. The key must be unique to the cache - if you pass in a key that already exists you'll get an exception. The item to be cached must be serialisable so AppFabric can internally pass it around the servers in the cache. At the most basic level, items are added to the cache using the Add
method.
object myCachedItem = new Object();
string myCachedItemKey = "MyCacheKey";
myCache.Add(myCachedItemKey, myCachedItem);
Removing an Item from the Cache
myCache.Remove(myCachedItemKey);
Simple as.
Getting an Item From the Cache
When getting an item from the cache, we use the cache-aside pattern. This means we look in the cache to see if the desired item is there (using the key). If the item is in the cache, we take the cached item (potentially casting it to a different type); otherwise we take steps to get the item from scratch e.g. reading from a database, and then cache it so it is there for us next time.
object cachedObject;
string myImportantDataKey = "MyImportantDataTable";
DataTable myImportantData;
// This is an object because everything is returned from the cache as an object
cachedObject = myCache.Get(myImportantDataKey);
// Check to see if we got something from the cache
if (cachedObject == null)
{
// The requested item wasn't in the cache - a cache miss
// Go get the data from the db
myImportantData = getMyImportantDataFromMyDatabase();
// Add the item to the cache so we've got it for next time
myCache.Add(myImportantDataKey, myImportantData);
}
else
{
// We have the cached object so cast it to a DataTable
myImportantData = (DataTable)cachedObject;
}
Updating an Item in the Cache
// Put can be used to update an existing item in the cache
// If the item does not exist then it functions like Add
myCache.Put(myImportantDataKey, myUpdatedImportantData);
That's the basic CRUD operations, but there's plenty more that you can get into for dealing with concurrency!
The Windows Server AppFabric Training Kit can be downloaded here, it has a section on caching. Keep following the appfabric tag here as I'm sure over time there'll be many more code samples solving problems for people.
Also worth adding that the Singleton pattern is pretty much essential if you're using the Azure AppFabric Caching service as each instatiation of a DataCacheFactory creates a new connection to the Azure AppFabric Caching service. Since the number of connections is limited based upon the size of your cache (128MB cache includes 5 connections) you'll very quickly lockout all your connections if you don't re-use the same factory!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With