ASP.NET cache and session with Redis and CacheManager

How to use Redis as a session provider and a cache provider for ASP.Net solution. To make solution flexible for cache backend we use CacheManager.

Source code is available here

Control your Redis

I use Redis Desktop Manager. It is a free and simple application running on Windows, Linux, MacOS. Redis Desktop Manager provides easy-to-use GUI to access your Redis DB and perform some basic operations: view keys as a tree, CRUD keys, execute commands via shell.

Session in Redis

Useful link How to use configuration parameters of Session State Provider and Output Cache Provider

1. Install packages

2. Write a serializer class

This step is only needed if we are going to put NOT serializable objects to the session. If we have no control over it in the project then better to add a serializer. It’s just a tiny class and a one configuration option. This class must extend Microsoft.Web.Redis.ISerializer interface.

3. Add a session provider definition in web.config

Where redisSerializerType is the created serializer class option.

Now your application is ready to place session data in Redis.

Cache in Redis

To make solution flexible for cache backend we use CacheManager. CacheManager supports various cache providers and it is easy to switch between them. For example it is possible to use System.Web.Caching.Cache while developing and Redis in production by only changing configuration.

My blogpost about common cache interface and implementations

1. Install packages

2. CacheManager configuration for Redis

Useful link all valid configurations options in *.config file.

3. Serialization

Serialization is needed for non serializable objects for distributed (not in-process) cache systems which is Redis.
Useful link CacheManager serialization documentation.
To enable caching for non serializable objects we need to use CacheManager.Serialization.Json.
Install the package and add the following line to config for this for the Redis cache manager:
serializerType="CacheManager.Serialization.Json.JsonCacheSerializer, CacheManager.Serialization.Json

Now our ASP.Net solution is ready to use Redis through CacheManger as a caching backend.
Use it: