mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-19 12:59:39 -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;
|
DemoSingleton.Instance.Demo = enabledDemo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddJwtAuthentication(this IServiceCollection services, IConfigurationRoot configuration)
|
public static void AddJwtAuthentication(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
var tokenValidationParameters = new TokenValidationParameters
|
var tokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
|
@ -117,12 +117,17 @@ namespace Ombi
|
||||||
OnTokenValidated = async context =>
|
OnTokenValidated = async context =>
|
||||||
{
|
{
|
||||||
var userid = context.Principal?.Claims?.Where(x => x.Type.Equals("id", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault()?.Value ?? default;
|
var userid = context.Principal?.Claims?.Where(x => x.Type.Equals("id", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault()?.Value ?? default;
|
||||||
|
var cache = context.HttpContext.RequestServices.GetRequiredService<ICacheService>();
|
||||||
|
var user = await cache.GetOrAdd(userid + "token", async () =>
|
||||||
|
{
|
||||||
var um = context.HttpContext.RequestServices.GetRequiredService<OmbiUserManager>();
|
var um = context.HttpContext.RequestServices.GetRequiredService<OmbiUserManager>();
|
||||||
var user = await um.FindByIdAsync(userid);
|
return await um.FindByIdAsync(userid);
|
||||||
|
}, DateTime.UtcNow.AddMinutes(10));
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
context.Fail("invaild token");
|
context.Fail("invaild token");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -86,7 +86,7 @@ namespace Ombi
|
||||||
services.AddMemoryCache();
|
services.AddMemoryCache();
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|
||||||
services.AddJwtAuthentication(Configuration);
|
services.AddJwtAuthentication();
|
||||||
|
|
||||||
services.AddMvc()
|
services.AddMvc()
|
||||||
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
|
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue