How To Implement Redis With Episerver Easily

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. It’s probably worth saying that 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.

When it comes to implementing Redis we have two flavours:

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. There’s a much better write up than I could do here as well.

How To Use Redis?

Now we know which 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:

public IDatabase SetRedisConnection()
{
var connectionString = string.Format("{0}:{1}", "127.0.0.1", 6379);
var connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString);
var cache = connectionMultiplexer.GetDatabase();
return cache ;
}

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:

var cache = connectionMultiplexer.GetDatabase(1);

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.

connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(connectionString);

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

using StackExchange.Redis;
public class RedisCacher
{
private static IDatabase cache;
public RedisCacher()
{
cache = SetRedisConnection()
}
public object GetValue(string key)
{
return cache.StringGet(key);
}
public bool Add(string key, object value, TimeSpan? absExpiration)
{
return cache.StringSet(key, value.ToString(), absExpiration);
}
}     

In the code above, we can now use the IDatabase cache object, to do a task like Add, Get, Delete. I won’t go over this code as it’s pretty self-explanatory.

Conclusion

In today’s guide, I’ve had a brief discussion about what Redis is and why it’s a better caching solution when you’re working with very large amount of os cached data compared to standard Memory Cache. Redis comes with two popular.NET flavor, ServiceStack, and StackExchange. I would recommend Stackoverflow flow as it’s open source and has better async support.

Integrating Redis with Episerver is exactly the same as you would implement a normal MVC site. You install a Redis database on a server, you create a connection to the server and then you use the Stackoverflow.Redis API to perform tasks against it.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *