Introdution
Caching is a technique of storing frequently used data in a temporary storage area. Caching improves performance and scalability. When we implement caching on data, the copy of data is stored in the temporary storage area. Hence, when the same data is requested next time, it is picked up from temporary storage area, loading it much faster than from the original source.
ASP.NET Core supports different kinds of caching such as In-Memory Cache, Distributed Cache and Response Cache. This article introduces the In-Memory Cache.
The In-Memory Cache stores data in the memory of Web Server where a web application is hosted. An application can be hosted on single Server or multiple Servers in a Server Farm. When an application is hosted on a Server, the In-Memory Cache works perfectly but when an application runs on the Server farm, then we should ensure that the sessions are sticky.
- Server Farm
It is the group of networked Servers which are interconnected and connected to a Central Management Server at a physical location. It provides the combined computing power of many Servers by simultaneously executing one or more applications or services. The Central Server has load-balancing software which manages the overall operations of these Servers, such as assigning processes, resource balancing, scheduling, security, updates and more. Hence, a web application can be hosted on multiple Servers and managed by the Server farm.
- Sticky Session
All application requests are redirected to the same physical Web Server for a particular client. Suppose, we have a web application which runs on 2 web Servers. Each Server has a separate physical box. These servers are interconnected by a load balancer. The load balancer decides which actual web Server should each request to go.
Ex- We have two web Servers - Server 1 and Server 2 and we have requested to access page1 and page2. Now, page1 is served by Server 1 and page 2 is served by Server 2. Thus, both the Servers create a separate session for both requests with the same client. There's no direct way of knowing what is there in the session object of the other.
Now, here comes the role of sticky session. If the load balancer is instructed to use sticky sessions, all of the application interactions will happen on the same physical Server, even though other Servers are present. Thus, application session object will be the same throughout our entire interaction with this web application.
Configure In Memory Cache
First, create an empty ASP.NET Core project. As this project doesn’t hold default implementation of ASP.NET Core Cache, we will build an application step by step with ASP.NET Core Cache. The project.json file doesn't have any cache NuGet packages.
The InMemory cache which uses ImemoryCache interface requires NuGet package "Microsoft.Extensions.Caching.Memory". We have installed it in the application. The following code snippet is used for project.json file.
- {
- "dependencies": {
- "Microsoft.NETCore.App": {
- "version": "1.1.0",
- "type": "platform"
- },
- "Microsoft.AspNetCore.Diagnostics": "1.1.0",
- "Microsoft.AspNetCore.Mvc": "1.1.1",
- "Microsoft.AspNetCore.Razor.Tools": {
- "version": "1.0.0-preview2-final",
- "type": "build"
- },
- "Microsoft.AspNetCore.Routing": "1.1.0",
- "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
- "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
- "Microsoft.AspNetCore.StaticFiles": "1.1.0",
- "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
- "Microsoft.Extensions.Configuration.Json": "1.1.0",
- "Microsoft.Extensions.Logging": "1.1.0",
- "Microsoft.Extensions.Logging.Console": "1.1.0",
- "Microsoft.Extensions.Logging.Debug": "1.1.0",
- "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
- "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
- "Microsoft.Extensions.Caching.Memory": "1.1.0",
- "BundlerMinifier.Core": "2.3.327"
- },
-
- "tools": {
- "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
- "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
- },
-
- "frameworks": {
- "netcoreapp1.0": {
- "imports": [
- "dotnet5.6",
- "portable-net45+win8"
- ]
- }
- },
-
- "buildOptions": {
- "emitEntryPoint": true,
- "preserveCompilationContext": true
- },
-
- "runtimeOptions": {
- "configProperties": {
- "System.GC.Server": true
- }
- },
-
- "publishOptions": {
- "include": [
- "wwwroot",
- "**/*.cshtml",
- "appsettings.json",
- "web.config"
- ]
- },
-
- "scripts": {
- "prepublish": [ "bower install", "dotnet bundle" ],
- "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
- }
- }
The In-Memory caching is a service called by dependency injection in the application, so we register it in the ConfigureServices method of Startup class, as per the following code snippet.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- services.AddMemoryCache();
- }
Implement In-Memory Cache
To implement the In-Memory cache, we create a Controller named HomeController. This Controller holds the implementation of the In-Memory cache.
Now, we create ImemoryCache interface instance in the HomeController using constructor dependency injection. The following code snippet can be used for the same.
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Caching.Memory;
- using System;
-
- namespace MemoryCacheApplication.Controllers
- {
- public class HomeController : Controller
- {
- private readonly IMemoryCache memoryCache;
-
- public HomeController(IMemoryCache memoryCache)
- {
- this.memoryCache = memoryCache;
- }
- }
- }
Now, we need to create an action method named Index. This action method sets data in the cache. The following code snippet is for the same.
- public IActionResult Index()
- {
- DateTime currentTime;
- bool isExist = memoryCache.TryGetValue("CacheTime", out currentTime);
- if (!isExist)
- {
- currentTime = DateTime.Now;
- var cacheEntryOptions = new MemoryCacheEntryOptions()
- .SetSlidingExpiration(TimeSpan.FromSeconds(30));
-
- memoryCache.Set("CacheTime", currentTime, cacheEntryOptions);
- }
- return View(currentTime);
- }
The above code snippet has consumed some methods of InMemory cache service for reading and writing the data in the cache.
- TryGetValue
This method reads value and assigns it in out parameter. It returns true if the value exists otherwise returns false.
- Set
This method writes data in the cache. This method has three options - Cache Key name, data which is to be cached, and expiration option respectively.
- SetSlidingExpiration
It sets cache expiration from absolute to sliding. When we make another request with expiration time, then it will be renewed with each response. Now, we create a View named "Index" under Views/Home folder for the Index action method as per the following code snippet.
- @model DateTime?
- <div class="row">
- <div class="col-md-3">
- <h3> Current Time: @DateTime.Now.ToString()</h3>
- </div>
- <div class="col-md-3">
- <h3>Cached Time: @Model.Value.ToString()</h3>
- </div>
- </div>
The above code shows current time and cached time. Now, we will run the application and make another request within 30 seconds with the same client. The result shows as per the below figure.
Figure 1: Data from cache
The result shows that when we request within expiration time, then it returns cache data.
Read and Write In-Memory Cache
The In-Memory Cache provides another option to read and write data in the cache. There are two methods - Get and GetOrCreate.
- Get
This method is used to get cached data. It takes cache key as a parameter and returns data stored based on this key.
- GetOrCreate
If the data exists for a cache key, then this method reads that data and returns. If cache data for that cache key doesn’t exist, then it writes data in the cache.
Now, we will create another example in the same application while using these two methods. We have created View Model named CacheViewModel as per the following code snippet.
- using System;
- namespace MemoryCacheApplication.Models
- {
- public class CacheViewModel
- {
- public DateTime? FirstTime { get; set; }
- public DateTime SecondTime { get; set; }
- }
- }
Now, create Index action method in HomeController. This action method implements both cache In-Memory methods. See the code snippet.
- public IActionResult Index()
- {
- CacheViewModel model = new CacheViewModel
- {
- FirstTime = memoryCache.Get<DateTime?>("CacheTime"),
- SecondTime = memoryCache.GetOrCreate("CacheTime", entry =>
- {
- entry.SlidingExpiration = TimeSpan.FromSeconds(45);
- return DateTime.Now;
- })
- };
- return View(model);
- }
Now, we create a View named "Index" under Views/Home folder for the Index action method, as per the following code snippet.
- @model MemoryCacheApplication.Models.CacheViewModel
- <div class="row">
- <div class="col-md-3">
- <h3> Current Time: @DateTime.Now.ToString()</h3>
- </div>
- <div class="col-md-3">
- <h3> First Time: @(Model.FirstTime.HasValue?Model.FirstTime.ToString():"Cache is not available")</h3>
- </div>
- <div class="col-md-3">
- <h3>Second Time: @Model.SecondTime.ToString()</h3>
- </div>
- </div>
The above code shows the current time and cached time.
Now, we will run the application and make another request with in 45 seconds with the same client. The result shows as per the below figure for both methods.
Figure 2: Show Caching Data
Remove Data from In-Memory Cache
The In-Memory cache can be evicted either automatically or manually. There are some cases when cache evicts automatically. These are
- Memory Pressure
The IMemoryCache cache will evict cache entries under memory pressure unless the cache priority is set to CacheItemPriority.NeverRemove.
- Sliding Expiration
We set the value in timespan for how long a cache entry can be inactive before removing it from the cache. If a request doesn't make for that period, then it removes automatically.
There is Remove method of IMemoryCache interface which removes the cache from the memory. We create Index action method which writes data in the cache if cache does not exist already. The following code snippet is used for the same.
- [HttpGet]
- public IActionResult Index()
- {
- DateTime currentTime = memoryCache.GetOrCreate("CacheTime", entry =>
- {
- entry.SlidingExpiration = TimeSpan.FromSeconds(45);
- return DateTime.Now;
- });
- return View(currentTime);
- }
Now, we will create a View named Index under Views/Home folder for the Index action method as per following code snippet.
- @model DateTime?
-
- @using (Html.BeginForm("RemoveCache","Home"))
- {
- <div class="row">
- <div class="col-md-3">
- <h3> Current Time: @DateTime.Now.ToString()</h3>
- </div>
- <div class="col-md-3">
- <h3> Cache Time: @(Model!=null ? @Model.ToString() : "Cache is not available")</h3>
- </div>
- <div class="col-md-3">
- <input type="submit" class="btn btn-danger" value="Remove" />
- </div>
- </div>
- }
As this View has Submit button which calls an action method named RemoveCache. This action method removes cached data and returns same View. The following code snippet is used for same.
- [HttpPost]
- public IActionResult RemoveCache()
- {
- memoryCache.Remove("CacheTime");
- DateTime? currentTime = memoryCache.Get<DateTime?>("CacheTime");
- return View("Index",currentTime);
- }
Now, run the application. It shows both current time and cached data along with Remove button. When clicked on Remove button, it calls action method and removes the cached data.
Figure 3: Remove Cache Data
Conclusion
The In-Memory cache is used when application is hosted on single Server. It stores data on Server and improves application performance.
Download
You can download the complete source code from MSDN sample, using the links, mentioned below.
- Rating Star Application in ASP.NET Core
- CRUD Operations in ASP.NET Core and Entity Framework Core
- Repository Pattern In ASP.NET Core
- Generic Repository Pattern in ASP.NET Core
- Onion Architecture In ASP.NET Core MVC
- NET Core MVC: Authentication and Role Based Authorisation with Identity
- NET Core MVC: Authentication and Claim Based authorization with Identity