IDistributedCache 可用但数据不再在缓存中?
IDistributedCache available but the data is no longer in the cache?
我在我的 .Net Core 3 网站中使用 services.AddDistributedMemoryCache()
api startup.cs
class。当我第一次设置缓存时:
public void SetCommandEventMappingCache(IServiceCollection serviceCollection)
{
var cache = serviceCollection.BuildServiceProvider().GetService<IDistributedCache>();
var mappingList = new List<CommandEventMapping>()
{
new CommandEventMapping()
{
ActionType = "add",
GrowFlowCommand = new AddEmployee(),
GrowFlowEvent = "EmployeeAdded",
TraceabilityCommand = "employee_add"
}
};
cache.Set<List<CommandEventMapping>>(
"command_event_mappings", mappingList,new DistributedCacheEntryOptions()
{
AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)
});
//I am able to get back the command_event_mappings here.
//Once the IDistributedCache is injected. the data is lost
var commandMapping = cache.Get<List<CommandEventMapping>>("command_event_mappings");
}
在我见过的所有示例中,通常都是这样设置的。唯一的区别是我为 Set<T>
和 Get<T>
添加了一对扩展。我确实在没有新扩展方法的情况下进行了尝试,但结果相同。实际注入时的IDistributedCache
是可用的,但是之前缓存的数据没有了。
这是我如何注入它的示例。
public LegacyCommandBus(IServiceProvider provider, IDistributedCache cache,
ITraceabilityTenantService tenantService, IHttpClientFactory httpClientFactory)
: base(provider)
{
_provider = provider;
_cache = cache;
_tenantService = tenantService;
_httpClientFactory = httpClientFactory;
//This is null
_commandEventMappings = cache.Get<List<CommandEventMapping>>("command_event_mappings");
}
var cache = serviceCollection.BuildServiceProvider().GetService<IDistributedCache>();
每次你调用 BuildServiceProvider()
,你都会生成一个新的容器,它有自己的一组单例(不同于使用内置 DI 注入的单例)。
解决方案是在 Startup class.
中解析 Configure
中的 IDistributedCache
实例
public void Configure(IApplicationBuilder app, IDistributedCache cache) {
//...
}
我在我的 .Net Core 3 网站中使用 services.AddDistributedMemoryCache()
api startup.cs
class。当我第一次设置缓存时:
public void SetCommandEventMappingCache(IServiceCollection serviceCollection)
{
var cache = serviceCollection.BuildServiceProvider().GetService<IDistributedCache>();
var mappingList = new List<CommandEventMapping>()
{
new CommandEventMapping()
{
ActionType = "add",
GrowFlowCommand = new AddEmployee(),
GrowFlowEvent = "EmployeeAdded",
TraceabilityCommand = "employee_add"
}
};
cache.Set<List<CommandEventMapping>>(
"command_event_mappings", mappingList,new DistributedCacheEntryOptions()
{
AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)
});
//I am able to get back the command_event_mappings here.
//Once the IDistributedCache is injected. the data is lost
var commandMapping = cache.Get<List<CommandEventMapping>>("command_event_mappings");
}
在我见过的所有示例中,通常都是这样设置的。唯一的区别是我为 Set<T>
和 Get<T>
添加了一对扩展。我确实在没有新扩展方法的情况下进行了尝试,但结果相同。实际注入时的IDistributedCache
是可用的,但是之前缓存的数据没有了。
这是我如何注入它的示例。
public LegacyCommandBus(IServiceProvider provider, IDistributedCache cache,
ITraceabilityTenantService tenantService, IHttpClientFactory httpClientFactory)
: base(provider)
{
_provider = provider;
_cache = cache;
_tenantService = tenantService;
_httpClientFactory = httpClientFactory;
//This is null
_commandEventMappings = cache.Get<List<CommandEventMapping>>("command_event_mappings");
}
var cache = serviceCollection.BuildServiceProvider().GetService<IDistributedCache>();
每次你调用 BuildServiceProvider()
,你都会生成一个新的容器,它有自己的一组单例(不同于使用内置 DI 注入的单例)。
解决方案是在 Startup class.
中解析Configure
中的 IDistributedCache
实例
public void Configure(IApplicationBuilder app, IDistributedCache cache) {
//...
}