How to implement a distributed cache in ASP.Net Core

ASP.Net Core provides support for various types of caching. In addition to in-memory caching and response caching, it provides built-in support for distributed caching. In one of my previous articles here, I discussed in-memory caching in ASP.Net Core. In this article we will examine how we can work with distributed caching in ASP.Net Core. We will discuss both supported implementations, i.e., using Redis as well as SQL Server as the cache stores.
What is a distributed cache
Distributed caches are used to improve the performance and scalability of applications. Typically, a distributed cache is shared by multiple application servers. In a distributed cache, the cached data doesn’t reside in the memory of an individual web server. Rather, this data is stored centrally so that it is available to all of the application’s servers. If any of the servers go down or stop responding, other servers will still be able to retrieve the cached data. Another advantage of a distributed cache is that the cached data survives server restarts. You can add or remove a server without impacting the cache or the cached data.
To take advantage of a distributed cache when working on ASP.NET Core applications, we will use the IDistributedCache interface. We’ll examine how the IDistributedCache interface differs from the IMemoryCache interface in the section that follows.