filmov
tv
Distributed Caching with Redis and Response Caching in ASP.NET Core | HOW TO - Code Samples
Показать описание
#coding #codingbootcamp #softwaredeveloper #CodeYourFuture
Caching allows applications to store regularly accessed data in memory or a data store, enhancing application performance by retrieving the cached data instead of fetching it from the original source, such as the database.
In ASP.NET Core, there are different ways to perform caching which are:
- Memory cache (IMemoryCache)
- Response caching
- Distributed Caching (IDistributedCache)
Memory Cache
The memory cache is one of the simplest caching in ASP.NET Core that provides an interface “IMemoryCache” that caches data in the server memory and it also works with sticky session that ensures that all request goes to a single server. For apps running on multiple servers, the IMemoryCache should not be used, instead use a distributed cache that supports non-sticky session. This video is not about memory caching so let’s move to the next.
Response Caching
Response caching (as the name implies) ensures that responses are cached, therefore reducing the client request that hits the server. Response caching works with HTTP headers and they are very useful. Imagine every time you send a request to an API endpoint, it takes about 5 mins to get a response from the server and the data that is returned is like a static data or data that doesn't change frequently. Response caching helps to cache the response by reducing the work the server has to perform to generate a response so that subsequent request would in turn be given a cached response.
The response cache can also vary based on some information like “User-Agent” and it also has a duration or max-age. To use the Response caching in ASP.NET Core, we have to specify the ResponseCache Attributes in the Action method and its Parameters:
- Duration: The durations Gets or Sets the max-age in seconds in the Cache-Control header
- Location: Specifies where the data from a url should be cached. The ResponseCacheLocation.Any ensures that it is cached from both client and proxy
- VaryByHeader: The VaryByHeader is optional but it ensures that the response cache can vary based on a given header like “Auhorization”. The code below specify the VaryByHeader parameter to “Authorization” therefore the cache response would be varied by “Authorization”. For example, if “User A” sends a fresh request, the server would return a response that would be cached subsequently. If “User A” sends another request with a different authorization header, that response would not be cached until subsequent requests because the authorization header has changed.
Distributed Caching with Redis
Distributed caching supports a non-sticky session, that is, it can run on multiple servers and can survive server restarts. A distributed cache can improve the performance of an ASP.NET Core application. The interface that supports the use of distributed caching is the “IDistributedCache” interface and supports third party providers such as: NCache and Redis.
In production environment Distributed Redis cache is the preferred approach as it reduces the loads on the database and speeds up the retrieval of data.
The IDistributedCache Interface
The IDistributedCache interface is used to store and retrieve cache data from the cache. It provides several methods that is used to cache data which are:
- SetAsync: Adds an item to the cache using a specified string key
- GetAsync: Retrieves an item from the cache using a specified string key
- GetStringAsync: Retrieves the string values from a specified key
Others are RefreshAsync that refreshes the cache and RemoveAsync that removes a cache using the specified key.
Conclusions
Caching brings good performance to ASP.NET Core applications. Storing frequently accessed data in a cache reduces the load on the database. Redis is fast and it has better performance compared to caching with SQL Server. If you also want to view and manage the data that was cached, you can download RedisInsight that provides a GUI for doing such.
Lots of Great How Tos and Code Samples! Make sure to like and subscribe!