将缓存数据条目的时间戳重置为now()

问题描述 投票:0回答:1

我正在尝试重置缓存条目上的时间戳。我想知道如何实现这一目标。

现在我正在创建一个新的MemoryCacheEntryOptions实例

        private MemoryCacheEntryOptions GetCacheOptions()
        {
            return new MemoryCacheEntryOptions()
                .SetSlidingExpiration(TimeSpan.FromSeconds(20)); // Cache for some seconds.
        }  

我希望能够重置SetSlideExpiration()以从零(0)秒开始计数,每次MemoryCacheEntry中存在缓存数据。

因此,如果


public bool hasCached(string key, byte[] values)
{
  return _memoryCache.TryGetValue(GetDocStoreKey(key), out values);
}

返回true我应该能够重置SetSlideExpiration从头开始计数。

c# asp.net asp.net-core .net-core
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.