缓存在 Identity Server 4 中似乎不起作用 [英] Caching Does Not Appear to Work in Identity Server 4

查看:22
本文介绍了缓存在 Identity Server 4 中似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to add caching to our IS4 implementation using their Caching methods. However, my implementation does not appear to be having any impact on the speed of login or the number of queries hitting my database per login (which I would expect caching to reduce both).

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

The changes I made to implement caching are as follows:

将以下内容添加到 Startup.cs ConfigureServices

Added the following to Startup.cs ConfigureServices

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

Updated the services.AddIdenttiyServer() call to include the lines:

.AddInMemoryCaching()
.AddClientStoreCache<IClientStore>()
.AddResourceStoreCache<IResourceStore>()
.AddCorsPolicyCache<ICorsPolicyService>();

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

Updated ConfigureServices to also have the following:

services.AddScoped<ICorsPolicyService, DefaultCorsPolicyService>();
services.AddScoped<IClientStore, ClientStore>();
services.AddScoped<IResourceStore, ResourceStore>();

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

That appeared to be the only things I needed to implement, and while the application runs normally, the caching does not seem to be doing anything. What am I missing?

推荐答案

基本上你需要做两件事:

Basically you need to do 2 things:

首先实现IClientStore:

public class ClientStore : IClientStore
{
    private readonly IClientService clientService;

    public ClientStore(IClientService clientService)
    {
        this.clientService = clientService;
    }

    public Task<Client> FindClientByIdAsync(string clientId)
    {
        var client = this.clientService.GetById(clientId);
        return Task.FromResult(client);
    }
}

ClientService 是我从数据库中获取客户端的实现,因此您需要放置自己的.

The ClientService is my implementation for getting the client from the db, so there you need to put your own.

然后在 Startup.cs 中你需要:

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

这是用于 Client Caching 但对于 CorsResourceStore 是完全相同的.

This is for the Client Caching but for the Cors and the ResourceStore is quite the same.

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

I think that you are missing the options.Caching.ClientStoreExpiration part. Start from there.

希望这会有所帮助.

PS:忘记提及 - 您不需要显式注入 IClientStore 的实现.通过将其添加到 .AddClientStoreCache() 中,它会被注入.但是(就像我的例子一样)如果你有其他服务,被商店使用,你需要注入它们.

PS: Forgot to mention - you don't need to explicitly inject your implementation of the IClientStore. By adding it to the .AddClientStoreCache<ClientStore>() it gets injected. But (as in my example) if you have other services, used by the store, you need to inject them.

这篇关于缓存在 Identity Server 4 中似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