高速缓存在Identity Server 4中似乎无法正常工作 [英] Caching Does Not Appear to Work in Identity Server 4

查看:301
本文介绍了高速缓存在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<ClientStore>()中,它会被注入.但是(例如在我的示例中)如果商店使用了其他服务,则需要注入它们.

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天全站免登陆