Distributed Redis caching in ASP.NET Core

In this article, we will see Distributed caching, Redis cache, and also Redis caching in ASP.NET Core.

Table of Content

  • IDistributedCache interface
  • Framework provided to implement
  • Distributed Redis cache
  • Setting up Redis on Windows 10
  • Redis CLI commands
  • Steps to integrate Redis cache in ASP.NET core
  • Summary

What is distributed caching and its benefit

A distributed cache can improve the performance and scalability of an ASP.NET Core app, especially when the app is hosted by a cloud service or a server farm.

Benefits

  • This is more suitable for microservice architecture
  • In case of loading balancing, this is recommended
  • Multiple Applications / Servers can use one instance of Redis Server to cache data. This reduces the cost of maintenance in the longer run

IDistributedCache interface

  1. GetAsync — Gets the Value from the Cache Server based on the passed key.
  2. SetAsync — Accepts a key and Value and sets it to the Cache server
  3. RefreshAsync — Resets the Sliding Expiration Timer (more about this later in the article) if any.
  4. RemoveAsync — Deletes the cache data based on the key.

Framework provided to implement IDistributedCache

  • Distributed Memory Cache
  • Distributed SQL Server cache
  • Distributed Redis cache
  • Distributed NCache cache

Distributed Redis cache

Redis supports quite lot of data structures like string, hashed, lists, queries and much more and it’s fast key value based database that is written in C. It’s a NoSQL database as well. For this purpose, it is being used in Stackoverflow, Github and so on.

Setting Up Redis

  1. First Approach by redis-server.exe
  • Minimize the Redis-server.exe and open Redis-cli.exe.
  • To test, just enter command ping

2. Second Approach via Chocolatey

Other way to install Redis on a Windows machine is chocolatey. To install chocolatey in a local machine, run the command given below from PowerShell (with administrative mode).

iex ((new-object net.webclient).DownloadString(‘https://chocolatey.org/install.ps1'))

This command downloads the chocalatey installer for Windows and using the command given below, we can install Redis on the local machine.

choco install redis-64

Once Redis Server is installed, use the command given below, where we can start Redis Server

redis-server

Running redis-server

Go to the path and open up Powershell and run the following command

By default, Redis runs on the local 6379 port. To change port number

./redis-server --port {your_port}

Once Redis is running at your defined port, the Redis CLI would no longer work. This is because it tries to connect to 6379 by default. To override this, open up PowerShell again and enter the following command

./redis-cli -p {your_port}

Redis CLI Commands

Setting a key with expiration time (in seconds) and ttl name is for Time left to expire

Steps to integrate Redis cache in ASP.NET core

./redis-server --port 6000

Step 2: Install package that helps you communicate with the Redis serverMicrosoft.Extensions.Caching.StackExchangeRedis

Install-Package Microsoft.Extensions.Caching.StackExchangeRedis

Step 3: configure in application to support Redis cache with specific port.

In startup.cs/ConfigureServices method

Step 4: Implementing caching code snippet in Get call of WeatherForeCastController.

In Line 36, we check if key has a value in Redis, then convert it to a list of Weather list and send back the data and if the value does not exist in Redis, then access the database via efcore (in above code value is hardcoded), get the data and set it to redis.

If key has value then, the data will be stored in Redis as a byte array. We will be converting this array of a string. Which will be converts the string to an object of type, List (Line 38)

DistributedCacheEntryOptions

  1. SetAbsoluteExpiration — Here you can set the expiration time of the cached object.
  2. SetSlidingExpiration — This is similar to Absolute Expiration. It expires as a cached object if it not being requested for a defined amount of time period. Note that Sliding Expiration should always be set lower than the absolute expiration

Summary

Technical lead with over 10+ years of work experience in Analysis, Design and Development of Software Applications Architecture https://github.com/Amitpnk