Pros | Cons |
---|---|
In memory data stored | 120 GB limitation |
Distributed cache | Only a few different data types can exist by default, but there are enough for a cache system |
Geo-replication (only for Premium) | Don’t try to store data in Redis Cache that has inheritance. It will be a nightmare and again this is not a use case for a (key,value) database solution |
Data persistence to disk (only for premium) | |
(key, value) pair store. Each operation is atomic. | |
Full support for expiration keys. | |
Replicated cache in multiple nodes (starting with standard package). | |
Service-level agreement 99.9% (starting with standard package). | |
Supports multiple data types. | |
Publisher and Subscriber support. | |
Better scaling support. |
High performance: “Azure Cache for Redis achieves superior throughput and latency performance by storing data in memory instead of on disk. It consistently serves read and write requests within single-digit milliseconds, delivering exceedingly fast cache operations to scale data tiers as application loads increase..”- Microsoft Reference
public static class RedisCache
{
private static readonly Lazy<ConnectionMultiplexer> lazyConnection = new
Lazy<ConnectionMultiplexer>(
() =>
{
string cacheConnection = GlobalEnvironmentConfiguration.CacheConnection;
return ConnectionMultiplexer.Connect(cacheConnection);
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}
public class RedisCacheService : IRedisCacheService
{
public async Task SetAsync(string key, T value)
{
IDatabase cache = RedisCache.Connection.GetDatabase();
return await cache.StringSetAsync(key, JsonConvert.SerializeObject(value));// expiration - new TimeSpan(0,0,1,0), When.Exists);
}
public async Task GetAsync(string key)
{
IDatabase cache = RedisCache.Connection.GetDatabase();
var cachedObject = await cache.StringGetWithExpiryAsync(key);
if (cachedObject.Value == RedisValue.Null || cachedObject.Value == RedisValue.EmptyString)
{
return default(T);
}
return JsonConvert.DeserializeObject(cachedObject.Value);
}
}