AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Asp.net core private cache11/1/2022 ![]() ![]() Our applications often calls same method, again and again and fetch data from database, but sometimes, data doesn't get's changed or updated in database, in that case, we can use caching to reduce database calls and get's same data directly from memory-cache. NET Core console application Why do we need caching? Sample Console application using Memor圜ache with all operations. ASP.NET CORE PRIVATE CACHE CODEVisit the Git Repository and feel free to take the code for a spin. That’s it! Request Caching is now injectable in any place you need it. Private static string BuildCacheKey(int userId) Return _cache.RetrieveOrAdd(BuildCacheKey, () => ) Var buildCacheKey = UserService.BuildCacheKey(userId) Public UserService(IRequestCache cache, IUserRepository userRepository) Private readonly IUserRepository _userRepository services.AddScoped() Usage public class UserService Using ASP.Net Core’s DI framework we’ll wire it up as Scoped. Public TValue RetrieveOrAdd(string key, Func add) Public void Add(string key, TValue value) } Implementation public class RequestCache : IRequestCache When the key does not exist the default value for the type is returned. TValue RetrieveOrAdd(string key, Func add) ![]() / adding the value to cache and returning the added value. / Retrieve the value by key, if the key is not in the cache then the add func is called / Remove the key from the request cache If the key already exists, the value is overwritten. I’ve created an interface modeled after the IMemor圜ache interface to keep things consistent. Singleton is for the life of the application, Scoped is for the life of the request and Transient is a new instance with each request. The built-in DI framework has three lifetimes for dependencies: Singleton, Scoped, and Transient. Luckily for us, ASP.Net Core gives us the tools to create an injectable Request Caching implementation by using the built-in dependency injection (DI) framework. ![]() As mentioned earlier, HttpContext.Items is an option, but it’s not an elegant solution. Out of the box, Asp.Net Core doesn’t have injectable caching. And secondly, expiration isn’t a concern because the data dies when the request ends. In most scenarios, a request executes in less than a second and which typically isn’t long enough for data to become stale. ![]() Request Scoped caching has a few benefits: First, it eliminates the concern of stale data. In dot-net, we’ve had this ability in some capacity with the HttpContext.Items collection, however, HttpContext is not known for its injectability. What is Request caching? Request caching is a mechanism to cache data for the life of a web request. In this article, we look at Request Scoped caching and how it can benefit us. Microsoft provides IMemor圜ache for time-based caching, however sometimes time-based caching isn’t what you need. After some research, the culprit is, unnecessarily retrieving the same data, and a light goes off, and you think: “I need some caching.”Ĭaching is an invaluable pattern for eliminating redundant calls to a database or a third party API. At some point in an application’s development, usually, fairly early on, you realize the application is slow. ![]()
0 Comments
Read More
Leave a Reply. |