我希望能够在我自己的动作过滤器的OnActionExecuting方法上指定缓存持续时间。
虽然我能够使用内置的System.Web.Mvc.OutputCacheAttribute
属性来装饰我的方法,但它并没有起作用。我把它作为用例,纯粹是在控制器的动作方法中。
[OutputCache(Duration = 300)]
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//some code here
}
因此,就自定义操作过滤器中的缓存而言,最佳做法是什么?
答案 0 :(得分:0)
我认为ASP.NET MVC中没有任何开箱即用的功能。您可以使用System.Web.Caching.Cache
课程并开发自己的解决方案。
var cache = new Cache();
// Set cache value:
cache.Add("YourKey", "YourData", null, DateTime.Now.AddSeconds(300), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
// Get cache value:
var value = cache["YourKey"];