In any high load website, how you deal with your cache is kinda important.  In most website scenarios, you might consider using the out-of-box output cache provider with memory cache and for the majority of projects this is more than adequate; on some higher load projects, however, Redis can work a lot more efficiently. 

On a recent project, I was asked to look into implementing a Redis cache solution.  Not knowing anything about Redis the task sounded quite daunting, however, after some reading around the subject implementing Redis is actually surprisingly easy with .NET. In this particular scenario we wanted to cache HTML partial snippets in Redis due to its better scaling with large amounts of data.

the reason behind this approach was to use a donut hole cache output provider. What a donut hole cache is outside this tutorial, but if you are interested then I would recommend reading Episerver Caching – Output Caching Strategies Explained…. What Is a Donut Cache?. I've eluded that Redis is better than MemoryCache in certain situations, so it's probably a good idea to cover a few of those reasons briefly.

  • Redis can scale better in a distributed server set-up
  • Redis can run on several nodes
  • Redis can more efficiently retrieve data in large data stores
  • Redis has more advanced features than mem cache, async support, event notifications, various data structure support, advanced eviction policies, blocking queues

What Is Redis and How Can I Use It?

Simply put, Redis as an advanced and really quick key/value store.  The reason why you may consider Redis over, say a 'traditional' database, is because Redis stores and retrieves data from memory and not disk.  As Redis has a powerful feature set, when performance is critical in your application then Redis will give you a lot more customization ability than Memory Cache.

I'm assuming that are you're workign with Episerver, your running a Windows box.  To get started with Redis you'll need to install it somewhere.  You can download Redis for Windows msi from, https://github.com/MSOpenTech/redis/releases.  You will also want to download the Redis Desktop client from here, so you can check that data is being added into Redis.  Think of Redis Desktop Manager as a similiar type of program that Sql Manager is to MS-SQL.

How To Get .NET Code To Talk To Redis

After you have a Redis server set-up, you'll need your code to talk to.  Asides from a conenction string to your Redis database, you'll also need .NET code that can manage this communication. Instead of having the write all this communication code yourself, you can use one of two third-party Nuget packages instead, these are:

As most websites nowaday have a heavy angular or node presence on the front-end, async calls are more common practice in most of the builds I work with, so my recommendation would be to use the StackOverFlow version, but, please feel free to do your own research.  

You can find good documentation about StackExchange.Redis here and there's a much better write up than I could do here as well (from this point onwards I'm assumign your using  the StackExchange version).

How To Use Redis In My Code?

The Redis client to use and what it does, the next step is using it.  First we need to create a connection to the Redis database, this can be done with the following code:

In the code above, we set the connection string to Redis, specifying the host and port.  We created a connection to the database and return an IDatabase instance. IN Redis you can have more than one database, so in the GetDatabase() call you can additionally specify a database ID, like so:

With Stackoverflow.Redis if you want to use async calls then you need to create an async ConnectionMultiplexer, in the SetRedisConnection method above you swap out the Connect method with the ConnectAsync method.

After you have a connection to Redis the CRUD operations are very similar to MemCache code:

The IDatabase can be used to cache objects and perform tasks like Add, Get, Delete. I won't go over this code as it's pretty self-explanatory.