【发布时间】:2020-09-04 16:14:47
【问题描述】:
我正在尝试将 Azure Redis 缓存添加到我的应用程序。我试图用拦截器来做,所以当查询完成时,如果结果存在于缓存中,我会替换结果并少做一次查询,否则我会得到查询结果并将其存储在缓存中. 当然,我只在涉及某些表时才这样做。 我已将 Redis 缓存添加到我的启动中:
services.AddDistributedRedisCache(config =>
{
config.Configuration = GetAppSettingsValue("RedisConnectionString", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
});
现在我需要在我的拦截器中使用它:
public class RedisCacheInterceptor : DbCommandInterceptor
{
private readonly IDistributedCache _distributedCache;
public RedisCacheInterceptor(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
}
此时,当我尝试将拦截器添加到数据库连接时,我不知道如何使用依赖注入来实例化拦截器。
我应该如何解决这个问题? 我是否以错误的方式使用拦截器?
【问题讨论】:
标签: .net-core dependency-injection interceptor ef-core-3.1 ef-core-3.0