How to work with Redis Cache in .Net

Redis is an open source, fast, feature rich, in-memory caching engine that can be used to store and retrieve data in your applications

redis cache
IDG

Caching is a state management strategy that can be used to improve the performance of your applications as it helps you to reduce the consumption of resources in your system.

Redis Cache is an open source (BSD licensed), high-speed, NoSQL database. It's fast, and it runs entirely in the memory with negligible performance overhead when reading and writing data. It should be noted that Redis is free for both commercial and non-commercial use under the BSD license.

What is Redis Cache and why should I use it?

Redis is one of the most popular open source, NoSQL, in-memory based data stores available. It's an in-memory data store that can support a wide variety of data structures, i.e., strings, hashes, sets, lists, etc. Redis also provides in-built support for replication, transactions and excellent support for data persistence.

Redis is a good choice primarily if your application needs to store and retrieve a huge amount of data. Redis Cache is very easy to setup and has relatively much less performance overhead when storing and retrieving data. If your application needs to store and retrieve lots of data and availability of free memory is not a constraint, Redis Cache is the caching engine you should go for. Setting up Redis is quite simple -- the sections that follow discuss how to install, configure and use Redis.

Installing Redis

You can download a copy of Redis Cache from GitHub.

While installing, you should check the option to add Redis to the PATH environmental variable. Once Redis Cache is installed in your system, you can type Run -> service.msc to see the Redis service running in your system.

Working with Redis C# Client

Now that Redis has been installed in your system, you need a client to store and retrieve data to and from Redis Cache. In this example, we'll use the ServiceStack C# Redis open source client. To do this, create a new Console Application project in Visual Studio. You can install ServiceStack.Redis via the NuGet Package Manager.

Assuming that ServiceStack.Redis has been installed via NuGet, the following two methods illustrate how you can store and retrieve data from the Redis Cache using the ServiceStack.Redis API.

private static bool Save(string host, string key, string value)

        {

            bool isSuccess = false;

            using (RedisClient redisClient = new RedisClient(host))

            {

                if (redisClient.Get<string>(key) == null)

                {

                    isSuccess = redisClient.Set(key, value);

                }

            }

            return isSuccess;

        }

        private static string Get(string host, string key)

        {

            using (RedisClient redisClient = new RedisClient(host))

            {

                return redisClient.Get<string>(key);

            }

        }

Note how the Set and Get methods of RedisClient class have been used to store and retrieve data to and from Redis Cache. I leave it to my readers to update these two methods to make them generic so that they can work with any type.

Here's how you can call these methods from the Main method:

static void Main(string[] args)

        {

            string host = "localhost";

            string key = "IDG";

            // Store data in the cache

            bool success = Save(host, key, "Hello World!");

            // Retrieve data from the cache using the key

            Console.WriteLine("Data retrieved from Redis Cache: " + Get(host,key));

            Console.Read();

        }

As I said earlier, Redis is feature rich. In one of my future articles here, I'll discuss some advanced concepts like persistence, pub-sub, automatic failover, etc. You can take advantage of the RDB (a single compact file) or AOF way of persistence. However, you need to consider the trade-offs between performance, durability, and the disk I/O before you choose the right persistence option.

You can learn more on Redis from its documentation.

If you're interested in having a GUI admin tool to see your Redis data, you can use the Redis Admin UI tool.