Faster Redis and Easy Object Caching in Episerver

This is my second post regarding Redis. In How To Implement Redis With Episerver Easily . In my last post, I talked about the benefits of using Redis as a session state provider and a cache manager. To recap the article, Redis provides a persistence cache, you can reboot your Redis server and your users won't lose their sessions data, this is a big win over technologies like InProc or Memcached. Using some other technologies when you reboot your server, you loose your cache data. Second, Redis is really really fast, as it uses the NoSQL philosophy, Redis will write data to disk/memory faster than SQL so if you run a load balanced environment and need to cache data, or persist sessions, it's a good choice. Redis still provides better benefits than InProc on a single environment so I can't really see a reason why you wouldn't implement it. I also recommend that you implement StackExchange.Redis as your client, as it's free and provides better async support. In the code sample in my last tutorial, I covered the basics of reading/writing data to Redis. That article only worked with storing strings. In production, I extended this to use Json.Net to serialise/deserialize objects into Redis. That was until I read this article Serialization Performance comparison. Out of the results, Json.NET is performant, but, Protobuf probably is a little more efficient. If you run a high traffic site where you really care about performance, then this little switch in serialisation/deserialisation methods may provide a quick easy win in the optimization fight. Luckily, there are even two Nuget packages that can extend Redis Stack Exchange Redis, so the amount of code you need to implement this is very small. First, in your NuGet Manager, install StackExchange.Redis.Extensions.Core and StackExchange.Redis.Extensions.Protobuf. These packages will provide a Protobuf Serializer and a set of extension methods that will allow us to save C# objects directly in Redis without you having to write any more code: redis_extensions_nuget_installation Now, In your web.config, add this section:

    <section name="redisCacheClient" type="StackExchange.Redis.Extensions.Core.Configuration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.Core" />
and the matching element somewhere within :
    <redisCacheClient allowAdmin="true" ssl="false" connectTimeout="5000" database="0">
            <add host="" cachePort="6379"/>
That should have taken you about 3 minutes and all your configuration work is done (assuming you've already installed Redis). The code to cache your data will look like this:
 public class RedisHelper : ICacheHelper
        private readonly ILog Logger = LogManager.GetLogger(typeof(CacheHelper));

        private static readonly IDatabase Database;

        private static readonly StackExchangeRedisCacheClient CacheClient;

        public static RedisHelper ()
            var serializer = new ProtobufSerializer();
            CacheClient = new StackExchangeRedisCacheClient(serializer);
            Database = CacheClient.Database;

        public static object GetValue(string key)
            return Database.StringGet(key);

        public static void StoreValue(string key, object obj)
            CacheClient.Add(key, obj);
Now you have a simple, performant, cache manager!

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

Back to top