Simple Sophisticated Object Cache Service Using Azure Redis
Cache service has been an integral part of major distributed systems. So let's dive deep into how to build a sophisticated object cache service using Redis.
Join the DZone community and get the full member experience.
Join For FreeThe usage of cache has evolved a long way from simple string key-value pair to a string key to any object. Each application scenario demands the usage of cache in a different way. Most of the applications want to partition cache and reuse in other microservices or other parts of the system at the granular level. We will dive into detail about implementing a sophisticated object cache service at a granular level using Azure Redis in C#.
Prerequisites:
- Access to the Azure portal.
- Azure Redis provisioned of desired SKU.
The main objective of object storing is to set and get objects. But depending on application scenarios, usage of cache needs to be partitioned at the granular level. So let's define three variants of object store API.
API to:
- Global cache
- Partition cache by single key to add more distribution and uniqueness.
- Partition cache by double key to add even more distribution and uniqueness.
[ApiController]
[Route("api/objectcache")]
public class ObjectCacheController : ControllerBase
{
// Simple global cache to set
[HttpPost]
public async Task<bool> Set ([FromBody] object value, [FromQuery] string key)
{
}
// Simple global cache to get
[HttpGet]
public async Task<object> Get ([FromuQuery] string key)
{
}
// Global cache partitioned by single record
[HttpPost(“/pk1/{partitionkey1}”)]
public async Task<bool> SetBySinglePartition ([FromBody] object value, [FromuQuery] string key, string partitionkey1)
{
}
// Global cache partitioned by single entity
[HttpGet(“/pk1/{partitionkey1}”)]
public async Task<object> GetBySingleParititon ([FromuQuery] string key, string partitionkey1)
{
}
// Global cache partitioned by two entities for more uniqueness
[HttpPost(“/pk1/{partitionkey1}/pk2/{partitionkey2}”)]
public async Task<bool> SetByDoublePartition ([FromBody] object value, [FromuQuery] string key, string partitionkey1, string partitionkey2)
{
}
// Global cache partitioned by two entities for more uniqueness
[HttpGet(“/pk1/{partitionkey1}/pk2/{partitionkey2}”)]
public async Task<object> GetByDoublePartition ([FromQuery] string key, string partitionkey1, string partitionkey2)
{
}
}
To implement an object store, let's use Azure Redis as our backend storage.
To leverage Redis, we need to provide a common interface for API to access.
Let's put backend storage as an enum option. Example:
public enum ObjectStoreHandlerType
{
Redis,
CosmosDb,
PostGre
}
Let's define the interface IObjectsStoreHandler that provides abstraction and functionalities toward backend storage.
public interface IObjectStoreHandler
{
Task<T> GetObjectAsync(string key);
Task<bool> SetObjectAsync<T>( string key, T value);
Task<T> GetObjectBySinglePartitionAsync<T>(string key, string partitionkey1);
Task<bool> SetObjectBySinglePartitionAsync<T>(string key, string partitionkey1, T value);
Task<T> GetObjectByDoublePartitionAsync<T>(string key, string partitionkey1, string partitionkey2);
Task<bool> SetObjectBySinglePartitionAsync<T>(string key, string partitionkey1, string partitionkey2, T value);
ObjectStoreHandlerType Type { get; }
}
To follow OOD paradigms, let's define an ObjectStoreHandlerfactory to create different ObjectStores based on the handler type.
public interface IObjectStoreHandlerFactory
{
IObjectStoreHandler CreateObjectStoreHandler(ObjectStoreHandlerType objectStoreProviderType = ObjectStoreHandlerType.Redis);
}
public class ObjectStoreHandlerFactory : IObjectStoreHandlerFactory
{
private readonly IEnumerable<IObjectStoreHandlers> _objectStorehandlers;
public ObjectStoreProviderFactory(IEnumerable<IObjectStoreHandlers> objectStorehandlers)
{
_objectStorehandlers = objectStorehandlers;
}
public IObjectStoreHandler CreateObjectStoreHandler(ObjectStoreProviderType requestedHandlerType)
{
return _objectStorehandlers.SingleOrDefault(handlers => handlers.Type == requestedHandlerType);
}
}
Let's define Redis ObjectStoreHandler. Then, you can create a simple RedisClient.
public class RedisObjectStoreHander : IObjectStoreHandler
{
private readonly RedisClient _redisClient;
public RedisObjectStoreProvider(RedisClient redisClient)
{
_redisClient= redisClient;
}
public ObjectStoreHandlerType Type => ObjectStoreHandlerType.Redis;
public async Task<T> GetObjectAsync<T>(string key)
{
RedisKey redisKey = $"{key}";
try
{
var redisValue = await _redisClient.GetConnectedDatabase().StringGetAsync(redisKey);
string strValue = redisValue.ToString();
if (!string.IsNullOrWhiteSpace(strValue))
{
return JsonConvert.DeserializeObject<T>(strValue);
}
}
catch (Exception ex)
{
}
return default;
}
public async Task<bool> SetObjectAsync<T>(string key, T value)
{
RedisKey redisKey = $"{key}";
RedisValue redisValue = JsonConvert.SerializeObject(value);
try
{
await _redisConnection.GetConnectedDatabase().StringSetAsync(redisKey, redisValue, flags: ComandFlags.DemandMaster);
return true;
}
catch (Exception ex)
{
}
return false;
}
public async Task<T> GetObjectBySinglePartitionAsync<T>(string key, string partitionkey1); {
RedisKey redisKey = $" {{partitionkey1}}_{key}";
try
{
var redisValue = await _redisClient.GetConnectedDatabase().StringGetAsync(redisKey);
string strValue = redisValue.ToString();
if (!string.IsNullOrWhiteSpace(strValue))
{
return JsonConvert.DeserializeObject<T>(strValue);
}
}
catch (Exception ex)
{
}
return default;
}
public async Task<bool> SetObjectBySinglePartitionAsync <T>( string key, string partitionkey1, T value)
{
RedisKey redisKey = $" {{partitionkey1}}_{key}";
RedisValue redisValue = JsonConvert.SerializeObject(value);
try
{
await _redisConnection.GetConnectedDatabase().StringSetAsync(redisKey, redisValue, flags: ComandFlags.DemandMaster);
return true;
}
catch (Exception ex)
{
}
return false;
}
public async Task<T> GetObjectByDoublePartitionAsync<T>(string key, string partitionkey1, string partitionkey2); {
RedisKey redisKey = $"{{{partitionkey1}_{partitionkey2}}}_{key}";
try
{
var redisValue = await _redisClient.GetConnectedDatabase().StringGetAsync(redisKey);
string strValue = redisValue.ToString();
if (!string.IsNullOrWhiteSpace(strValue))
{
return JsonConvert.DeserializeObject<T>(strValue);
}
}
catch (Exception ex)
{
}
return default;
}
public async Task<bool> SetObjectByDoublePartitionAsync <T>( string key, string partitionkey1, string partitionkey2, T value)
{
RedisKey redisKey = $"{{{partitionkey1}_{partitionkey2}}}_{key}";
RedisValue redisValue = JsonConvert.SerializeObject(value);
try
{
await _redisConnection.GetConnectedDatabase().StringSetAsync(redisKey, redisValue, flags: ComandFlags.DemandMaster);
return true;
}
catch (Exception ex)
{
}
return false;
}
}
That's it. You can now integrate RedisObjectStoreHandler with the API layer.
private readonly IObjectStoreHandler _objectStoreHandler;
public ObjectCacheController(IObjectStoreHandlerFactory objectStoreHandlerFactory)
{
_objectStoreHandler = objectStoreHandlerFactory.Create(ObjectStoreHandlerType.Redis);
}
You can now have a robust object store service that can take in unique keys with granular level partitions and can store/retrieve objects of the desired type.
Opinions expressed by DZone contributors are their own.
Comments