gpt4 book ai didi

caching - 如何在 .net-core-2.1 中使用内存缓存避免缓存未命中

转载 作者:行者123 更新时间:2023-12-05 05:15:34 26 4
gpt4 key购买 nike

如何在后台更新缓存以避免缓存未命中?

在 .net-core-2.1 中,我可以像这样添加内存缓存:

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
}
}

那么使用起来就非常简单了:

[Route("api")]
public class DataController : Controller
{
private readonly IMemoryCache _cache;
private readonly DataContext _dataContext;

public DataController(IMemoryCache cache, DataContext dataContext)
{
_cache = cache;
_dataContext = dataContext;
}

[HttpGet]
[Route("GimmeCachedData")]
public async Task<IActionResult> Get()
{
var cacheEntry = await
_cache.GetOrCreateAsync("MyCacheKey", entry =>
{
entry.AbsoluteExpiration = DateTime.Now.AddSeconds(20);
return Task.FromResult(_dataContext.GetOrders(DateTime.Now));
});

return Ok(cacheEntry);
}
}

但是,在 20 秒的高速缓存 powered bliss infused 请求之后,正如预期的那样,缓存项已过期并且下一个请求因缓存未命中和后续数据加载而停止。

啊!所以缓存只是有时有效。为什么不能选择让它一直工作?

如何添加功能:

  1. 归还旧元素(同时)
  2. 当项目过期或被注意到过期时自动更新缓存以便下一个请求将获得更新后的值?

在尝试解决这个问题时,我在使用 IHostedService 的实现过程中遇到了 2 个主要障碍。 :

  1. 当缓存项过期时,它会被逐出并且不再可用;这意味着我无法返回。
  2. 更新需要数据库的缓存项会导致这些调用超出范围。

此缓存更新可以在注意到缓存未命中后直接启动,也可以通过主动监视下一个项目是否过期来启动。

我尝试使用 ConcurrentDictionary<String, CacheItem> 滚动我自己的缓存(将其添加为单例) . CacheItem类包含 Value 的属性, Expiration , 和一个 Factory (即:值(value)返回代表)。但我发现,因为这个委托(delegate)可能是在请求时设置的,并在 IHostedService 中调用后台线程,它导致上下文超出范围异常。

最佳答案

我找到了一个似乎有效的解决方案。

  1. 实现 IHostedService(扩展自 BackgroundService 类)。此类将作为.net 核心框架管理的后台线程。后台线程将继续进行缓存更新(通过调用 ICache.UpdateCache,如下所述),以避免请求时缓存未命中。
