Redis as a cache server

Although Redis is much more than a cache, But it's possible to use as cache server specially in distributed applications. Redis lets you operate on the values and it's just a key value pair in memory database.

To getting start, get the latest version of Redis from here and install in your  windows machine (Server) by instruction and run it in your services. Redis runs in your server machine (you can have redis server and client in one machine which could be you client development machine) and you call it from client by a library. Now to work with Reidis in a server you need to  get stack exchange via Nuget and implement your methods provided by it.

Note: You can read all descriptions and instructions of stack exchange redis in github.

 

  1.    public class RedisCacheManager
  2.     {
  3.         private readonly ConnectionMultiplexer _connectionMultiplexer;
  4.         private readonly IDatabase _dataBase;
  5.         private IServer _server;
  6.  
  7.         private static Lazy<ConfigurationOptions> configurations = new Lazy<ConfigurationOptions>(() =>
  8.         {
  9.             var configuration = new ConfigurationOptions();
  10.             configuration.EndPoints.Add("192.168.110.149:6421");
  11.             configuration.ConnectTimeout = 100000;
  12.             configuration.SyncTimeout = 100000;
  13.             configuration.AllowAdmin = true;
  14.             configuration.Password = "skdfenr4";
  15.             return configuration;
  16.         });
  17.  
  18.         public RedisCacheManager()
  19.         {
  20.             _connectionMultiplexer = ConnectionMultiplexer.Connect(configurations.Value);
  21.             _dataBase = _connectionMultiplexer.GetDatabase();
  22.             var endpoints = _connectionMultiplexer.GetEndPoints();
  23.             _server = _connectionMultiplexer.GetServer(endpoints.First());
  24.         }
  25.  
  26.         public object Retrieve(string key)
  27.         {
  28.             var rValue = _dataBase.StringGet(key);
  29.             if (!rValue.HasValue)
  30.                 return rValue;
  31.             return JsonHelper.Deserialize<object>(rValue);
  32.         }
  33.  
  34.         public T Retrieve<T>(string key)
  35.         {
  36.             var rValue = _dataBase.StringGet(key);
  37.             if (!rValue.HasValue)
  38.                 return JsonHelper.Deserialize<T>(null);
  39.             return JsonHelper.Deserialize<T>(rValue);
  40.         }
  41.  
  42.         public T Retrieve<T>(string key, int pageIndex)
  43.         {
  44.             var rValue = _dataBase.StringGet(key);
  45.             if (!rValue.HasValue)
  46.                 return JsonHelper.Deserialize<T>(null);
  47.             return JsonHelper.Deserialize<T>(rValue);
  48.         }
  49.  
  50.         public bool Contain(string key)
  51.         {
  52.             return _dataBase.KeyExists(key);
  53.         }
  54.  
  55.         public void Store(string key, object value)
  56.         {
  57.             if (value == null)
  58.                 return;
  59.  
  60.             var entryBytes = JsonHelper.Serialize(value);
  61.             _dataBase.StringSet(key, entryBytes);
  62.         }
  63.  
  64.         public void Store(string key, object value, int cacheTime)
  65.         {
  66.             if (value == null)
  67.                 return;
  68.  
  69.             var entryBytes = JsonHelper.Serialize(value);
  70.             var expiresIn = TimeSpan.FromMinutes(cacheTime);
  71.  
  72.             _dataBase.StringSet(key, entryBytes, expiresIn);
  73.         }
  74.  
  75.         public void Remove(string key)
  76.         {
  77.             _dataBase.KeyDelete(key);
  78.         }
  79.  
  80.         public void Clear()
  81.         {
  82.             var endpoints = _connectionMultiplexer.GetEndPoints(true);
  83.             foreach (var endpoint in endpoints)
  84.             {
  85.                 var server = _connectionMultiplexer.GetServer(endpoint);
  86.                 server.FlushAllDatabases();
  87.             }
  88.         }
  89.  
  90.         private static byte[] Serialize(object item)
  91.         {
  92.             var jsonString = JsonConvert.SerializeObject(item);
  93.             return Encoding.UTF8.GetBytes(jsonString);
  94.         }
  95.  
  96.         private static T Deserialize<T>(byte[] serializedObjectValue)
  97.         {
  98.             if (serializedObjectValue == null)
  99.                 return default(T);
  100.  
  101.             var jsonString = Encoding.UTF8.GetString(serializedObjectValue);
  102.             return JsonConvert.DeserializeObject<T>(jsonString);
  103.         }
  104.     }

 

I Just implemented the most used methods in a caching system, you can implement your own based on you needs. As you see I created configurations with some initializing. for example EndPoints(you can get it from the server that Redis is running on) and password (for making the connection between client and server secure).  In the constructor of the class you need to connect to Redis server via ConnectionMultiplexer and get the database to work with.


Tags: Libraries


comments powered by Disqus