c# - 将缓存数据条目的时间戳重置为 now()
问题描述
我正在尝试重置缓存条目的时间戳。我想知道如何实现这一目标。
现在我正在创建一个新实例 MemoryCacheEntryOptions
private MemoryCacheEntryOptions GetCacheOptions()
{
return new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(20)); // Cache for some seconds.
}
每次SetSlideExpiration()
缓存数据出现在MemoryCacheEntry
.
因此,如果
public bool hasCached(string key, byte[] values)
{
return _memoryCache.TryGetValue(GetDocStoreKey(key), out values);
}
返回true
我应该能够重置 SetSlideExpiration 以从头开始计数。
解决方案
SetSlidingExpiration(TimeSpan.FromSeconds(20))
每次访问缓存中的项目时都会自动重置缓存时间戳
public object GetFile(string key)
{
return (_memoryCache.Get(Cache(key));
}
因此,如果缓存条目是这样访问resultFromCache = _cacheServiceClass.GetFile(cacheKey);
的,则 MemoryCacheEntryOptions()
SetSlidingExpiration(TimeSpan.FromSeconds(20))
每次都会重置。
推荐阅读
- python - Iteratively Load Files from List and Save to Separate DataFrames in Python
- ckeditor - ckeditor 5工具栏链接项目在ui-dialog中不起作用
- python-3.x - date_parser 的问题:TypeError: parse() 接受 1 个位置参数,但给出了 2 个
- java - Convert epoch in string format to just a date without timestamp
- firebase - signOut() does not work after i navigate to a screen..but it works when i do not navigate.. how can i fix this?
- php - PHP "Fatal error: Uncaught Error: Call to a member function prepare() on null"
- python - 为什么 my_str.decode('utf-8') 仍然失败?
- azure - Azure Application Insights 不跟踪 SQL 查询
- typescript - ReferenceError: fn is not defined - Marklogic 服务器端打字稿的 ts-jest 测试
- javascript - 在循环中创建 refs