8000 12167 Removed n+1 queries retrieving users with brief representation by sschu · Pull Request #12168 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void removeAttribute(String name) {
@Override
public String getFirstAttribute(String name) {
if (updated != null) return updated.getFirstAttribute(name);
return cached.getAttributes(modelSupplier).getFirst(name);
return cached.getFirstAttribute(name, modelSupplier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,35 @@
public class CachedUser extends AbstractExtendableRevisioned implements InRealm {

private final String realm;
private final String username;
private final Long createdTimestamp;
private final String email;
private final boolean emailVerified;
private final boolean enabled;
private final String federationLink;
private final String serviceAccountClientLink;
private final int notBefore;
private final LazyLoader<UserModel, Set<String>> requiredActions;
private final LazyLoader<UserModel, MultivaluedHashMap<String, String>> attributes;
private final LazyLoader<UserModel, MultivaluedHashMap<String, String>> lazyLoadedAttributes;
private final MultivaluedHashMap<String,String> eagerLoadedAttributes;
private final LazyLoader<UserModel, Set<String>> roleMappings;
private final LazyLoader<UserModel, Set<String>> groups;
private final LazyLoader<UserModel, List<CredentialModel>> storedCredentials;

public CachedUser(Long revision, RealmModel realm, UserModel user, int notBefore) {
super(revision, user.getId());
this.realm = realm.getId();
this.username = user.getUsername();
this.createdTimestamp = user.getCreatedTimestamp();
this.email = user.getEmail();
this.emailVerified = user.isEmailVerified();
this.enabled = user.isEnabled();
this.federationLink = user.getFederationLink();
this.serviceAccountClientLink = user.getServiceAccountClientLink();
this.notBefore = notBefore;
this.eagerLoadedAttributes = new MultivaluedHashMap<>();
this.eagerLoadedAttributes.putSingle(UserModel.USERNAME,user.getUsername());
this.eagerLoadedAttributes.putSingle(UserModel.FIRST_NAME,user.getFirstName());
this.eagerLoadedAttributes.putSingle(UserModel.LAST_NAME,user.getLastName());
this.eagerLoadedAttributes.putSingle(UserModel.EMAIL,user.getEmail());
this.lazyLoadedAttributes = new DefaultLazyLoader<>(userModel -> new MultivaluedHashMap<>(userModel.getAttributes()), MultivaluedHashMap::new);
this.requiredActions = new DefaultLazyLoader<>(userModel -> userModel.getRequiredActionsStream().collect(Collectors.toSet()), Collections::emptySet);
this.attributes = new DefaultLazyLoader<>(userModel -> new MultivaluedHashMap<>(userModel.getAttributes()), MultivaluedHashMap::new);
this.roleMappings = new DefaultLazyLoader<>(userModel -> userModel.getRoleMappingsStream().map(RoleModel::getId).collect(Collectors.toSet()), Collections::emptySet);
this.groups = new DefaultLazyLoader<>(userModel -> userModel.getGroupsStream().map(GroupModel::getId).collect(Collectors.toCollection(LinkedHashSet::new)), LinkedHashSet::new);
this.storedCredentials = new DefaultLazyLoader<>(userModel -> userModel.credentialManager().getStoredCredentialsStream().collect(Collectors.toCollection(LinkedList::new)), LinkedList::new);
Expand All @@ -78,15 +80,22 @@ public String getRealm() {
}

public String getUsername() {
return username;
return eagerLoadedAttributes.getFirst(UserModel.USERNAME);
}

public String getFirstAttribute(String name, Supplier<UserModel> userModel) {
if(eagerLoadedAttributes.containsKey(name))
return eagerLoadedAttributes.getFirst(name);
else
return this.lazyLoadedAttributes.get(userModel).getFirst(name);
}

public Long getCreatedTimestamp() {
return createdTimestamp;
}

public String getEmail() {
return email;
return eagerLoadedAttributes.getFirst(UserModel.EMAIL);
}

public boolean isEmailVerified() {
Expand All @@ -98,7 +107,7 @@ public boolean isEnabled() {
}

public MultivaluedHashMap<String, String> getAttributes(Supplier<UserModel> userModel) {
return attributes.get(userModel);
return lazyLoadedAttributes.get(userModel);
}

public Set<String> getRequiredActions(Supplier<UserModel> userModel) {
Expand Down
0