首页 > 解决方案 > 将缓存数据条目的时间戳重置为 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 以从头开始计数。

标签: c#asp.netasp.net-core.net-core

解决方案


SetSlidingExpiration(TimeSpan.FromSeconds(20))每次访问缓存中的项目时都会自动重置缓存时间戳

 public object GetFile(string key)
 {
   return (_memoryCache.Get(Cache(key));
 }

因此,如果缓存条目是这样访问resultFromCache = _cacheServiceClass.GetFile(cacheKey);的,则 MemoryCacheEntryOptions() SetSlidingExpiration(TimeSpan.FromSeconds(20))每次都会重置。


推荐阅读