How to Implement Caching in .NET 8 Core, Useful Methods and Best Practices
Cache is the most important thing when we aim to optimize our app for performance and scalability. Using caching in NET Core can decrease the load on the database, make responses faster, and enhance user satisfaction. In this blog, I will demonstrate how caching can be utilized. This blog will walk you through the process of setting up caching in NET Core, including different techniques and recommended approaches.
Why Use Caching?
First, let's see why caching is done.
Increased performance: Caching shortens load-time duration for most-accessed data.
Load on the DB is Less: If you are storing data and you want the same information multiple times then add it to the cache.
Scalability: This makes your application capable of handling more requests because the load on those backend services decreases due to caching.
Types of Caching
In .NET Core, you can write different types of caching
In-Memory Caching: Data is stored in the memory of the web server.
Distributed Caching: The data is stored in an external cache store such as Redis or SQL Server and can be shared with multiple servers.
Response Caching: Cache is used to cache HTTP responses.
We will see how to use each type of caching in .NET Core. Let’s see our first type of in-memory caching.
In-Memory Caching
In-memory storage is appropriate for apps running on just one server or when the storage is not required to be shared among different instances.
Step 1: Set up the Package by running the given command.
Firstly, let's add the Microsoft.Extensions.Caching.Memory
from the library to your project.
https://www.nuget.org/packages/Microsoft.Extensions.Caching.Memory/
dotnet add package Microsoft.Extensions.Caching.Memory
Step 2: Add In-Memory Storage Method in startup file
In your Program.cs
file, configure the in-memory caching services shown below:
Step 3: Use In-Memory Caching
Inject the IMemoryCache
interface into your service or controller. get cache from the TryGetValue() method and set your cache using _cache.Set() Method :
Distributed Caching
Distributed caching works well for cloud-based applications or situations where the cache needs to be used by many servers.
Step 1: Set up the Package by running the given command.
To utilize the Redis distributed cache, you need to set up the MicrosoftExtensions.Caching.StackExchangeRedis
package.
https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
Step 2: Add Distributed Caching in the startup file
In your Program.cs
file, configure the distributed cache:
Step 3: Use Distributed Caching
Inject the IDistributedCache
interface into your service or controller and use cache.GetStringAsync(key) to get your cache and use cache.SetStringAsync() to set your data in cache :
Response Caching
Response caching involves storing HTTP responses in memory or on disk, leading to faster web API performance.
Step 1: Set up the Package by running the given command.
set up the Microsoft AspNetCore.ResponseCaching
bundle.
dotnet add package Microsoft.AspNetCore.ResponseCaching
Step 2: Add Response Caching in startup file
In your Program.cs
file, configure response caching:
Step 3: Use Response Caching
Apply the [ResponseCache]
attribute to your controller actions:
Best Practices
Cache Invalidation: Ensure you have a strategy for cache invalidation to prevent serving stale data.
Cache Size: Monitor and manage the size of your cache to avoid memory issues.
Security: Be cautious with caching sensitive information. Implement proper security measures.
Testing: Test the performance impact of caching and make adjustments as needed.
Conclusion
Using caching can help improve the speed and ability to handle more users of your .NET Core apps. By using in-memory storage, distributed storage, and response storage, you can significantly reduce the time it takes to receive a response and the pressure on your server. Follow best practices to ensure efficient and secure caching in your applications.