Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement caching on IdentityServer4?

How to implement cache for ClientStore in IdSrv4? I'm already checking the docs at AddClientStoreCache but it does not help me... On my ConfigureServices method i'm configuring IdSrv as follows:

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

...And in my ClientStore implementation i do not have anything about caching...Should i check wheter the info is on cache in FindClientByIdAsync in some way? Or its done for me under the hood?

I found a sample only at IdentityServer4.Postgresql, but i can't replicate it with sucess on my custom Store classes...

like image 403
Richard Lee Avatar asked May 08 '17 22:05

Richard Lee


2 Answers

If you're looking for a sample implementation of the CachingClientStore.cs, you could check out the default implementation (the way the identity server does this) here.

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

     return client;
}

They are giving you the choice how to implement your caching algorithm. You could cache the ClientStore in a In-Memory database such as Redis. The good thing about IdentityServer4 is you can implement the interfaces however you want and however you need them.

like image 128
moritzg Avatar answered Sep 21 '22 23:09

moritzg


AddConfigurationStoreCache() via https://github.com/IdentityServer/IdentityServer4/blob/master/src/EntityFramework/host/Startup.cs#L47

like image 35
Victorio Berra Avatar answered Sep 18 '22 23:09

Victorio Berra