IDistributedCache Abstraction
Cache data across multiple servers.
Why Distributed Caching?
An in-memory cache lives inside a single process, so it is lost on restart and not shared across servers. A distributed cache lives in an external store (like Redis) that every instance of your app can read and write, making caching consistent across a scaled-out deployment.
The IDistributedCache Interface
ASP.NET Core abstracts distributed caching behind IDistributedCache in Microsoft.Extensions.Caching.Distributed. Your code depends on this interface, not on Redis directly, so you can swap implementations.
public interface IDistributedCache
{
byte[] Get(string key);
Task<byte[]> GetAsync(string key, CancellationToken token = default);
void Set(string key, byte[] value, DistributedCacheEntryOptions options);
void Remove(string key);
void Refresh(string key);
}All lessons in this course
- IDistributedCache Abstraction
- Connecting to Redis
- Cache Patterns and Expiration
- Caching Serialized Objects