Creating an in-memory cache for .NET 6 Web API
“We need to improve the Web API performance!” How many times have we as developers heard this from leads and users’ feedback? A lot! Right?
Then, we start checking it out database indexes, communication between applications, increasing infrastructures resources, refactoring codes, and so on.
This article presents how to create an in-memory cache (local cache) for our .NET Web API. You will see how simple is to implement this approach to improve the application performance.
Before presenting this approach, keep in mind that the local cache consumes resources from its host, for example, memory. Then, as does not exists free lunch be sure that this can be applied to your reality.
1. Register the Cache Service
Adding a local cache in .NET 6 is simple. For the first step, all we need is to register the cache service on application startup. Check the code below:
This code above is part of the Program class from web api. Check line 9 from this code and you will realize that the cache service was registered through the AddMemoryCache method.
That’s it! Now we can handle data in the web api local cache.
2. Get or Add new data to the Cache Service
For this sample, I created a new controller to test the cache service. This controller has a GET method that will simulate a long-running process when its result is not in the local cache.
After registering the cache service in the Web API, we need to initiate an instance from the IMemoryCache interface in the controller to handle the data in the local cache.
See the code below:
I added a GET method (line 18) that returns a random number given a specific key (the cacheKey parameter).
The first step of this method is to verify if there is any data in the local cache associated with the provided key (line 20). In case the data has been found in the local cache, this will be returned to the consumer. Otherwise, the GET method will call the LongRunningProcess method to simulate a slow execution (line 25).
The LongRunningProcess method simulates slow processing that returns a random number. Inside this method, I added a delay of 1 second for each call (line 39), then I returns a random number.
Backing to the GET method, when the LongRunningProcess returns a number, this will be stored in the local cache, but first, a MemoryCacheEntryOptions to define how this data will be persisted in the local cache (line 27):
- AbsoluteExpirationRelativeToNow: gets or sets an absolute expiration time.
- SlidingExpiration: gets or sets how long a cache entry can be inactive before it will be removed.
The last step is to add the data to the local cache and return this data to the consumer (line 33). Note that before returning an OK message to the consumer, the data is stored in the local cache through IMemoryCache.Set method.
Through this method, we provide the data that will be stored in the local cache and for how long this data will keep there, following the rules defined in the MemoryCacheEntryOptions object.
. . .
I hope that this can be helpful for you! HAPPY CODING!!