Added some extra caching to reduce DB calls

pull/3970/head
tidusjar 4 years ago
parent cb127f3858
commit fcc87740bf

@ -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 um = context.HttpContext.RequestServices.GetRequiredService<OmbiUserManager>(); var cache = context.HttpContext.RequestServices.GetRequiredService<ICacheService>();
var user = await um.FindByIdAsync(userid); 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) 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…
Cancel
Save