|
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Web.Caching;
namespace SampleMVCWeb.ActionFilters
{
public class ResultCacheAttribute : ActionFilterAttribute
{
public ResultCacheAttribute()
{
}
public string CacheKey
{
get;
private set;
}
public CacheDependency Dependency
{
get;
set;
}
private CacheItemPriority _priority = CacheItemPriority.Default;
public CacheItemPriority Priority
{
get
{
return _priority;
}
set
{
_priority = value;
}
}
/// <summary>
/// Duration in seconds of the cached values before expiring.
/// </summary>
public int Duration
{
get;
set;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string url = filterContext.HttpContext.Request.Url.PathAndQuery;
this.CacheKey = "ResultCache-" + url;
if (filterContext.HttpContext.Cache[this.CacheKey] != null)
{
filterContext.Result = (ActionResult)filterContext.HttpContext.Cache[this.CacheKey];
}
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewData["CachedStamp"] = DateTime.Now;
filterContext.HttpContext.Cache.Add(this.CacheKey, filterContext.Result, Dependency, DateTime.Now.AddSeconds(Duration), System.Web.Caching.Cache.NoSlidingExpiration, Priority, null);
base.OnActionExecuted(filterContext);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.