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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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!
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
Cache
object. A Cache is obtained from the DataCacheFactory'sGetCache
method, 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
Add
method.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.