Programming Azure Cache
Programming with Windows Azure Caching
In order to work with Windows Azure Caching from client apps, you need to add the following namespace in your client app:
using Microsoft.ApplicationServer.Caching;
To programmatically access the Windows Azure Caching, you first need to create an instance of DataCache class.
The following code block will create a cache client by creating an instance of DataCache which will take the settings from configuration file.
//Creating a cache object
DataCacheFactory cacheFactory = new DataCacheFactory();
DataCache dataCache = cacheFactory.GetDefaultCache();
DataCacheFactory cacheFactory = new DataCacheFactory();
DataCache dataCache = cacheFactory.GetDefaultCache();
The above code block will create the cache client object by using DataCacheFactory object. The GetDefaultCache method of DataCacheFactory object will return the DataCache object.
Adding and Retrieving Data from the Windows Azure Cache
To add an item to Cache object, we can use either Add or Put method of DataCache object. The Add method adds an object to the Cache object with a specified key and will generate an exception if the item is already exists with the same key. The put method adds the object to the Cache object with a specified key if the key does not exist, and replaces if the key is already exists.
The following code block adds the collection of data objects to Cache:
//Query from DB and adding data to Cache
var categories = categoryRepository.GetAll();
dataCache.Add("categories", categories);
var categories = categoryRepository.GetAll();
dataCache.Add("categories", categories);
In the above code block, querying data from Database and adding this data to Cache object with a key name “categories” and we can take the data from Cache for the subsequent requests for the same data which can avoid database round trips and improve the performance of the application.
Using the overloads of the Add and Put methods of DataCache object, we can override the timeout interval, which are specified in the Cache Roles settings.
The following code block adds the data to the Cache with time span that allows to specify when the object should expired from Cache.
dataCache.Add("categories", categories, TimeSpan.FromMinutes(45));
The above code block adds the data to the Cache object with a key categories and specified the timeout as 45 minutes.
The following code block retrieves the data from Cache:
//Getting the Cache item with key "categories"
IEnumerable category = dataCache.Get("categories") as IEnumerable;
IEnumerable
The Get method of the DataCache object retrieves the data from Cache where you have to give the key as the parameter value for the Get method. The above code block retrieves the data and casting it for the appropriate data type.
Working with a Cache Provider Class
In the previous steps, we have discussed how to perform data caching with Windows Azure Caching. Let’s create a Cache provider class that will help us better manage Data in the cache. Let’s create a contract type for the Cache provider.
public interface ICacheProvider
{
object Get(string key);
void Add(string key, object data);
void Put(string key, object data);
void Add(string key, object data, TimeSpan? timeout);
void Put(string key, object data, TimeSpan? timeout);
bool IsSet(string key);
void Remove(string key);
}
{
object Get(string key);
void Add(string key, object data);
void Put(string key, object data);
void Add(string key, object data, TimeSpan? timeout);
void Put(string key, object data, TimeSpan? timeout);
bool IsSet(string key);
void Remove(string key);
}
The Get method returns the cached item based on the given key and Add and Put methods adds and puts an item into Cache. The IsSet method will return true if an item added on the Cache. The Remove method will remove an item from the Cache.
Let’s create a concrete implementation for the ICacheProvider. The below AzureCacheProvider Class is an implementation of ICacheProvider for Windows Azure Caching.
public class AzureCacheProvider : ICacheProvider
{
private DataCache dataCache=null;
public AzureCacheProvider()
{
//Creating a cache object by using DataCacheFactory
DataCacheFactory cacheFactory = new DataCacheFactory();
dataCache = cacheFactory.GetDefaultCache();
}
//Get cached item
public object Get(string key)
{
return dataCache.Get(key);
}
public void Add(string key, object data)
{
Add(key, data, null);
}
public void Add(string key, object data, TimeSpan? timeout)
{
//Add item to Cache
if (timeout.HasValue)
{
dataCache.Add(key, data, timeout.Value);
}
else
{
dataCache.Add(key, data);
}
}
public void Put(string key, object data)
{
Put(key, data, null);
}
public void Put(string key, object data, TimeSpan? timeout)
{
//Put item to Cache
if (timeout.HasValue)
{
dataCache.Put(key, data, timeout.Value);
}
else
{
dataCache.Put(key, data);
}
}
//Returns true if Cache item is exist
public bool IsSet(string key)
{
return (dataCache.Get(key) != null);
}
public void Remove(string key)
{
dataCache.Remove(key);
}
}
{
private DataCache dataCache=null;
public AzureCacheProvider()
{
//Creating a cache object by using DataCacheFactory
DataCacheFactory cacheFactory = new DataCacheFactory();
dataCache = cacheFactory.GetDefaultCache();
}
//Get cached item
public object Get(string key)
{
return dataCache.Get(key);
}
public void Add(string key, object data)
{
Add(key, data, null);
}
public void Add(string key, object data, TimeSpan? timeout)
{
//Add item to Cache
if (timeout.HasValue)
{
dataCache.Add(key, data, timeout.Value);
}
else
{
dataCache.Add(key, data);
}
}
public void Put(string key, object data)
{
Put(key, data, null);
}
public void Put(string key, object data, TimeSpan? timeout)
{
//Put item to Cache
if (timeout.HasValue)
{
dataCache.Put(key, data, timeout.Value);
}
else
{
dataCache.Put(key, data);
}
}
//Returns true if Cache item is exist
public bool IsSet(string key)
{
return (dataCache.Get(key) != null);
}
public void Remove(string key)
{
dataCache.Remove(key);
}
}
In the below code block, we are using the ICacheProvider from an ASP.NET MVC application
private readonly ICacheProvider cache;
public CategoryController(ICategoryRepository categoryRepository,ICacheProvider cache)
{
this.categoryRepository = categoryRepository;
this.cache = cache;
}
public ActionResult Index()
{
IEnumerable categories;
var cachedCategories=cache.Get("categories");
public CategoryController(ICategoryRepository categoryRepository,ICacheProvider cache)
{
this.categoryRepository = categoryRepository;
this.cache = cache;
}
public ActionResult Index()
{
IEnumerable
var cachedCategories=cache.Get("categories");
if (cachedCategories != null)
{
categories = cachedCategories as IEnumerable;
}
else
{
categories = categoryRepository.GetAll();
cache.Put("categories", categories);
}
return View(categories);
}
{
categories = cachedCategories as IEnumerable
}
else
{
categories = categoryRepository.GetAll();
cache.Put("categories", categories);
}
return View(categories);
}
In the Index action method, we are taking the data from Cache, if the cached item exists. If the item does not exist, we retrieve the data from Database and put it on Cache so that we can take the data from Cache for the subsequent requests. In this sample code, we are injecting an instance of AzureCacheProvider class through constructor for the contract type ICacheProvider.
No comments:
Post a Comment