Friday, December 14, 2018

Caching in Web API

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.

By default get is not cacheable, you need to make it using Action Filter

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  
        };  
    }  
    }  



https://www.c-sharpcorner.com/article/implementing-caching-in-web-api/

No comments:

Followers

Link