mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 02:26:55 -07:00
Added some extra caching to reduce DB calls
This commit is contained in:
parent
cb127f3858
commit
fcc87740bf
2 changed files with 9 additions and 4 deletions
|
@ -76,7 +76,7 @@ namespace Ombi
|
|||
DemoSingleton.Instance.Demo = enabledDemo;
|
||||
}
|
||||
|
||||
public static void AddJwtAuthentication(this IServiceCollection services, IConfigurationRoot configuration)
|
||||
public static void AddJwtAuthentication(this IServiceCollection services)
|
||||
{
|
||||
var tokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
|
@ -117,12 +117,17 @@ namespace Ombi
|
|||
OnTokenValidated = async context =>
|
||||
{
|
||||
var userid = context.Principal?.Claims?.Where(x => x.Type.Equals("id", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault()?.Value ?? default;
|
||||
var um = context.HttpContext.RequestServices.GetRequiredService<OmbiUserManager>();
|
||||
var user = await um.FindByIdAsync(userid);
|
||||
var cache = context.HttpContext.RequestServices.GetRequiredService<ICacheService>();
|
||||
var user = await cache.GetOrAdd(userid + "token", async () =>
|
||||
{
|
||||
var um = context.HttpContext.RequestServices.GetRequiredService<OmbiUserManager>();
|
||||
return await um.FindByIdAsync(userid);
|
||||
}, DateTime.UtcNow.AddMinutes(10));
|
||||
if (user == null)
|
||||
{
|
||||
context.Fail("invaild token");
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
|
@ -86,7 +86,7 @@ namespace Ombi
|
|||
services.AddMemoryCache();
|
||||
services.AddHttpClient();
|
||||
|
||||
services.AddJwtAuthentication(Configuration);
|
||||
services.AddJwtAuthentication();
|
||||
|
||||
services.AddMvc()
|
||||
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue