Share via


Microsoft Azure Redis Cache - Implementation of a service to work cache data

This article walks you through configuration Azure Redis as CacheManager.

Redis is one of the fastest and feature-rich key-value stores to come from the NoSQL movement. It is similar to memcached but the dataset is not volatile, and values can either be strings lists, sets, sorted sets or hashes.

You can download the Redis Client in any one of the following ways:

  • Packaged by default in ServiceStack.dll
  • Available to download separately as a stand-alone ServiceStack.Redis.dll
  • As Source Code via Git: git clone git://github.com/ServiceStack/ServiceStack.Redis.git
  • For those interested in having a GUI admin tool to visualize your Redis data should check out the Redis Admin UI

STEP1 - Create cache on Azure

To create a cache, first sign in to the Azure management portal, and click New, Redis Cache.

https://code.msdn.microsoft.com/site/view/file/142984/1/redis_001.png

STEP2 - Configure cache clients

On this example we will use StackExchange.Redis cache client, but Azure redis Cache could be called from any client .NET.

In order to use Redis as CacheManager we need to install a Nuget package.

So on the Visual Studio 2015, select the follow menu option:

Tools-> Library Package manager -> Manage NuGet Packages for Solution

Search for Redis and select the option Install.

 

https://code.msdn.microsoft.com/site/view/file/142985/1/redis_002.png

 

STEP3 - Send and retrieve data from cache

Items can be stored in and retrieved from a cache by using the StringSet and StringGet methods.

I create a base Azure Redis Cache class to get access to redis database

C#

public interface IRedisCache 
    { 
        bool IsAlive(); 
        ConnectionMultiplexer Conn { get; } 
        IDatabase DBInstance { get; } 
    }

 

Then create a Cache Service class that instanciate the base one .

On this class I implement the base methods, to work with data (Exists, Save, Get and Remove)

C#

public interface ICacheService 
    { 
        bool IsAlive(); 
        bool Exists(string key); 
        void Save(string key, string value, TimeSpan? expiry = null); 
        void Save(string key, int value, TimeSpan? expiry = null); 
        string Get(string key); 
        List<string> Get(List<string> keys); 
        void Remove(string key); 
    }

 

Resources:

Some good resources about Signal could be found here: