CACHING
Caching is a technique of storing frequently used data or information in a local memory, for a certain time period. So, next time, when the client requests the same information, instead of retrieving the information from the database, it will give the information from the local memory. The main advantage of caching is that it improves the performance by reducing the processing burden.
Here, in this article, I will explain how to use caching in web API to improve its performance, by decreasing the response time. Here, I have created a web API project. I have a "DataController" and "GetData" action Method, as per the following.
- [AllowAnonymous]
- [Route("GetData")]
- public async Task<IHttpActionResult> getData()
- {
- Dictionary<object, object> obj = new Dictionary<object, object>();
- obj.Add("1", "Punjab");
- obj.Add("2", "Assam");
- obj.Add("3", "UP");
- obj.Add("4", "AP");
- obj.Add("5", "J&K");
- obj.Add("6", "Odisha");
- obj.Add("7", "Delhi");
- obj.Add("9", "Karnataka");
- obj.Add("10", "Bangalore");
- obj.Add("21", "Rajesthan");
- obj.Add("31", "Jharkhand");
- obj.Add("41", "chennai");
- obj.Add("51", "jammu");
- obj.Add("61", "Bhubaneshwar");
- obj.Add("71", "Delhi");
- obj.Add("19", "Karnataka");
-
- return Ok(obj);
-
-
-
- }
Now, I will request the method from my PostMan Client, as shown below:
Now, if you check the Header section, you will get no-cache.
We can see that it takes around 2449 miliseconds as my response time.
Now, we will use caching and see how we can reduce the response time of this request. For this, we have to create a custom filter. Just add a new class and rename it, as shown in the image below.
Now, inherit this class from
"ActionFilterAttribute" .
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http.Headers;
- using System.Web;
- using System.Web.Http.Filters;
-
- namespace Caching_In_API
- {
- public class CacheFilter : ActionFilterAttribute
- {
- public int TimeDuration { get; set; }
- public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
- {
- actionExecutedContext.Response.Headers.CacheControl = new CacheControlHeaderValue
- {
- MaxAge = TimeSpan.FromSeconds(TimeDuration),
- MustRevalidate = true,
- Public = true
- };
- }
- }
- }
Now, add the TimeDuration attribute in the required controller, as follows.
Here is my complete action method.
- [AllowAnonymous]
- [Route("GetData")]
- [CacheFilter(TimeDuration=100)]
- public async Task<IHttpActionResult> getData()
- {
- Dictionary<object, object> obj = new Dictionary<object, object>();
- obj.Add("1", "Punjab");
- obj.Add("2", "Assam");
- obj.Add("3", "UP");
- obj.Add("4", "AP");
- obj.Add("5", "J&K");
- obj.Add("6", "Odisha");
- obj.Add("7", "Delhi");
- obj.Add("9", "Karnataka");
- obj.Add("10", "Bangalore");
- obj.Add("21", "Rajesthan");
- obj.Add("31", "Jharkhand");
- obj.Add("41", "chennai");
- obj.Add("51", "jammu");
- obj.Add("61", "Bhubaneshwar");
- obj.Add("71", "Delhi");
- obj.Add("19", "Karnataka");
-
- return Ok(obj);
-
-
-
- }
Now, run the application. On the first time, it will retrieve the data from the database. Later, it shows the information from local memory. Next, we see that the response time has decreased.
Now, if we check the header portion, we will see that the caching has been applied on it.
Thus, in this way, we can create a custom cache filter and apply it on the action method, in web API.