【发布时间】:2020-05-06 16:24:16
【问题描述】:
我在由云代工厂托管的 ASP.Net 核心 Web 应用程序中使用 IMemoryCache。
应用程序有 3 个实例。
一切正常,但是当我想删除缓存时,它仅在一个实例上从逻辑上删除缓存,而其他两个实例仍然具有旧的(缓存的)值。
我的代码如下:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// other code...
services.AddMemoryCache();
// other code...
}
ExampleController.cs
private IMemoryCache cache;
public ExampleController(IMemoryCache cache)
{
this.cache = cache;
}
private void ClearCacheByKey(string key)
{
this.cache.Remove(key);
}
如何处理缓存和应用程序的多个实例的问题?
【问题讨论】:
-
@CodeCaster 我的应用程序没有数据库
-
那么您错过了该链接的重点。内存缓存不能扩展到多台服务器。你需要一个分布式缓存。 一个这样的解决方案使用 SQL Server,但您也可以使用 Redis,例如。
标签: c# asp.net-core caching cloud-foundry