如何在 IdentityServer4 上实现缓存?

Ric*_*Lee 5 identityserver4

如何在IdSrv4中为ClientStore实现缓存?我已经在AddClientStoreCache检查文档, 但它对我没有帮助......在我的ConfigureServices方法中,我正在按如下方式配置 IdSrv:

services.AddIdentityServer(options =>
        {
            options.IssuerUri = "http://idp.address.jus.br/";
            options.Caching.ClientStoreExpiration = TimeSpan.FromHours(1);
        })
        .AddSigningCredential(cert)
        .AddClientStoreCache<ClientStore>();
Run Code Online (Sandbox Code Playgroud)

...在我的ClientStore实现中,我没有任何关于缓存的内容......我是否应该以某种方式检查FindClientByIdAsync中的缓存信息?或者它是在引擎盖下为我完成的?

我只在IdentityServer4.Postgresql 上找到了一个示例,但我无法在我的自定义Store类上成功复制它...

mor*_*tzg 4

如果您正在寻找 的示例实现,您可以在此处CachingClientStore.cs查看默认实现(身份服务器执行此操作的方式)。

public async Task<Client> FindClientByIdAsync(string clientId)
{
     var client = await _cache.GetAsync(clientId,
     _options.Caching.ClientStoreExpiration,
     () => _inner.FindClientByIdAsync(clientId),
     _logger);

     return client;
}
Run Code Online (Sandbox Code Playgroud)

他们让您选择如何实现缓存算法。您可以将 ClientStore 缓存在内存数据库(例如 Redis)中。IdentityServer4 的好处是您可以按照您想要的方式实现接口。