This component uses the Windows Server AppFabric Cache to provide distributed caching over servers and can be plugged into existing services/applications
to directly use the AppFabric for caching. This component can be used for:
I. Caching outgoing WCF service responses - If the response message is in the cache, the custom channel immediately returns
the response message from the cache without invoking the underlying service. This would not require any changes in the client implementation.
At the service, a custom attribute is to be applied on the operation for which the response is to be cached.
II. Caching specific objects in the Business Layer to cache data retrieved from
the data store (database or external services). It includes a
CacheHelper class which currently supports:
- Helper methods to read and write from the AppFabric cache.
- An API that would return the encrypted key required to store data in the cache.
- An API to reset the time to live for an object which exists in the cache.
Installing and configuring the AppFabric
There is no GUI for AppFabric. Powershell can be used for managing AppFabric. Make sure that the AppFabric
cache service is running. You can determine whether the cache host is running by calling the Get-CacheHost command.
The following shows an example output from this command for a two-server cache cluster.
HostName : CachePort Service Name Service Status Version Info
-------------------- ------------ -------------- ------------
CacheServer1:22233 AppFabricCachingService UP 1 [1,1][1,1]
CacheServer2:22233 AppFabricCachingService UP 1 [1,1][1,1]
In this example, both CacheServer1 and CacheServer2 are running.
The next step is to create a named cache, a logical container used to store data. You do this through the New-Cache cmdlet in the Windows PowerShell:
View the cache configuration settings
To view the current settings for the cache, use the Get-CacheConfig command.
In the previous example, the Get-CacheConfig command displays the cache configuration settings for Cache1. Here is an example of the output from this command:
CacheName : appFabricCache
TimeToLive : 10 mins
CacheType : Partitioned
Secondaries : 0
IsExpirable : True
EvictionType : LRU
NotificationsEnabled : False
Steps to use this component
- Add a reference to the Caching Extension library.
- You must update your application's web.config file as below:
The cache component is ready to be used. Below are the two different places where we would be leveraging this component:
- Apply the
[CacheOperationBehavior] attribute to the operations you want to cache the responses for.
[CacheOperationBehavior(MinutesToCache = 5)]
CustomerDC GetCustomer(string customerID);
- For caching responses to the calls made to the database:
- Define constants for a key and get the encrypted key using the
GetEncryptedKey(string) API of the cache helper.
- Use Cache-aside pattern as shown below:
Customer custObj = (Customer)CacheHelper.Get(CUST_KEY);
if(custObj == null)
custObj = ReadFromDataStore();
To use AppFabric caching, the component requires the following libraries:
I. Caching outgoing WCF service responses
The incoming call from the client is received through the Transport channel, and it passes through some channels before it reaches
the Dispatcher. The Dispatcher associates the incoming call with the appropriate operation and then invokes it.
I have created a custom
OperationInvoker to customize the behavior of invoking the data store.
How does it work?
The key is generated using a combination of input parameters and the operation name. But as the parameters can be any number of objects, they are serialized into an XML string first.
It also has the
TimeInMinutes property to indicate the lifetime of the cached object.
II. Caching data that is retrieved from the data store
CacheHelper class exposes the following APIs:
- Get and put methods of the
- To reset the object timeout value - defining how long objects reside in the cache before expiring. The value specified for the object overrides the default settings for the cache.
- To create encrypted keys from a string.
Health monitoring tools
The AppFabric caching features install several Performance Monitor counters. For more information about the available counters,
see Performance Counters for AppFabric Caching. You can observe or log some counter values to determine
a baseline of typical cache cluster behavior. For example, in the AppFabric Caching:Cache category, you might observe that the Total Client Requests / sec
value stays within general ranges that varies with the time of day.
There are several Windows PowerShell commands that indicate the current status and the health of a cache cluster. This section demonstrates how to use the following commands.
Download the Windows Server AppFabric here: http://www.microsoft.com/download/en/details.aspx?id=15848.