.NET Core provides a rich set of features for implementing caching in your applications. Here are the types of memory caching you can use:
- In-Memory Cache: The
Microsoft.Extensions.Caching.Memory
namespace provides APIs to use an in-memory cache. This is a simple, in-process memory cache that doesn’t support a distributed scenario where you have multiple servers or instances of your application. Data cached in-memory can be of any object type and you can specify options for each item such as priority, expiration tokens, size, and callbacks. - Distributed Cache: For distributed caching scenarios where you want to scale out your application over multiple servers or instances, .NET Core offers the
IDistributedCache
interface in theMicrosoft.Extensions.Caching.Distributed
namespace. It provides a consistent, abstract API for using distributed cache technologies like Redis or NCache, so you can easily switch the implementation without changing application code. TheIDistributedCache
API stores data in byte[] format, unlike the in-memory cache which can store arbitrary objects. - Response Caching: .NET Core includes middleware for caching HTTP responses on the server. Response caching adds cache-related headers to responses and it can store responses either in-memory on the server or on the client. This is great for caching static files or pages that don’t change often.
- Session State: Although it’s not a general-purpose caching technique, .NET Core allows session state data (small amounts of user-specific data) to be cached on the server between HTTP requests using either in-memory or distributed caching.