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?
I am currently researching the integration of AppFabirc caching into my .net c# application
Share
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.The main interface to a cache is through the
Cacheobject. A Cache is obtained from the DataCacheFactory’sGetCachemethod, passing in the name of the cache: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
Addmethod.Removing an Item from the Cache
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.
Updating an Item in the Cache
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.