c# - MemoryCache 不接受任何过期时间
问题描述
我有一个非常简单的代码,但它的作用完全奇怪。这是一个简单的 Cache 抽象,如下所示:
public class CacheAbstraction
{
private MemoryCache _cache;
public CacheAbstraction()
{
_cache = new MemoryCache(new MemoryCacheOptions { });
}
public async Task<T> GetItemAsync<T>(TimeSpan duration, Func<Task<T>> factory,
[CallerMemberName] string identifier = null ) where T : class
{
return await _cache.GetOrCreateAsync<T>(identifier, async x =>
{
x.SetAbsoluteExpiration(DateTime.UtcNow.Add(duration));
T result = null;
result = await factory();
return result;
});
}
}
现在有趣的部分:我正在通过 1h - 1d 的过期时间
如果我在测试套件中运行它,一切都很好。
如果我将其作为 .net 核心应用程序运行,则过期始终设置为“现在”,并且该项目在下一次缓存检查时过期。哇!?
解决方案
推荐阅读
- android - 广播接收器不在应用程序的同一进程中
- javascript - 在侧边菜单和导航栏上加载相同的链接
- c# - 使用 C# 进程从 chrome 的网络选项卡读取请求和响应数据
- node.js - 使用 lambda 函数 nodejs 从 Amazon Redshift 查询选择
- scala - Fs2处理管道之间的错误?
- factory-bot - FactoryBot 创建记录 + 与共享父级关联
- c# - Ruby:将字节数组转换为整数?
- hl7-fhir - 如何使用扩展扩展 FHIR 中的原始数据类型?
- css-transitions - 设计具有固定高度和 Flexbox 的手风琴时的 Choppy/Jumping 幻灯片过渡
- python - TypeError:需要一个有效的目标来修补。尝试模拟函数返回值时