Skip to content

Commit

Permalink
perf: cache requests results by scope
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Apr 23, 2021
1 parent e9cd6d9 commit 2fa6e37
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public static void AddTheIdServerStores(this IServiceCollection services, Type u
var userStoreType = typeof(UserStore<,>).MakeGenericType(userType, roleType);
var roleStoreType = typeof(RoleStore<>).MakeGenericType(roleType);

services.TryAddScoped(typeof(UserOnlyStore<>)
services.TryAddTransient(typeof(UserOnlyStore<>)
.MakeGenericType(userType),
provider => provider.CreateUserOnlyStore(userOnlyStoreType));
services.TryAddScoped(typeof(IUserStore<>).MakeGenericType(userType),
services.TryAddTransient(typeof(IUserStore<>).MakeGenericType(userType),
provider => provider.CreateUserStore(userOnlyStoreType, userStoreType));
services.TryAddScoped(typeof(IRoleStore<>).MakeGenericType(roleType),
services.TryAddTransient(typeof(IRoleStore<>).MakeGenericType(roleType),
provider => provider.CreateRoleStore(roleStoreType));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static class ServiceProviderExtensions
public static object CreateRoleStore(this IServiceProvider provider, Type roleStoreType)
{
roleStoreType = roleStoreType ?? throw new ArgumentNullException(nameof(roleStoreType));
var roleStore = provider.GetRequiredService<IAdminStore<Role>>();
var RoleClaimeStore = provider.GetRequiredService<IAdminStore<RoleClaim>>();
var roleStore = provider.GetRequiredService<IAdminStore<Role>>();
var errorDescriber = provider.GetRequiredService<IdentityErrorDescriber>();
var constructor = roleStoreType.GetConstructor(new Type[]
{
Expand Down
2 changes: 1 addition & 1 deletion src/Aguacongas.TheIdServer.Identity/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class UserStore<TUser, TRole> : TheIdServerUserStoreBase<TUser,
/// <param name="userRoleStore">The user role store.</param>
/// <param name="userOnlyStore">The user only store.</param>
/// <param name="describer">The <see cref="IdentityErrorDescriber" /> used to describe store errors.</param>
/// <exception cref="System.ArgumentNullException">
/// <exception cref="ArgumentNullException">
/// store
/// or
/// userRoleStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Aguacongas.IdentityServer.Store.Entity;
using Aguacongas.TheIdServer.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -25,11 +24,9 @@ public static DynamicAuthenticationBuilder AddTheIdServerEntityFrameworkStore<TS
var services = builder.Services;
services.AddTransient<IAdminStore<ExternalProvider>>(p =>
{
var store = new AdminStore<ExternalProvider, ConfigurationDbContext>(
p.GetRequiredService<ConfigurationDbContext>(),
p.GetRequiredService<ILogger<AdminStore<ExternalProvider, ConfigurationDbContext>>>());
var store = p.GetRequiredService<CacheAdminStore<AdminStore<ExternalProvider, ConfigurationDbContext>, ExternalProvider>>();
return new NotifyChangedExternalProviderStore(
return new NotifyChangedExternalProviderStore<CacheAdminStore<AdminStore<ExternalProvider, ConfigurationDbContext>, ExternalProvider>>(
store,
p.GetRequiredService<IProviderClient>(),
new PersistentDynamicManager<SchemeDefinition>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2021 @Olivier Lefebvre
using Aguacongas.AspNetCore.Authentication;
using Aguacongas.IdentityServer.Abstractions;
using Aguacongas.IdentityServer.EntityFramework.Store;
using Aguacongas.IdentityServer.Store;
using Aguacongas.IdentityServer.Store.Entity;
using Aguacongas.TheIdServer.Authentication;
using Aguacongas.TheIdServer.Data;
using Aguacongas.TheIdServer.Identity;
using Aguacongas.TheIdServer.Models;
using AutoMapper.Internal;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
Expand All @@ -33,52 +29,45 @@ public static class ServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddIdentityServer4AdminEntityFrameworkStores(this IServiceCollection services, Action<DbContextOptionsBuilder> optionsAction = null)
{
AddStoresForContext(services, typeof(ApplicationDbContext));
return services.AddDbContext<ApplicationDbContext>(optionsAction)
AddStoresForContext(services, typeof(ApplicationDbContext));
return services.AddDbContext<ApplicationDbContext>(optionsAction)
.AddTransient<IAdminStore<User>>(p => {
var userStore = new AdminStore<User, ApplicationDbContext>(
p.GetRequiredService<ApplicationDbContext>(),
p.GetRequiredService<ILogger<AdminStore<User, ApplicationDbContext>>>());
var roleStore = new AdminStore<Role, ApplicationDbContext>(
p.GetRequiredService<ApplicationDbContext>(),
p.GetRequiredService<ILogger<AdminStore<Role, ApplicationDbContext>>>());
var userStore = p.GetRequiredService<CacheAdminStore<AdminStore<User, ApplicationDbContext>, User>>();
var roleStore = p.GetRequiredService<CacheAdminStore<AdminStore<Role, ApplicationDbContext>, Role>>();
return new CheckIdentityRulesUserStore(userStore,
new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
roleStore,
p.GetRequiredService<IAdminStore<UserRole>>(),
new UserOnlyStore<ApplicationUser>(
userStore,
p.GetRequiredService<IAdminStore<UserClaim>>(),
p.GetRequiredService<IAdminStore<UserLogin>>(),
p.GetRequiredService<IAdminStore<UserToken>>(),
p.GetService<IdentityErrorDescriber>()),
p.GetService<IdentityErrorDescriber>()),
p.GetRequiredService<IOptions<IdentityOptions>>(),
p.GetRequiredService<IPasswordHasher<ApplicationUser>>(),
p.GetRequiredService<IEnumerable<IUserValidator<ApplicationUser>>>(),
p.GetRequiredService<IEnumerable<IPasswordValidator<ApplicationUser>>>(),
p.GetRequiredService<ILookupNormalizer>(),
p.GetRequiredService<IdentityErrorDescriber>(),
p,
p.GetRequiredService<ILogger<UserManager<ApplicationUser>>>()));
})
.AddTransient<IAdminStore<Role>>(p => {
var store = new AdminStore<Role, ApplicationDbContext>(
p.GetRequiredService<ApplicationDbContext>(),
p.GetRequiredService<ILogger<AdminStore<Role, ApplicationDbContext>>>());
return new CheckIdentityRulesRoleStore(store,
new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(
store,
p.GetRequiredService<IAdminStore<RoleClaim>>(),
p.GetService<IdentityErrorDescriber>()),
p.GetRequiredService<IEnumerable<IRoleValidator<IdentityRole>>>(),
p.GetRequiredService<ILookupNormalizer>(),
p.GetRequiredService<IdentityErrorDescriber>(),
p.GetRequiredService<ILogger<RoleManager<IdentityRole>>>()));
return new CheckIdentityRulesUserStore<CacheAdminStore<AdminStore<User, ApplicationDbContext>, User>>(userStore,
new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
roleStore,
p.GetRequiredService<IAdminStore<UserRole>>(),
new UserOnlyStore<ApplicationUser>(
userStore,
p.GetRequiredService<IAdminStore<UserClaim>>(),
p.GetRequiredService<IAdminStore<UserLogin>>(),
p.GetRequiredService<IAdminStore<UserToken>>(),
p.GetService<IdentityErrorDescriber>()),
p.GetService<IdentityErrorDescriber>()),
p.GetRequiredService<IOptions<IdentityOptions>>(),
p.GetRequiredService<IPasswordHasher<ApplicationUser>>(),
p.GetRequiredService<IEnumerable<IUserValidator<ApplicationUser>>>(),
p.GetRequiredService<IEnumerable<IPasswordValidator<ApplicationUser>>>(),
p.GetRequiredService<ILookupNormalizer>(),
p.GetRequiredService<IdentityErrorDescriber>(),
p,
p.GetRequiredService<ILogger<UserManager<ApplicationUser>>>()));
})
.AddTransient<IAdminStore<Role>>(p => {
var store = p.GetRequiredService<CacheAdminStore<AdminStore<Role, ApplicationDbContext>, Role>>();
return new CheckIdentityRulesRoleStore<CacheAdminStore<AdminStore<Role, ApplicationDbContext>, Role>>(store,
new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(
store,
p.GetRequiredService<IAdminStore<RoleClaim>>(),
p.GetService<IdentityErrorDescriber>()),
p.GetRequiredService<IEnumerable<IRoleValidator<IdentityRole>>>(),
p.GetRequiredService<ILookupNormalizer>(),
p.GetRequiredService<IdentityErrorDescriber>(),
p.GetRequiredService<ILogger<RoleManager<IdentityRole>>>()));
});
}

Expand All @@ -104,9 +93,15 @@ private static void AddStoresForContext(IServiceCollection services, Type dbCont
var entityType = property.PropertyType.GetGenericArguments()[0];
var adminStoreType = typeof(AdminStore<,>)
.MakeGenericType(entityType.GetTypeInfo(), dbContextType.GetTypeInfo()).GetTypeInfo();
services.AddTransient(adminStoreType);

var cacheAdminStoreType = typeof(CacheAdminStore<,>)
.MakeGenericType(adminStoreType.GetTypeInfo(), entityType.GetTypeInfo()).GetTypeInfo();
services.AddTransient(cacheAdminStoreType);

var iAdminStoreType = typeof(IAdminStore<>)
.MakeGenericType(entityType.GetTypeInfo()).GetTypeInfo();
services.AddTransient(iAdminStoreType, adminStoreType);
services.AddTransient(iAdminStoreType, cacheAdminStoreType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Aguacongas.IdentityServer.Store.Entity;
using Aguacongas.TheIdServer.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -39,11 +38,9 @@ public static DynamicAuthenticationBuilder AddTheIdServerEntityMongoDbStore<TSch
var services = builder.Services;
services.AddTransient<IAdminStore<ExternalProvider>>(p =>
{
var store = new AdminStore<ExternalProvider>(
p,
p.GetRequiredService<ILogger<AdminStore<ExternalProvider>>>());
var store = p.GetRequiredService<CacheAdminStore<AdminStore<ExternalProvider>, ExternalProvider>>();
return new NotifyChangedExternalProviderStore(
return new NotifyChangedExternalProviderStore<CacheAdminStore<AdminStore<ExternalProvider>, ExternalProvider>>(
store,
p.GetRequiredService<IProviderClient>(),
new PersistentDynamicManager<SchemeDefinition>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2021 @Olivier Lefebvre
using Aguacongas.AspNetCore.Authentication;
using Aguacongas.IdentityServer.Abstractions;
using Aguacongas.IdentityServer.MongoDb.Store;
using Aguacongas.IdentityServer.Store;
using Aguacongas.IdentityServer.Store.Entity;
using Aguacongas.TheIdServer.Authentication;
using Aguacongas.TheIdServer.Identity;
using Aguacongas.TheIdServer.Models;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -44,47 +41,35 @@ public static IServiceCollection AddIdentityServer4AdminMongoDbStores(this IServ
AddMonogoAdminStore(services, entityType, getDatabase);
}

services.AddTransient<IAdminStore<ExternalProvider>>(p => new NotifyChangedExternalProviderStore(new AdminStore<ExternalProvider>(
p,
p.GetRequiredService<ILogger<AdminStore<ExternalProvider>>>()),
p.GetRequiredService<IProviderClient>(),
p.GetRequiredService<PersistentDynamicManager<SchemeDefinition>>(),
p.GetRequiredService<IAuthenticationSchemeOptionsSerializer>()))
.AddTransient<IAdminStore<User>>(p => {
var userStore = new AdminStore<User>(
p,
p.GetRequiredService<ILogger<AdminStore<User>>>());
var roleStore = new AdminStore<Role>(
p,
p.GetRequiredService<ILogger<AdminStore<Role>>>());
services.AddTransient<IAdminStore<User>>(p => {
var userStore = p.GetRequiredService<CacheAdminStore<AdminStore<User>, User>>();
var roleStore = p.GetRequiredService<CacheAdminStore<AdminStore<Role>, Role>>();
return new CheckIdentityRulesUserStore(userStore,
new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
roleStore,
p.GetRequiredService<IAdminStore<UserRole>>(),
new UserOnlyStore<ApplicationUser>(
userStore,
p.GetRequiredService<IAdminStore<UserClaim>>(),
p.GetRequiredService<IAdminStore<UserLogin>>(),
p.GetRequiredService<IAdminStore<UserToken>>(),
p.GetService<IdentityErrorDescriber>()),
return new CheckIdentityRulesUserStore<CacheAdminStore<AdminStore<User>, User>>(userStore,
new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
roleStore,
p.GetRequiredService<IAdminStore<UserRole>>(),
new UserOnlyStore<ApplicationUser>(
userStore,
p.GetRequiredService<IAdminStore<UserClaim>>(),
p.GetRequiredService<IAdminStore<UserLogin>>(),
p.GetRequiredService<IAdminStore<UserToken>>(),
p.GetService<IdentityErrorDescriber>()),
p.GetRequiredService<IOptions<IdentityOptions>>(),
p.GetRequiredService<IPasswordHasher<ApplicationUser>>(),
p.GetRequiredService<IEnumerable<IUserValidator<ApplicationUser>>>(),
p.GetRequiredService<IEnumerable<IPasswordValidator<ApplicationUser>>>(),
p.GetRequiredService<ILookupNormalizer>(),
p.GetRequiredService<IdentityErrorDescriber>(),
p,
p.GetRequiredService<ILogger<UserManager<ApplicationUser>>>()));
p.GetService<IdentityErrorDescriber>()),
p.GetRequiredService<IOptions<IdentityOptions>>(),
p.GetRequiredService<IPasswordHasher<ApplicationUser>>(),
p.GetRequiredService<IEnumerable<IUserValidator<ApplicationUser>>>(),
p.GetRequiredService<IEnumerable<IPasswordValidator<ApplicationUser>>>(),
p.GetRequiredService<ILookupNormalizer>(),
p.GetRequiredService<IdentityErrorDescriber>(),
p,
p.GetRequiredService<ILogger<UserManager<ApplicationUser>>>()));
})
.AddTransient<IAdminStore<Role>>(p => {
var store = new AdminStore<Role>(
p,
p.GetRequiredService<ILogger<AdminStore<Role>>>());
var store = p.GetRequiredService<CacheAdminStore<AdminStore<Role>, Role>>();
return new CheckIdentityRulesRoleStore(store,
return new CheckIdentityRulesRoleStore<CacheAdminStore<AdminStore<Role>, Role>>(store,
new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(
store,
Expand All @@ -105,8 +90,20 @@ private static void AddMonogoAdminStore(IServiceCollection services, Type entity
{
return GetCollection(getDatabase, provider, entityType);
});
services.AddTransient(typeof(IAdminStore<>).MakeGenericType(entityType.GetTypeInfo()).GetTypeInfo(),
typeof(AdminStore<>).MakeGenericType(entityType.GetTypeInfo()).GetTypeInfo());

var adminStoreType = typeof(AdminStore<>)
.MakeGenericType(entityType.GetTypeInfo()).GetTypeInfo();
services.AddTransient(adminStoreType);

var cacheAdminStoreType = typeof(CacheAdminStore<,>)
.MakeGenericType(adminStoreType.GetTypeInfo(), entityType.GetTypeInfo()).GetTypeInfo();
services.AddTransient(cacheAdminStoreType);

var iAdminStoreType = typeof(IAdminStore<>)
.MakeGenericType(entityType.GetTypeInfo()).GetTypeInfo();
services.AddTransient(iAdminStoreType, cacheAdminStoreType);


}

private static object GetCollection(Func<IServiceProvider, IMongoDatabase> getDatabase, IServiceProvider provider, Type entityType)
Expand Down
Loading

0 comments on commit 2fa6e37

Please sign in to comment.