Asp.net Mvc OutputCache属性和滑动到期

时间:2021-06-30 03:51:12

Calling

调用

http://foo/home/cachetest

for

对于

[UrlRoute(Path = "home/cachetest")]
[OutputCache(Duration = 10, VaryByParam = "none")]
public ActionResult CacheTest()
{
    return Content(DateTime.Now.ToString());
}

will show the same content for every 10 seconds no matter how often i refresh page.

无论我多久刷新页面,都会每隔10秒显示相同的内容。

Is it possible to easily add sliding expiration so it would NOT change after 10 seconds in case i have refreshed the page?

是否可以轻松添加滑动过期,以便在我刷新页面后10秒内不会更改?

3 个解决方案

#1


2  

Been reading the source for the OutputCacheAttribute and I don't think there's an easy way to do this.

正在阅读OutputCacheAttribute的源代码,我认为没有一种简单的方法可以做到这一点。

You're most likely going to need to create your own solution.

您最有可能需要创建自己的解决方案。

#2


7  

You could create a custom cache filter instead of default OutputCache one. Like this below, note the sliding expiration could be set here. Caveat in that I have not used this for sliding expiration, but works well for other things.

您可以创建自定义缓存筛选器而不是默认的OutputCache筛选器。如下所示,请注意可以在此处设置滑动到期时间。请注意,我没有使用它来滑动过期,但适用于其他事情。

public class CacheFilterAttribute : ActionFilterAttribute
    {
        private const int Second = 1;
        private const int Minute = 60 * Second;
        private const int Hour = 60 * Minute;
        public const int SecondsInDay = Hour * 24;


        /// <summary>
        /// Gets or sets the cache duration in seconds. 
        /// The default is 10 seconds.
        /// </summary>
        /// <value>The cache duration in seconds.</value>
        public int Duration
        {
            get;
            set;
        }

        public int DurationInDays
        {
            get { return Duration / SecondsInDay; }
            set { Duration = value * SecondsInDay; }
        }

        public CacheFilterAttribute()
        {
            Duration = 10;
        }

        public override void OnActionExecuted(
                               ActionExecutedContext filterContext)
        {
            if (Duration <= 0) return;

            HttpCachePolicyBase cache = 
                     filterContext.HttpContext.Response.Cache;
            TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(cacheDuration));
            cache.SetMaxAge(cacheDuration);
            cache.SetSlidingExpiration(true);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
        }
    }

#3


1  

You can't. Internal timer of Cache class spins every 20 secs. I suggest you to try PCache class under PokeIn library. You can set down to 6 secs on it. Also, PCache far more faster in comparison to .NET cache class.

你不能。 Cache类的内部计时器每20秒旋转一次。我建议你在PokeIn库下尝试PCache类。您可以将其设置为6秒。此外,与.NET缓存类相比,PCache要快得多。

#1


2  

Been reading the source for the OutputCacheAttribute and I don't think there's an easy way to do this.

正在阅读OutputCacheAttribute的源代码,我认为没有一种简单的方法可以做到这一点。

You're most likely going to need to create your own solution.

您最有可能需要创建自己的解决方案。

#2


7  

You could create a custom cache filter instead of default OutputCache one. Like this below, note the sliding expiration could be set here. Caveat in that I have not used this for sliding expiration, but works well for other things.

您可以创建自定义缓存筛选器而不是默认的OutputCache筛选器。如下所示,请注意可以在此处设置滑动到期时间。请注意,我没有使用它来滑动过期,但适用于其他事情。

public class CacheFilterAttribute : ActionFilterAttribute
    {
        private const int Second = 1;
        private const int Minute = 60 * Second;
        private const int Hour = 60 * Minute;
        public const int SecondsInDay = Hour * 24;


        /// <summary>
        /// Gets or sets the cache duration in seconds. 
        /// The default is 10 seconds.
        /// </summary>
        /// <value>The cache duration in seconds.</value>
        public int Duration
        {
            get;
            set;
        }

        public int DurationInDays
        {
            get { return Duration / SecondsInDay; }
            set { Duration = value * SecondsInDay; }
        }

        public CacheFilterAttribute()
        {
            Duration = 10;
        }

        public override void OnActionExecuted(
                               ActionExecutedContext filterContext)
        {
            if (Duration <= 0) return;

            HttpCachePolicyBase cache = 
                     filterContext.HttpContext.Response.Cache;
            TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(cacheDuration));
            cache.SetMaxAge(cacheDuration);
            cache.SetSlidingExpiration(true);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
        }
    }

#3


1  

You can't. Internal timer of Cache class spins every 20 secs. I suggest you to try PCache class under PokeIn library. You can set down to 6 secs on it. Also, PCache far more faster in comparison to .NET cache class.

你不能。 Cache类的内部计时器每20秒旋转一次。我建议你在PokeIn库下尝试PCache类。您可以将其设置为6秒。此外,与.NET缓存类相比,PCache要快得多。