Know About In-Memory Caching (ASP.NET Core)

Know About In-Memory Caching (ASP.NET Core)

Web Development

Some of the things to grasp concerning In-Memory Caching in ASP.NET Core

The main purpose of caching mechanism is to boost the performance of an application. ASP.NET web forms technology, as well as ASP.NET MVC technology, could use server aspect information caching and was offered as an integral feature of the .NET framework. In ASP.NET Core we do not have cache object, you’ll implement in-memory caching quite simply.

  • Enable caching within the Startup.cs

ASP.NET Core doesn’t have the integral Cache object that you just will directly use within controllers as like ASP.NET internet forms and ASP.NET MVC. In-memory caching works through dependency injection and thus the primary step is to register the in-memory caching service within the Startup.cs.

So, open the Startup.cs class file and modify the ConfigureServices() method by adding AddMemoryCache().

To add in-memory caching to your application you would like to call AddMemoryCache() function on the services assortment. By this fashion, the default implementation of in-memory cache is injected to the controllers.

  • Uses of dependency injection to inject the cache object

For that go to Controller and declares a non-public variable of IMemoryCache. This variable gets appointed within the constructor. The constructor will receive the cache parameter through dependency injection then this cache object is kept within the native variable for later use.

  • Set() method to save an item in the cache

As and when you got IMemoryCache object, you can get and set values into the cache object.

Let’s sets a value to the cache in action method by using the cache.Set<string>(“username”, “admin”); the primary parameter of Set() method is the key name and that key we can use it for fetching the value from cache. The second parameter is that the value of the key.

  • Get() method to fetch data that stored in the cache

As and when you stored value the cache, you can be fetched it anywhere in the application by using Get() method and can display in view or do any kind of operation on it. For example. string username = cache.Get<string>(“username”);

  • Use of GetOrCreate() method

By using the cache.GetOrCreate() you can check whether the passed key exists or not and if it does not exist then will create new cache key value.

Ex. cache.GetOrCreate()<string>(“username”, entry => { return “admin”; });

  • Use of TryGetValue() to check whether the key exists or not

By using the cache.TryGetValue() we can check whether the key exists or not. Here the main difference between cache.GetOrCreate() and cache.TryGetValue() method is that TryGet() method will only check whether the key exists or not whereas GetOrCreate() method will cache whether the key exists or not and if it doesn’t exist then can create a new one.

Syntax for TryGet() method is as below:

cache.TryGetValue()<string>(“username”, out string username);

  • Set priority for cache

Similarly, as you can set the lapse arrangements of a reserved thing you can assign a priority value to a cached item. On the off chance that server encounters lack of memory in view of this need the things will be evacuated to recover the memory. To set the need you utilize MemoryCacheEntryOptions once more.

Apart from all above you can set a dependency between multiple cached items, set absolute and sliding expiration and also wire a callback when an item is removed from the cache.

Posted on August 14, 2018 by Keyur Patel
Keyur Patel