In-memory and Distributed Caching in ASP.NET Core
Caching is an essential technique to improve the performance of web applications. ASP.NET Core provides both in-memory and distributed caching options to cache frequently accessed data and reduce the number of database requests.
In this page, we will discuss how to use in-memory and distributed caching in ASP.NET Core applications.
What is Caching?
Caching is the process of storing data in temporary storage, known as a cache, to reduce the number of roundtrips to the source. A cache stores data that is frequently used so that it can be retrieved quickly.
In-memory Caching
In-memory caching is a process of storing frequently accessed data in memory instead of the database. ASP.NET Core provides built-in support for in-memory caching that can be easily used in applications.
Syntax
To use in-memory caching in ASP.NET Core, you need to add the Microsoft.Extensions.Caching.Memory
NuGet package to your project. Then, you can use the IMemoryCache
interface to store and retrieve data from the cache.
public interface IMemoryCache
{
bool TryGetValue(object key, out object value);
ICacheEntry CreateEntry(object key);
void Remove(object key);
}
Example
Here's an example of using the in-memory cache in ASP.NET Core:
public class HomeController : Controller
{
private readonly IMemoryCache _cache;
public HomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
public IActionResult Index()
{
const string cacheKey = "message";
if(!_cache.TryGetValue(cacheKey, out string message))
{
message = "Hello, ASP.NET Core Caching!";
var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(10));
_cache.Set(cacheKey, message, cacheEntryOptions);
}
return Content(message);
}
}
In the above example, we are creating an instance of IMemoryCache
and using it to get a cached value using a specific key. If the value is not in the cache, we create a new entry with a sliding expiration time, and then set the value for the key.
Output
After running the above code, you can see the cached output by visiting the URL of your application (e.g., https://localhost:5001/
).
Distributed Caching
Distributed caching is a process of storing frequently accessed data in a distributed cache, which can be shared across multiple servers. ASP.NET Core provides built-in support for distributed caching that can be used with various cache providers.
Example
Here's an example of using distributed caching in ASP.NET Core with Redis:
public void ConfigureServices(IServiceCollection services)
{
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost:6379";
options.InstanceName = "SampleInstance";
});
}
public class HomeController : Controller
{
private readonly IDistributedCache _cache;
public HomeController(IDistributedCache cache)
{
_cache = cache;
}
public IActionResult Index()
{
const string cacheKey = "message";
if(!_cache.TryGetValue(cacheKey, out byte[] message))
{
message = Encoding.UTF8.GetBytes("Hello, ASP.NET Core Caching!");
var cacheEntryOptions = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(10));
_cache.Set(cacheKey, message, cacheEntryOptions);
}
return Content(Encoding.UTF8.GetString(message));
}
}
In the above example, we are configuring the Redis cache provider and then using it to get and set cached values using a specific key.
Output
After running the above code, you can see the cached output by visiting the URL of your application (e.g., https://localhost:5001/
).
Use
In-memory and distributed caching can be used to improve the performance of ASP.NET Core applications by reducing the number of database requests. You can cache frequently accessed data and retrieve it quickly from the cache.
Important Points
- Caching is important to improve the performance of web applications.
- ASP.NET Core provides built-in support for in-memory and distributed caching.
- In-memory caching stores data in memory instead of the database.
- Distributed caching stores data in a distributed cache, which can be shared across multiple servers.
Summary
In this page, we discussed how to use in-memory and distributed caching in ASP.NET Core. We covered the syntax, example, output, use, important points, and summary of in-memory and distributed caching. By using caching in your ASP.NET Core application, you can reduce the number of database requests and improve the performance of your application.