为什么 MemoryCache 中的滑动过期行为如此奇怪?

Mar*_*kus 2 .net c# caching memorycache

我不明白在 System.Runtime.Caching.MemoryCache 和 .NET 4.0 中滑动过期应该如何工作。

根据文档,到期时间跨度是“在缓存条目从缓存中逐出之前必须访问缓存条目的时间跨度”。

但是,以下单元测试失败:

private const string AnyKey = "key";
private const string AnyValue = "value";

private readonly TimeSpan timeout = TimeSpan.FromSeconds(0.5);

private void WaitSixtyPercentOfTheTimeout()
{
    Thread.Sleep(TimeSpan.FromSeconds(timeout.TotalSeconds*0.6));
}

[Test]
public void Get_RefreshesTimeoutOfSlidingExpiration()
{
    var cache = MemoryCache.Default;
    cache.Set(AnyKey, AnyValue, new CacheItemPolicy {SlidingExpiration = timeout});

    WaitSixtyPercentOfTheTimeout();
    cache[AnyKey].Should().Be(AnyValue);
    WaitSixtyPercentOfTheTimeout();

    cache[AnyKey].Should().Be(AnyValue);
}

private void UpdateCallback(CacheEntryUpdateArguments arguments)
{
}
Run Code Online (Sandbox Code Playgroud)

巧合的是,我做了一个小改动来解决这个问题。但是,如果这是一个错误或功能,现在有人吗?

一旦设置了 UpdateCallBack,到期将按预期工作:

// [...]

[Test]
public void Get_RefreshesTimeoutOfSlidingExpiration()
{
    var cache = MemoryCache.Default;
    cache.Set(AnyKey, AnyValue, new CacheItemPolicy {SlidingExpiration = timeout, UpdateCallback = UpdateCallback});

    WaitSixtyPercentOfTheTimeout();
    cache[AnyKey].Should().Be(AnyValue);
    WaitSixtyPercentOfTheTimeout();

    cache[AnyKey].Should().Be(AnyValue);
}

private void UpdateCallback(CacheEntryUpdateArguments arguments)
{
}
Run Code Online (Sandbox Code Playgroud)

tia*_*tia 5

延长超时时间似乎可以解决问题。让它在我的机器上运行 2 秒:

private readonly TimeSpan timeout = TimeSpan.FromSeconds(2);
Run Code Online (Sandbox Code Playgroud)

我猜目前的缓存机制在时间上不是很准确,但实际上你无论如何都不会保持缓存半秒。

  • 这很奇怪,但似乎是正确的解决方案。我认为 SlidingExpiration 设置对我不起作用,直到我开始在测试中使用超过 1 秒的过期时间。 (2认同)
  • 为了保持这个答案的有用性 - /sf/ask/2897684331/ 检查来自 System.Runtime.Caching 的一些代码,确定 1 秒的最小值。 (2认同)