using System;
using System.Net.Http.Headers;
using System.Web.Http.Filters;

namespace LHDN.CRM.WebAPI.Filters
{
    public class CacheWebApiAttribute : ActionFilterAttribute
    {
        public CacheWebApiAttribute()
        {
            Duration = 0;
        }

        public int Duration { get; set; }

        public override void OnActionExecuted(HttpActionExecutedContext filterContext)
        {
            if (filterContext.Response != null)
                filterContext.Response.Headers.CacheControl = new CacheControlHeaderValue()
                {
                    MaxAge = TimeSpan.FromMinutes(Duration),
                    MustRevalidate = true,
                    NoCache = true,
                    NoStore = true
                };
        }
    }
}