缓存在Identity Server 4中似乎不起作用

我正在尝试使用缓存方法为我们的IS4实现添加缓存。 但是,我的实现似乎没有对登录速度或每次登录访问我的数据库的查询数量产生任何影响(我希望缓存可以减少这两者)。

我为实现缓存所做的更改如下:

在Startup.cs ConfigureServices中添加了以下内容

更新了services.AddIdenttiyServer()调用以包含以下行:

.AddInMemoryCaching() .AddClientStoreCache() .AddResourceStoreCache() .AddCorsPolicyCache(); 

更新了ConfigureServices以使其具有以下内容:

 services.AddScoped(); services.AddScoped(); services.AddScoped(); 

这似乎是我需要实现的唯一事情,当应用程序正常运行时,缓存似乎没有做任何事情。 我错过了什么?

基本上你需要做两件事:

首先实现IClientStore

 public class ClientStore : IClientStore { private readonly IClientService clientService; public ClientStore(IClientService clientService) { this.clientService = clientService; } public Task FindClientByIdAsync(string clientId) { var client = this.clientService.GetById(clientId); return Task.FromResult(client); } } 

ClientService是我从db获取客户端的实现,所以你需要自己设置。

然后在Startup.cs你需要:

 services.AddIdentityServer(options => { options.Caching.ClientStoreExpiration = new TimeSpan(0, 5, 0); }) .AddInMemoryCaching() .AddClientStoreCache() .// More stuff that you need 

这适用于Client CachingCorsResourceStore完全相同。

我认为你缺少options.Caching.ClientStoreExpiration部分。 从那里开始。

希望这会有所帮助。

PS:忘了提 – 你不需要明确地注入你的IClientStore实现。 通过将它添加到.AddClientStoreCache()它会被注入。 但是(如我的例子中)如果你有商店使用的其他服务,你需要注入它们。

没有标准的方法来缓存用户。

它只缓存:

  1. 客户端 – AddClientStoreCache
  2. 资源 – AddResourceStoreCache
  3. CorsPolicy – AddCorsPolicyCache

您可以从文档中获得更多详细信息