Mvc - System.Web.Caching - Absolute Expiration& UpdateCallback - 超时始终为20秒

时间:2014-08-22 13:56:39

标签: c# asp.net asp.net-mvc caching

问题

为什么System.Web.Caching.Cache onUpdateCallback尊重absolutionExpiration时间?

上下文

我正在尝试创建一个缓存策略,该策略会在给定的时间间隔后自动刷新。但是,我希望缓存在后台线程上刷新而不是逐出,然后等待新用户请求强制缓存刷新。在达到绝对到期时,我已选择使用onUpdateCallback重新填充缓存。

代码

我已经创建了一个示例控制器来说明这一点:

public class ExampleController : Controller
{
    private const string CacheKey = "Test";

    //Simple function to get current time
    private readonly Func<string> GetTime = 
        () => DateTime.Now.ToString("HH:mm:ss fff");

    //Cache for 5 seconds
    private readonly TimeSpan _cacheDuration = new TimeSpan(0, 0, 5);

    private static bool _cacheWarmed = false;

    private void WarmCache()
    {
        if (_cacheWarmed)
            return;

        _cacheWarmed = true;

        //Cache current time
        HttpContext.Cache
            .Insert(
                key: CacheKey,
                value: GetTime(),
                dependencies: null,
                absoluteExpiration: DateTime.Now.Add(_cacheDuration),
                slidingExpiration: Cache.NoSlidingExpiration,
                //On cache eviction - cache current time (ie eviction time)
                onUpdateCallback:
                      (string s,
                        CacheItemUpdateReason reason,
                        out object cachedItem,
                        out CacheDependency dependency,
                        out DateTime expiration,
                        out TimeSpan slidingExpiration) =>
                      {
                          expiration = DateTime.Now.Add(_cacheDuration);
                          cachedItem = GetTime();
                          dependency = null;
                          slidingExpiration = Cache.NoSlidingExpiration;
                      }); 
    }

    public ActionResult GetCachedTime()
    {
        WarmCache();

        return new ContentResult 
        {  
             Content =  (string)HttpContext.Cache.Get(CacheKey) 
        }; 
    }
}

一个带有一些javascript的简单Html一遍又一遍地调用该方法并输出结果:

<script type="text/javascript">
    $(document).ready(function() {
        var myFunc = function()
        {
            var startTime = new Date().getTime();

            $.ajax({
                url: "/example/GetCachedTime"
            }).done(function(data) {

                var currentTime = new Date();

                var msg =
                    currentTime.toLocaleTimeString() +
                    ": [" +
                    data +
                        "] - Executed in " +
                        (currentTime.getTime() - startTime) +
                        " ms <br/>";

                $("#container").append(msg);

                console.log(msg);
            });
        };

        var intervalHandle = setInterval(myFunc, 2000);

        myFunc();
    });
</script>

结果

为简洁而截断:

 3:45:52 PM: [15:45:40 387] - Executed in 30 ms 
 3:45:55 PM: [15:45:40 387] - Executed in 4 ms 
 3:45:57 PM: [15:45:40 387] - Executed in 4 ms 
 3:45:59 PM: [15:45:40 387] - Executed in 4 ms 
 3:46:01 PM: [15:46:00 399] - Executed in 4 ms 
 3:46:03 PM: [15:46:00 399] - Executed in 4 ms 
         (duplicates removed - :04 - :18)
 3:46:19 PM: [15:46:00 399] - Executed in 3 ms 
 3:46:21 PM: [15:46:20 406] - Executed in 4 ms 
 3:46:23 PM: [15:46:20 406] - Executed in 3 ms 
         (duplicates removed - :24 - :38)
 3:46:39 PM: [15:46:20 406] - Executed in 3 ms 
 3:46:41 PM: [15:46:40 418] - Executed in 3 ms 
 3:46:43 PM: [15:46:40 418] - Executed in 3 ms 
         (duplicates removed - :44 - :58)
 3:46:59 PM: [15:46:40 418] - Executed in 3 ms 
 3:47:01 PM: [15:47:00 419] - Executed in 3 ms 
 3:47:03 PM: [15:47:00 419] - Executed in 3 ms 

即使我为20设置了超时,该方法也会缓存5秒!

我正在尝试更改缓存持续时间:

  • _cacheDuration = new TimeSpan(0, 0, 1) - 无变化
  • _cacheDuration = new TimeSpan(0, 0, 10) - 无变化
  • _cacheDuration = new TimeSpan(0, 0, 20) - 无变化
  • _cacheDuration = new TimeSpan(0, 0, 25) - 缓存40秒

问题再现

那么这里发生了什么?为什么缓存持续时间不受尊重?或者我没有正确设置缓存?

1 个答案:

答案 0 :(得分:1)

ASP.NET缓存中有一个内部计时器,每隔20秒检查一次到期时间。在没有调整一些用户已完成的缓存轮询周期的情况下,5秒的时间段太短了。