- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 LazyCache并希望刷新缓存,例如每小时,但理想情况下,我希望缓存项过期后的第一个调用者不要等待缓存重新加载。我写了以下内容
public async Task<List<KeyValuePair<string, string>>> GetCarriersAsync()
{
var options = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = new TimeSpan(1,0,0),// consider to config
}.RegisterPostEvictionCallback(
async (key, value, reason, state) =>
{
await GetCarriersAsync();//will save to cache
_logger.LogInformation("Carriers are reloaded: " );
});
Func<Task<List<KeyValuePair<string, string>>>> cacheableAsyncFunc = () => GetCarriersFromApi();
var cachedCarriers = await _cache.GetOrAddAsync($"Carriers", cacheableAsyncFunc, options);
return cachedCarriers;
}
最佳答案
我发现了类似的问题 In-Memory Caching with auto-regeneration on ASP.Net Core建议调用AddExpirationToken(new CancellationChangeToken(new CancellationTokenSource(_options.ReferenceDataRefreshTimeSpan).Token)
.
我试过了,但没有让它工作。然而,同样的答案通过使用计时器有替代(和推荐)选项。我创建了一个 RefreshebleCache 类,用于不同的可缓存选项,如下所示:
var refreshebleCache = new RefreshebleCache<MyCashableObjectType>(_cache, _logger);
Task<MyCashableObjectType> CacheableAsyncFunc() => GetMyCashableObjectTypeFromApiAsync();
var cachedResponse = await refreshebleCache.GetOrAddAsync("MyCashableObject", CacheableAsyncFunc,
_options.RefreshTimeSpan);
RefreshebleCache 实现:
/// <summary>
/// Based on https://stackoverflow.com/questions/44723017/in-memory-caching-with-auto-regeneration-on-asp-net-core
/// </summary>
/// <typeparam name="T"></typeparam>
public class RefreshebleCache<T>
{
protected readonly IAppCache _cache;
private readonly ILogger _logger;
public bool LoadingBusy = false;
private string _cacheKey;
private TimeSpan _refreshTimeSpan;
private Func<Task<T>> _functionToLoad;
private Timer _timer;
public RefreshebleCache(IAppCache cache, ILogger logger)
{
_cache = cache;
_logger = logger;
}
public async Task<T> GetOrAddAsync (string cacheKey , Func<Task<T>> functionToLoad, TimeSpan refreshTimeSpan)
{
_refreshTimeSpan= refreshTimeSpan;
_functionToLoad = functionToLoad;
_cacheKey = cacheKey;
var timerCachedKey = "Timer_for_"+cacheKey;
//if removed from cache, _timer could continue to work, creating redundant calls
_timer = _appCache.GetOrAdd(timerCachedKey, () =>
CreateTimer(refreshTimeSpan),
SetMemoryCacheEntryOptions(CacheItemPriority.NeverRemove));
var cachedValue = await LoadCacheEntryAsync();
return cachedValue;
}
private Timer CreateTimer(TimeSpan refreshTimeSpan)
{
Debug.WriteLine($"calling CreateTimer for {_cacheKey} refreshTimeSpan {refreshTimeSpan}"); //start first time in refreshTimeSpan
return new Timer(TimerTickAsync, null, refreshTimeSpan, refreshTimeSpan);
}
private async void TimerTickAsync(object state)
{
if (LoadingBusy) return;
try
{
LoadingBusy = true;
Debug.WriteLine($"calling LoadCacheEntryAsync from TimerTickAsync for {_cacheKey}");
var loadingTask = LoadCacheEntryAsync(true);
await loadingTask;
}
catch(Exception e)
{
_logger.LogWarning($" {nameof(T)} for {_cacheKey} was not reloaded. {e} ");
}
finally
{
LoadingBusy = false;
}
}
private async Task<T> LoadCacheEntryAsync(bool update=false)
{
var cacheEntryOptions = SetMemoryCacheEntryOptions();
Func<Task<T>> cacheableAsyncFunc = () => _functionToLoad();
Debug.WriteLine($"called LoadCacheEntryAsync for {_cacheKey} update:{update}");
T cachedValues = default(T);
if (update)
{
cachedValues =await cacheableAsyncFunc();
if (cachedValues != null)
{
_cache.Add(_cacheKey, cachedValues, cacheEntryOptions);
}
// _cache.Add(_cacheKey, cacheableAsyncFunc, cacheEntryOptions);
}
else
{
cachedValues = await _cache.GetOrAddAsync(_cacheKey, cacheableAsyncFunc, cacheEntryOptions);
}
return cachedValues;
}
private MemoryCacheEntryOptions SetMemoryCacheEntryOptions(CacheItemPriority priority= CacheItemPriority.Normal)
{
var cacheEntryOptions = new MemoryCacheEntryOptions
{
Priority = priority
};
return cacheEntryOptions;
}
}
}
关于caching - LazyCache:定期刷新缓存的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56412143/
我正在使用 LazyCache并希望刷新缓存,例如每小时,但理想情况下,我希望缓存项过期后的第一个调用者不要等待缓存重新加载。我写了以下内容 public async Task>> GetCarrie
我正在尝试模拟一个名为 GetOrAddAsync 的函数。它被定义为: Task GetOrAddAsync(string key, Func> addItemFactory, DateTimeOf
微软的 ASP.NET Core 已经是一个非常流行的用于构建 高性能, 模块化 并能运行在 Windows, Linux, MacOS 上的 WEB 框架,通常能够让程序保持高性能的一个有效途
我是一名优秀的程序员,十分优秀!