public class CacheUpdateService : BackgroundService
{
private readonly ILogger<CacheUpdateService> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly ICache _cache;

public CacheUpdateService(ILogger<CacheUpdateService> logger, IServiceProvider serviceProvider, ICache cache)
{
_logger = logger;
_serviceProvider = serviceProvider;
_cache = cache;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug("CacheUpdateService is starting.");

stoppingToken.Register(Dispose);

while (!stoppingToken.IsCancellationRequested)
{
try
{
using (var scope = _serviceProvider.CreateScope())
{
var dataContext = scope.ServiceProvider.GetRequiredService<DataContext>();

// This tight loop calls the UpdateCache, which will block if no updates are necessary
await Task.Run(() => _cache.UpdateCache(dataContext), stoppingToken);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception in the CacheUpdateService");
}
}

_logger.LogDebug("CacheUpdateService has stopped.");
}

public override void Dispose()
{
using(var scope = _serviceProvider.CreateScope())
{
var scopedProcessingService = scope.ServiceProvider.GetRequiredService<ICache>();

// Dispose here on ICache will release any blocks
scopedProcessingService.Dispose();
}

base.Dispose();
}
}
  1. Cache下面的类实现了后台 UpdateCache一次更新 1 个过期项目的方法。优先考虑最过期的。它还实现了请求范围的 GetOrCreate方法。注意我在 Func<IDataContext, Object> 中使用委托(delegate)( CacheEntry )作为值(value)人口工厂。这允许 Cache注入(inject)适当范围的类 DataContext (从 IHostedService 收到)并且它还允许调用者指定 DataContext 的哪个方法被调用以获取特定缓存键值的结果。注意我使用的是 AutoResetEvent等待第一次数据填充以及启动下一次缓存刷新的计时器。此实现将在第一次调用该项目时遭受缓存未命中(我猜是在超过 1 小时未调用它之后;因为它将在 1 小时后被逐出。)。
public class CacheEntry
{
public String Key { get; set; }
public Object Value { get; set; }
public Boolean Updating { get; set; }
public Int32 ExpirySeconds { get; set; }
public DateTime Expiration { get; set; }
public DateTime LastAccessed { get; set; }
public Func<IDataContext, Object> ValueFactory { get; set; }
}

public interface ICache : IDisposable
{
void UpdateCache(IDataContext dataContext);
T GetOrCreate<T>(String key, Func<IDataContext, T> factory, Int32 expirySeconds = 0) where T : class;
}

public class Cache : ICache
{
private readonly ILogger _logger;
private readonly ConcurrentDictionary<String, CacheEntry> _cache;
private readonly AutoResetEvent _governor;

public Cache(ILogger<Cache> logger)
{
_logger = logger;
_cache = new ConcurrentDictionary<String, CacheEntry>();
_governor = new AutoResetEvent(false);
}

public void Dispose()
{
_governor.Set();
}

public static Int32 CacheForHour => 3600;
public static Int32 CacheForDay => 86400;
public static Int32 CacheIndefinitely => 0;

public void UpdateCache(IDataContext dataContext)
{
var evictees = _cache.Values
.Where(entry => entry.LastAccessed.AddHours(1) < DateTime.Now)
.Select(entry => entry.Key)
.ToList();

foreach (var evictee in evictees)
{
_logger.LogDebug($"Evicting: {evictee}...");
_cache.Remove(evictee, out _);
}

var earliest = _cache.Values
.Where(entry => !entry.Updating)
.OrderBy(entry => entry.Expiration)
.FirstOrDefault();

if (earliest == null || earliest.Expiration > DateTime.Now)
{
var timeout = (Int32) (earliest?.Expiration.Subtract(DateTime.Now).TotalMilliseconds ?? -1);
_logger.LogDebug($"Waiting {timeout}ms for next expiry...");
_governor.WaitOne(timeout);
return;
}

try
{
_logger.LogDebug($"Updating cache for: {earliest.Key}...");
earliest.Updating = true;
earliest.Value = earliest.ValueFactory(dataContext);
earliest.Expiration = earliest.ExpirySeconds > 0
? DateTime.Now.AddSeconds(earliest.ExpirySeconds)
: DateTime.MaxValue;
_governor.Set();
}
finally
{
earliest.Updating = false;
}
}

public T GetOrCreate<T>(String key, Func<IDataContext, T> factory, Int32 expirySeconds = -1) where T : class
{
var success = _cache.TryGetValue(key, out var entry);

if (success && entry.Value != null)
{
entry.LastAccessed = DateTime.Now;
return (T) entry.Value;
}

if (entry == null)
{
_logger.LogDebug($"Adding new entry to the cache: {key}...");
entry = new CacheEntry
{
Key = key,
Expiration = DateTime.MinValue,
ExpirySeconds = expirySeconds,
LastAccessed = DateTime.Now,
ValueFactory = factory
};

_cache.TryAdd(key, entry);

_governor.Set();
}

while (entry.Value == null)
{
_logger.LogDebug($"Waiting for 1st time cache update: {entry.Key}...");
_governor.WaitOne();
}

return (T)entry.Value;
}
}
  1. DataContext然后像这样创建类。使用 Dapper例如从数据库中检索数据:
public class DataContext : DbContext, IDataContext
{
private readonly IOptions<Settings> _settings;
private String _databaseServer;

public DataContext(IOptions<Settings> settings)
{
_settings = settings;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);

optionsBuilder.UseSqlServer(_settings.Value.ConnectionString);
}

public IEnumerable<OrderInfo> GetOrders(DateTime date)
{
return Database.GetDbConnection().Query<OrderInfo>(
$"SchemaName.usp_GetOrders",
new {Date = date},
commandType: CommandType.StoredProcedure);
}
}
  1. 在 Controller 中 ICache注入(inject)和使用如下:
    [HttpGet]
[Route("Orders/{date}")]
public IActionResult GetOrders(DateTime date)
{
var result = _cache.GetOrCreate(
$"GetOrders_{date:yyyyMMdd}",
context => context.GetOrders(date),
date.Date < DateTime.Today ? Cache.CacheIndefinitely : 20);

return Ok(result);
}
  1. 最后在 DI 设置中将类注册为单例
        services.AddOptions();
services.Configure<Settings>(Configuration);
services.AddLogging();
services.AddDbContext<DataContext>();
services.AddSingleton<ICache, Cache>();
services.AddSingleton<IHostedService, CacheUpdateService>();

关于caching - 如何在 .net-core-2.1 中使用内存缓存避免缓存未命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51451304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com