From 7d80fd94787be85c42b525464371286164e5a326 Mon Sep 17 00:00:00 2001 From: imbajin Date: Wed, 19 May 2021 10:55:20 +0800 Subject: [PATCH 1/2] refact: cache validate info to avoid duplicate hash calculation --- .../hugegraph/auth/StandardAuthManager.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java index 21cc1cac0a..8515dce0b9 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java @@ -45,6 +45,7 @@ public class StandardAuthManager implements AuthManager { private final HugeGraphParams graph; private final EventListener eventListener; private final Cache usersCache; + private final Cache validateCache; private final EntityManager users; private final EntityManager groups; @@ -59,6 +60,8 @@ public StandardAuthManager(HugeGraphParams graph) { this.graph = graph; this.eventListener = this.listenChanges(); this.usersCache = this.cache("users"); + this.validateCache = CacheManager.instance().cache("validate_pwd"); + this.validateCache.expire(CACHE_EXPIRE); this.users = new EntityManager<>(this.graph, HugeUser.P.USER, HugeUser::fromVertex); @@ -120,6 +123,7 @@ private void initSchemaIfNeeded() { private void invalidCache() { this.usersCache.clear(); + this.validateCache.clear(); } @Override @@ -341,8 +345,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(name); + if (password.equals(validateCache.get(id))) { + return user; + } + + if (StringEncoding.checkPassword(password, user.password())) { + validateCache.update(id, password); return user; } return null; From b1671907bb03e95619a25e97d5eed02cc4200cd0 Mon Sep 17 00:00:00 2001 From: imbajin Date: Thu, 20 May 2021 14:40:06 +0800 Subject: [PATCH 2/2] change cache name & unify cache --- .../hugegraph/auth/StandardAuthManager.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java index 8515dce0b9..e5f013b9c8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/StandardAuthManager.java @@ -45,7 +45,7 @@ public class StandardAuthManager implements AuthManager { private final HugeGraphParams graph; private final EventListener eventListener; private final Cache usersCache; - private final Cache validateCache; + private final Cache pwdCache; private final EntityManager users; private final EntityManager groups; @@ -60,8 +60,7 @@ public StandardAuthManager(HugeGraphParams graph) { this.graph = graph; this.eventListener = this.listenChanges(); this.usersCache = this.cache("users"); - this.validateCache = CacheManager.instance().cache("validate_pwd"); - this.validateCache.expire(CACHE_EXPIRE); + this.pwdCache = this.cache("users_pwd"); this.users = new EntityManager<>(this.graph, HugeUser.P.USER, HugeUser::fromVertex); @@ -76,9 +75,9 @@ public StandardAuthManager(HugeGraphParams graph) { HugeAccess::fromEdge); } - private Cache cache(String prefix) { + private Cache cache(String prefix) { String name = prefix + "-" + this.graph.name(); - Cache cache = CacheManager.instance().cache(name); + Cache cache = CacheManager.instance().cache(name); cache.expire(CACHE_EXPIRE); return cache; } @@ -123,7 +122,7 @@ private void initSchemaIfNeeded() { private void invalidCache() { this.usersCache.clear(); - this.validateCache.clear(); + this.pwdCache.clear(); } @Override @@ -349,13 +348,13 @@ public HugeUser matchUser(String name, String password) { return null; } - Id id = IdGenerator.of(name); - if (password.equals(validateCache.get(id))) { + Id id = IdGenerator.of(user.id()); + if (password.equals(pwdCache.get(id))) { return user; } if (StringEncoding.checkPassword(password, user.password())) { - validateCache.update(id, password); + pwdCache.update(id, password); return user; } return null;