In this tutorial, you will learn how to quickly and easily set up a Redis cache to work within an Episerver CMS-powered project. In any high traffic websites, how you deal with caching is critical. In most website scenarios, you might consider using the out-of-the-box ASP.NET Framework output cache. For the majority of projects, the output cache is more than adequate. On other more complex projects, you might bump into some limitations with output cache. You may find it is not powerful enough for you. In these situations, what do you do? If you find yourself in this situation, this is where Redis cache can help. Redis will give you more control. It persists session data on server reset. Via the desktop app and its query language, it's easier to query the contents of the cache for debugging purposes. If you want to learn more about Redis and how to implement it within your Episerver CMS-powered project, read on 🔥🔥🔥
Redis is actually surprisingly easy to set up and get working within a .NET project. I personally think Redis is a much better solution than MemoryCache in most situations, why?:
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 like async support, event notifications, various data structure support, advanced eviction policies, and blocking queues
Simply put, Redis is 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 Redis will give you a lot more customization ability than the output cache.
How To Install A Redis Server
To get started with Redis you'll need to install it somewhere. You can opt to host it on your own server, or, you can run a SaaS version in a cloud provider like Azure. In this tutorial, I will show you how to install Redis locally on your Windows PC.
You can download Redis for Windows here. You will also want to download the Redis Desktop client from here. The desktop app will allow you to easily check that data is being added to the Redis server. Think of Redis Desktop Manager as similar to Sql Server Manager Server for MSSQL.
How To Get .NET Code To Talk To Redis
After you have a Redis server set-up, you'll need to writer some code to talk to it. Asides from a valid connection string to your Redis database, you will need to write some code that gets and posts your cache data to the server. Instead of having the write all this communication code yourself, you can use one of two third-party Nuget packages instead, these are:
ServiceStack.Redis: IMHO, has better object caching support, but, requires a license
StackExchange.Redis: Created and used by [Stackoverflow(https://stackoverflow.com/)] This package is open source, free and has async support. This is by far the most popular option
How To Use Redis In My Code?
Moving forward, I am assuming you are using StackExchange.Redis. The next task is to use it to create a connection to the Redis server, this can be done with the following code:
Define a connection string to the Redis server. You need to specify the host and port. If you are ensure what your Redis server conenction details are, use the desktop app to validate the connection string to the server before debugging your code 😕. After a connection to the server has been established, an object of type
IDatabase is returned. In a Redis server you will have access to more than one database. This can be handy to partion your cache data to suit your needs. In the
GetDatabase() call you need to specify which database you want to use. By default, the code will talk to database 0. By default, 16 databases will get created when you install a new Redis server. Swap the ID to use the database your heart desires ❤️
Stackoverflow.Redis, if you want to use async calls, you need to create an async
ConnectionMultiplexer connection. To do this wrap the
SetRedisConnection method defined in the snippet above, with the
ConnectAsync() method like this:
After you have a valid connection to Redis, the CRUD operations you can perform will likely feel very familiar to you:
IDatabase you can cache objects and perform tasks like
Delete. I won't go over this code as it's pretty self-explanatory.
Redis is a very handy and powerful cache provider. If you have more complex caching needs, like HTML fragment caching, I strongly recommend you research the benefit of Redis. Happy Coding 🤘