In my last post I showed how I fixed a ton of errors that were being received with a very simple approach for working with the Azure Cache.
This time I will share how I dealt with serialization issues because I decided to add a property to an object I stored in the cache then deployed, BOOM.
Another simple approach, when an error happens trying to get the item from the cache (then casting it), remove that item from the cache and the next call will add it back in and life will go on. If you do not do this the item will be attempted to be retrieved many times each throwing an exception.
EXAMPLE
public static CustomerInfo GetCustomerInfoByAccessKey(string accessKey)
{
if (RoleEnvironment.IsAvailable)
{
try
{
//GetCacheItem is the method from my previous post
var customerCacheItem = GetCacheItem(accessKey);
if (customerCacheItem == null)
{
var customer = RetrieveCustomerInfoByAccessKeyFromDB(accessKey);
if (customer != null)
{
DataCache.Put(customer.AccessKey, customer, TimeSpan.FromHours(2));
}
return customer;
}
else
{
return (CustomerInfo)customerCacheItem.Value;
}
}
catch (Exception ex)
{
//Trys to Remove (i.e. the remove is in a try/catch) the
//CustomerInfo object related to the accessKey from the cache
FlushCustomer(accessKey);
//I want to know this happened
Log.Error("Caching Error: " + ex, CategorySources.Caching);
}
}
//Either not running in azure or emulator or the try to get it form the cache has failed
return RetrieveCustomerInfoByAccessKeyFromDB(accessKey);
}