Distributed Redis caching in ASP.NET Core

Table of Content

  • What is distributed caching and its benefit
  • 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

Distributed caching is when you want to handle caching outside of your application. This is also can be shared by one or more application/servers. Distributed cache is application-specific i.e., multiple cache providers that support distributed caches. To implement distributed cache, we can use Redis and NCache. We will see about Redis cache in detail.

Benefits

  • Data is consistent throughout multiple servers.
  • 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

IDistributedCache Interface provides you with the following methods to perform actions on the actual cache

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

Framework provided to implement IDistributedCache

Register an implementation of IDistributedCache in Startup.ConfigureServices. Framework-provided implementations described in this topic include

  • Distributed SQL Server cache
  • Distributed Redis cache
  • Distributed NCache cache

Distributed Redis cache

Redis is an open source in-memory data store, which is often used as a distributed cache. You can configure an Azure Redis Cache for an Azure-hosted ASP.NET Core app, and use an Azure Redis Cache for local development.

Setting Up Redis

We will see how we can setup Redis in local machine via two approach (Currently I am using Window 10)

  • To test, just enter command ping
iex ((new-object net.webclient).DownloadString(‘https://chocolatey.org/install.ps1'))
choco install redis-64
redis-server

Running redis-server

I will use first approach to run server (ie., via redis-server.exe)

./redis-server --port {your_port}
./redis-cli -p {your_port}

Redis CLI Commands

Below are some command which is run on power shell

Steps to integrate Redis cache in ASP.NET core

Step 1: Make sure redis server is running

./redis-server --port 6000
Install-Package Microsoft.Extensions.Caching.StackExchangeRedis
  1. 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

In this detailed article, we have seen Distributed Caching, Redis. You can find the completed source code here. I hope you learned something new and detailed in this article. Thanks and Happy Coding!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store