客户端,作用域和用户仓库的缓存

2017-02-25  本文已影响165人  灭蒙鸟

layout: docs-default

客户端,作用域和用户仓库的缓存

IdentityServer有几种方法从数据库中读取数据到内存中。由于IdentityServer的解耦设计,同一个HTTP请求,可能会导致好几次装载数据。这会导致好几次数据库读取动作。由于这种可能性,IdentityServer设计了一个缓存接口,我们可以实现自己的缓存机制。IdentityServer也提供了一个默认的缓存实现。

默认缓存

IdentityServer提供的缓存是一个内存缓存,缓存的数据有指定的生存期。默认缓存(真的很简单)可以解决大部分性能问题。配置默认缓存,需要使用下面IdentityServerServiceFactory的扩展方法:

var factory = InMemoryFactory.Create(
    users:   Users.Get(),
    clients: Clients.Get(),
    scopes:  Scopes.Get());

factory.ConfigureClientStoreCache();
factory.ConfigureScopeStoreCache();
factory.ConfigureUserServiceCache(TimeSpan.FromMinutes(1));

这些方法需要在设置合适的存储到IdentityServerServiceFactory后调用,他们实际上使用decorator模式包装了实际的存储实现.

自定义缓存

如果需要自定义实现缓存(比如Redis),那么要缓存的数据需要实现ICache<T>,这个缓存接口定义了下面的API:
If a custom cache impelmentation is desired (e.g. using reddis), then you can implement the ICache<T> for the data that needs to be cached. The cache interface defines this API:

上面描述的IdentityServerServiceFactory 扩展方法也有支持Registration的过载方法,来注册自定义的缓存服务:

上一篇 下一篇

猜你喜欢

热点阅读