Programming Microsoft ASP.NET 4 - Dino Esposito [391]
Important
Note that some old documentation and literature still refer to this feature as one of two possible deployment strategies: routing and simple. In the final version of ACS, the simple deployment mode is no longer supported. The drawback is that the same stale documentation and literature suggests you add a deployment attribute to the dataCacheClient section, which will cause a runtime configuration exception as the code attempts to gain access to the cache factory.
If you enable the local cache, any retrieved objects are saved in a local cache within the ACS client, thus forming an additional layer of virtual memory. The local cache takes precedence over data in the cluster. Note that ACS performs no automatic checks on data in the cluster to detect possible recent changes that occurred for the objects in the local cache. In other words, by using the local cache you trade speed of data retrieval for data accuracy.
Note
To install AppFabric Caching Services on a machine intended to serve as a cache cluster, you also need to have IIS 7 Manager for Remote Administration enabled on that machine.
Programming Caching Services
Let’s look at some sample code. The entry point in ACS is the cache factory object. The factory, then, will gain you access to any data cache available in the system. The simplest way to get a factory is shown here:
var factory = new DataCacheFactory();
The factory needs to consume some information to be successfully instantiated. You can provide configuration settings as a constructor parameter or let the class figure it out itself from the configuration file. Here’s the fluent code you can use to initialize the factory without using the web.config file:
var servers = new List { new DataCacheServerEndpoint("Server01", 22233) }; var configuration = new DataCacheFactoryConfiguration { Servers = servers, LocalCacheProperties = new DataCacheLocalCacheProperties() }; var factory = new DataCacheFactory(configuration); The next steps are straightforward. First, you get a data cache and then you start reading and writing data to it. A data cache must be created administratively using the Windows PowerShell console. Here’s the command you need to create a named cache. (As mentioned, you use the name default to create the default unnamed cache.) New-Cache -cachename yourCache In this way, you get a named cache with the same default settings as the default unnamed cache. Of course, you can express additional parameters on the command line. For example, the following line creates a cache with no eviction policy enabled: New-Cache -cachename yourCache -eviction:none To get command line information, you can type the following: New-Cache -? If the requested cache is not available, the constructor of DataCacheFactory just throws an exception: var factory = new DataCacheFactory(); var dinoCache = factory.GetCache("Dino"); var defaultCache = factory.GetDefaultCache(); From this point on, you can use the object returned by GetCache and GetDefaultCache in much the same way you would use the native Cache object of ASP.NET. With just a little difference, you now can access information stored across a (expansible) cluster of servers: dinoCache[key] = value; Of course, you might want to call the factory and get cache objects only once in the application, preferably at startup. Unlike to the ASP.NET Cache, you don’t have dependencies, but you do have regions, search capabilities, and a rich event model (through which you can simulate some of the ASP.NET cache dependencies). Note Interestingly, after you install AppFabric Caching Services, you also have a new (and free) out-of-process session provider that uses caching services to store your session data. You might want to check out the documentation to find out more details. Other Solutions