filipw / Strathweb.CacheOutput

ASP.NET Web API CacheOutput - library to allow you to cache the output of ApiControllers
Other
882 stars 253 forks source link

Can i configure options in the web.config #235

Open lesenro opened 6 years ago

lesenro commented 6 years ago

Can i configure options in the web.config

Kraviecc commented 6 years ago

Same question here. Is there a way to set ie. ClientTimeSpan in web.config's app settings and then use it as default value (for example in Global.asax or somewhere)? Anyway, it would be nice to still be able to override this value when setting CacheOutput's ClientTimeSpan attribute above action.

evry-johan commented 4 years ago

Late to the party but create an attribute that inherits from CacheOutputAttribute and apply settings in OnActionExecuting

pedrocpinto commented 3 years ago

@evry-johan Can you please provide an example? Thank you in advance :)

evry-johan commented 3 years ago

@pedrocpinto Sure, this is how I do it. Just as in IsCachingAllowed, you can get custom values in OnActionExecuting:

public class CachedAttribute : CacheOutputAttribute
{
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var maxAge = 86400;
            ClientTimeSpan = maxAge;
            ServerTimeSpan = maxAge;
            base.OnActionExecuting(actionContext);
        }

        protected override bool IsCachingAllowed(HttpActionContext actionContext, bool anonymousOnly)
        {
            return base.IsCachingAllowed(actionContext, anonymousOnly) && ObjectFactory.GetInstance<ICacheManager>().EnableCache();
        }
}
pedrocpinto commented 3 years ago

@evry-johan Thank you, that helped a lot 👍