ASP.NET MVC -UPTUMECACHE-设置持续时间到期的缓存值



设置outputcache的持续时间是否到期后值?因为如果这样做,我就不会看到它。

[OutputCache(Duration = 1, Location = OutputCacheLocation.Client, VaryByParam = "none", NoStore = true)]
    public ActionResult Index()
    {
        if (System.Web.HttpContext.Current.Cache["time"] == null)
        {
            System.Web.HttpContext.Current.Cache["time"] = DateTime.Now;
        }
    }

我是新手使用outputcache的新手,所以请原谅初学者问题。但是我的理解是,通过指定持续时间,在分配的时间后会发生一些事情。在上面的代码段中,无论我何时刷新我的观点,时间都持续。

您将outputcache与httpcontext.current.cache混淆。如果没有过期,则使用第一个用于返回缓存的视图。关于那,你是对的。每1秒钟它将返回新视图。

但是,您正在用DateTime填充的httpcontext.current.cache.现在永远不会过期。因为您没有定义绝对到期

https://msdn.microsoft.com/en-us/library/system.web.caching.cache(v = vs.110).aspx

这样做

System.Web.HttpContext.Current.Cache["time"] = DateTime.Now;

与此

相同
System.Web.HttpContext.Current.Cache.Insert("time", DateTime.Now, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration)

使用插入方法并正确定义到期,并且应该起作用。

最新更新