drag

Introduction to In-Memory Caching

Caching is a technique of storing constantly used data in an interim file. This will enhance performance and scalability. Once you have the data saved in the temporary storage area, because it is a copy of the data, it is loaded much more quickly. There are a few different kinds of caching in .NET Core supports, but for the sake of this article, we are only going to discuss one, In-Memory cache. The in Memory cache saves data in the coding of the Web Server where a web application is hosted. Single Servers as well as multiple Servers in a Server Farm are able to host applications. However, when the application runs on a Server Farm, it tends to not go as smoothly. Now that we got a general idea about what In Memory cache is, lets jump in.

In-memory caching needs to be enabled in the startup class

To incorporate the In-Memory cache service within ASP.NET Core you must use dependency injection. Unlike other versions, Core does not have a built-in caching option. You must start by creating a Core project in Visual Studio, then enabling the In-Memory cache in the configureservice method. That will be located in the Startup class. You need to use IMemoryCache interface which will allow you to work with the In-Memory cache within ASP.NET Core. Using the AddMemoryCache method, you will be able to enable the IMemoryCache in the Configservices. Your In-Memory will be all set up when you are done.

In-memory caching uses dependency injection to inject.

Now that you have your In-Memory caching set up, you will start up the HomeController and modify it, adding your In-Memory caching to the project. In the constructor, the variable gets designed. The constructor will collect the cache parameter through direct injection. The cache will then be saved in the local variable for future use.

Use the set() method to save an item in the cache.

Once you have the IMemoryCache object, you will now be able to read and write an item to it. Adding an entry is pretty much effortless. The first parameter of this method is a key by which the item will be recognized. The second parameter is equal to of said key. You can store three different types of keys and values; string, primitive, and custom types.

You can use the get() method to open up an item from the cache.

If you have an item in the cache and would like to open it on a different page you would use the Get() method. The Get() method will itemize the type of item as well as its key. If the item exists, it will be assigned to a timestamp string variable. It then goes through the Show view which plainly outputs the timestamp value. You can test what you have written thus far by running the application. To begin, Navigate to Home/Index so the timestamp key is assigned.

You can use target() to check whether a key exists in a cache.

You will notice that every time you travel to the Home/Index a timestamp is appointed to the item. This is because you didn’t put a check to appoint the value. You only checked if the item existed. When using the Get() method and the item can’t be found, IsNullOrEmpty() will return true. Only the Set() will get called to add that entry. Although, when you use the TryGet() method this task goes much more smoothly. This method returns a specific value showing whether the item was there. The real item can now be pulled out using an output parameter. If this method comes back untrue, Set() is used to put that Item in the cache.

You can use getOrCreate() to create an item if it is nonexistent.

There will be times when you want to grab an item from the cache, but it doesn’t exist. If this is the case, you will use this method to ‘get or create’ the item. This method makes sure whether the timestamp key exists or not. If the key is present, the value will be appointed to the variable. If it is nonexistent, a new entry will be created and added to the cache. This will be added based on the code written in the second parameter. When you want to test this code, you will run /Home/Show immediately. You do not want to navigate to /Home/Index. A timestamp value output would still be visible. This is because GetOrCreate() adds it if it doesn’t already exist.

You’re able to assign absolute and sliding expiration to a entries that are in the cache. Absolute Expiration means that the cached item could be eliminated at an exact date and time set. Sliding Expiration means that a cached item will be discarded. This item remains idle for a set amount of time. Use MemoryCacheEntryOption object to set either of these codes. The Set() method could be used to create an entry to the cache once the Absolute or Sliding expiration is set up. At this time, the MemoryCacheEntryOption object will be proceeded as the third parameter for the Set() method.

Conclusion

The In-Memory Cache will be used when ASP.NET is running on a single server. This is store data on Server. In the long run In-Memory Cache will improve application performance tremendously. You are able to use the Set() method to add an entry to the cache. The Get() or GetOrCreate() methods are used to retrieve an item that is in the cache. If an item the exist the GetOrCreate() method will create the entry. Absolute or Sliding Expiration will be set when you want to remove an entry at a certain date or time. Sliding Expiration will set an item to idle until the date. Absolute Expiration will remove an entry on an exact date or time that was set. These six things will help get you started using ASP.NET Core fluently and efficiently. Don’t stop coding!

Subscribe to Saffron Tech

Explore your marketing zen with our newsletter! Subscribe now.