Store your application data in Redis
Redis is an in-memory data store that is open-source and can be used as an alternation for database, message broker, cache. Redis supports multiple data formats such as string, lists, sets, hashes, bitmaps, etc.
Here in this article, I'm sharing an easy and simple way to use Redis as your data store in your application.
In this sample code, we will store data as hashes that data is JSON serialized. You can install Redis on your machine or can use Azure Redis for Cache if you have an Azure account.
In order to communicate with Redis, you need to use "StackExchange.Redis" NuGet package.
Here are the things you need to do. Let's start with the interfaces.
You need an interface for Redis Connection Factory which helps to create the Redis connection for your application.
using StackExchange.Redis;namespace RedisDemo.Interfaces{public interface IRedisConnectionFactory{ConnectionMultiplexer Connection();}}
Then you need to identify which CRUD operations that your application needs and have an interface for that. In my sample, I use Create | Read | Delete operations. As I use hashes it updates the date if the key is already available in the data collection.
using System.Threading.Tasks;namespace RedisDemo.Interfaces{public interface IRedisDataProvider<T>{Task<T> GetAsync(string key);Task SaveAsync(string key, T record);Task DeleteAsync(string key);}}
Now let's move to the implementation of the above interfaces.
My IRedisConnectionFactory is implemented as below.
using StackExchange.Redis;using System;namespace RedisDemo{public class RedisConnectionFactory : IRedisConnectionFactory{private readonly Lazy<ConnectionMultiplexer> _connectionMultiplexer;public RedisConnectionFactory(string connectionString){_connectionMultiplexer = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString));}public ConnectionMultiplexer Connection(){return _connectionMultiplexer.Value;}}}
In this class, you need to pass the Redis Connection String to the constructor so that we can create an object of Lazy<ConnectionMultiplexer> which we use Lazy<T> for thread-safe initialization of objects.
Here is my sample code for the data provider class.
using StackExchange.Redis;using System;using System.Threading.Tasks;using Newtonsoft.Json;
namespace RedisDemo{public class RedisDataProvider<T> : IRedisDataProvider<T>{internal readonly IDatabase _database;protected readonly IRedisConnectionFactory _redisConnectionFactory;public RedisDataProvider(IRedisConnectionFactory redisConnectionFactory){_redisConnectionFactory = redisConnectionFactory;_database = _redisConnectionFactory.Connection().GetDatabase();}public async Task DeleteAsync(string key){if (string.IsNullOrWhiteSpace(key) || key.Contains(":")){throw new ArgumentException("invalid key!!");}await _database.HashDeleteAsync(typeof(T).Name, key);}public async Task<T> GetAsync(string key){return JsonConvert.DeserializeObject<T>(await _database.HashGetAsync(typeof(T).Name, key));}public async Task SaveAsync(string key, T record){await _database.HashSetAsync(record.GetType().Name, key, JsonConvert.SerializeObject(record));}}}
In this sample code, I used "Newtonsoft.Json" NuGet to serialize the data that I want to store in Redis.
This is it.
Cheers. Happy coding.
Comments
Post a Comment