Skip to content

Commit

Permalink
perf(auth): validate user through cache to avoid duplicate hash calcu…
Browse files Browse the repository at this point in the history
…lation (#1460)

* refact: cache validate info to avoid duplicate hash calculation
* change cache name & unify cache
  • Loading branch information
imbajin authored May 20, 2021
1 parent c0b481a commit 0fa8cf9
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class StandardAuthManager implements AuthManager {
private final HugeGraphParams graph;
private final EventListener eventListener;
private final Cache<Id, HugeUser> usersCache;
private final Cache<Id, String> pwdCache;

private final EntityManager<HugeUser> users;
private final EntityManager<HugeGroup> groups;
Expand All @@ -59,6 +60,7 @@ public StandardAuthManager(HugeGraphParams graph) {
this.graph = graph;
this.eventListener = this.listenChanges();
this.usersCache = this.cache("users");
this.pwdCache = this.cache("users_pwd");

this.users = new EntityManager<>(this.graph, HugeUser.P.USER,
HugeUser::fromVertex);
Expand All @@ -73,9 +75,9 @@ public StandardAuthManager(HugeGraphParams graph) {
HugeAccess::fromEdge);
}

private Cache<Id, HugeUser> cache(String prefix) {
private <V> Cache<Id, V> cache(String prefix) {
String name = prefix + "-" + this.graph.name();
Cache<Id, HugeUser> cache = CacheManager.instance().cache(name);
Cache<Id, V> cache = CacheManager.instance().cache(name);
cache.expire(CACHE_EXPIRE);
return cache;
}
Expand Down Expand Up @@ -120,6 +122,7 @@ private void initSchemaIfNeeded() {

private void invalidCache() {
this.usersCache.clear();
this.pwdCache.clear();
}

@Override
Expand Down Expand Up @@ -341,8 +344,17 @@ public HugeUser matchUser(String name, String password) {
E.checkArgumentNotNull(name, "User name can't be null");
E.checkArgumentNotNull(password, "User password can't be null");
HugeUser user = this.findUser(name);
if (user != null &&
StringEncoding.checkPassword(password, user.password())) {
if (user == null) {
return null;
}

Id id = IdGenerator.of(user.id());
if (password.equals(pwdCache.get(id))) {
return user;
}

if (StringEncoding.checkPassword(password, user.password())) {
pwdCache.update(id, password);
return user;
}
return null;
Expand Down

0 comments on commit 0fa8cf9

Please sign in to comment.