Blog Highlights

Explore my latest thoughts, insights, and innovations in technology, AI, development, and security. Dive into comprehensive guides and analyses.

Back to all posts

Memory Caching in .NET CORE

July 31, 2023 by Rakesh Udutha Read Time: 2 min read.NET Core

.NET Core provides a rich set of features for implementing caching in your applications. Here are the types of memory caching you can use:

  1. 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.
  2. 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 the Microsoft.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. The IDistributedCache API stores data in byte[] format, unlike the in-memory cache which can store arbitrary objects.
  3. 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.
  4. 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.