Posts

Showing posts with the label Cache

Simple Memory Cache in ASP.NET Core

Image
In this blog post, we'll explore how to use in-memory caching in ASP.NET Core. Caching can highly enhance the performance of your web application by reducing the number of times data needs to be retrieved or computed. We'll use a simple example to illustrate how you can implement in-memory caching in your ASP.NET Core application. Setting Up the Project First, let's set up our ASP.NET Core project. We'll start with the HomeController class, which will handle our web requests. Here's the code for our HomeController: public class HomeController : Controller {     private readonly ILogger<HomeController> _logger;     private readonly IMemoryCache _memoryCache;     public HomeController(ILogger<HomeController> logger, IMemoryCache memoryCache)     {         _logger = logger;         _memoryCache = memoryCache;     }     public IActionResult Index()   ...

In-Memory Cache in ASP.NET Core

Image
You can improve the performance of your application by using cache. Instead of generating the content, you can retrieve the content from the cache. You can use cache for rarely changing content. Instead of creating content, the cache will hold the content, so it will be available quickly. ASP.NET Core support IMemoryCache, its cache storage in the memory of the webserver. Let's see how this works. The following is a HomeController code. Here the IMemoryCache interface is implemented using dependency injection. You can see in the controller constructor IMemoryCache object has been passed. In coding, the TryGetValue method checks if there is any cached content for the keyword ‘CacheDateKey’, which will provide content if there is any cached content in it. If there is no content in the cache. It will set the cache, i.e. the time for the cache. Also, the SetSlidingExpiration option is set in the example. This indicates how long the cache should be idle. If any user accesses ...

Response Caching Middleware Configuration in ASP.NET Core

Image
In this article we are going to see how to configure response caching middleware in ASP.NET Core. The response cache helps to reduce the number of requests for the web server. This is because, when using the response cache on the ASP.NET Core, it receives data from the cache instead of the web server. The response cache attribute [response cache] determines when the response cache should be saved and how long the response cache should be stored. When you first send a request from the client browser, it sends the request to the proxy server, and then it sends the request to the web server. The web server responds to the proxy server with a cache header. The proxy server sends the request to the client browser. For the next subsequent request from the client browser the proxy server will provide the response as long as the cache lifetime. Let us see a small example of the response cache middleware configuration. You must first add response cache middleware to the service collection to u...