Showing posts with label ASP.NET Web API Caching. Show all posts
Showing posts with label ASP.NET Web API Caching. Show all posts

Friday, July 19, 2019

ASP.NET Web API Caching

In this blog, we will discuss about the memory caching and how it improve the overall performance of Web API. Cached data save database call or external call to process the future request.



Web API doesn’t support the output caching and we have to store the data in local memory or in database.

Here is code sample to implement memory caching in web api.

Microsoft provides the System.Runtime.Caching library for memory caching.

Add a reference above lib and here is a Helper Class to store data or get data from cached memory

using System;
using System.Runtime.Caching;

public static class MemoryCacher
{
    public static object GetValue(string key)
    {
        MemoryCache memoryCache = MemoryCache.Default;
        return memoryCache.Get(key);
    }

    public static bool Add(string key, object value, DateTimeOffset absExpiration)
    {
        MemoryCache memoryCache = MemoryCache.Default;
        return memoryCache.Add(key, value, absExpiration);
    }

    public static void Delete(string key)
    {
        MemoryCache memoryCache = MemoryCache.Default;
        if (memoryCache.Contains(key))
        {


            memoryCache.Remove(key);
        }
    }
}

Add data in cache memory:
MemoryCacher.Add(“Key”,Object, DateTimeOffset.UtcNow.AddYears(1))

Get data from cache memory:
MemoryCacher.Get(“Key”)

ASP.NET Web API provides the filters, that you can use to add extra logic before or after action executes, so above caching data logic you can use inside filter to cache web api response.

here is example, how to create action filter that cache web api response.

  public class WebAPICacheAttribute : ActionFilterAttribute
    {
        public int Duration { getset; }
        private bool CacheEnabled = false;

      public WebAPICacheAttribute(int _duration, bool _cacheEnabled)
        {
            Duration = _duration;
            CacheEnabled = _cacheEnabled;
        }

        public override void OnActionExecuting(HttpActionContext context)
        {
            if (CacheEnabled)
            {
                if (context != null)
                {                   
                        //generate cache key from HTTP request URI and Header
                        string _cachekey = string.Join(":"new string[]
                        {
                            context.Request.RequestUri.OriginalString,
                            context.Request.Headers.Accept.FirstOrDefault().ToString(),
                        });                      

                        // Check Key exists
                        if (MemoryCacher.Contains(_cachekey))
                        {    

                            var val = (string)MemoryCacher.GetValue(_cachekey);
                            if (val != null)
                            {
                                context.Response = context.Request.CreateResponse();
                                context.Response.Content = new StringContent(val);
                                var contenttype = (MediaTypeHeaderValue)MemoryCacher.GetValue(_cachekey +
                            ":response-ct");
                                if (contenttype == null)
                                    contenttype = new MediaTypeHeaderValue(_cachekey.Split(':')[1]);
                                context.Response.Content.Headers.ContentType = contenttype;
                                return;
                            }
                        }                    
                }             
            }
        }


        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
          
                if (CacheEnabled)
                {
                    if (WebApiCache != null)
                    {
                        string _cachekey = string.Join(":"new string[]
                        {
                            context.Request.RequestUri.OriginalString,
                            context.Request.Headers.Accept.FirstOrDefault().ToString(),
                        });

                    if (context.Response != null && context.Response.Content != null)
                        {
                           string body = context.Response.Content.ReadAsStringAsync().Result;

                         if (MemoryCacher.Contains(_cachekey))
                            {
                        MemoryCacher.Add(_cachekey, body, DateTime.Now.AddSeconds(Duration));
                                MemoryCacher.Add(_cachekey + ":response-ct",
                                context.Response.Content.Headers.ContentType,
                                DateTime.Now.AddSeconds(_timespan));
                            }
                            else
                            {
                        MemoryCacher.Add(_cachekey, body, DateTime.Now.AddSeconds(Duration));
                                MemoryCacher.Add(_cachekey + ":response-ct",
                                context.Response.Content.Headers.ContentType,
                                DateTime.Now.AddSeconds(Duration));
                            }
                        }
                    }
                }                    
        }

    }




Now You can use WebAPICache action filter on GetProject Action to cache Project data and if the client will send the same request, the Web  API will not call to data repository layer to get project records , it will get from cache
Web API Controller:

public class ProjectApiController : ApiController
{
 [HttpGet]
        [Route("api/Project/{projectId:int}")]
        [WebAPICache(_duration:3600,_cacheEnabled: true)]
        public Project GetProject(int projectId)
        {
            return Repository.GetProperty(projectId);
 }
  }


Other related topics 
  1. ASP.NET Web API Caching
  2. Steps To Improve ASP.NET Web API Performance
  3. 4 Simple Steps To Create ASP.NET Web API
  4. Parameter Binding or Model Binding in ASP.NET Web API
  5. The Life-cycle of an ASP.NET Web API
Thanks for visiting !!

Setup Swagger for ASP.Net Web API

this blog demonstrates step by step to add swagger in web api project and will submit  http request GET/POST/PUT via swagger UI. Swagger ...