From e4e9135bafc155928500d40d099ad323aa80d9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=BF=E5=98=89=E4=BF=8A?= <47295240+cmgyqjj@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:26:42 +0800 Subject: [PATCH 1/8] Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java Co-authored-by: crossoverJie --- .../cim/route/service/impl/UserInfoCacheServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index 50ffbd14..abd633ac 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -34,7 +34,7 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { @Autowired private RedisTemplate redisTemplate ; - @Autowired + @Resource(name = "userInfoCache") private LoadingCache USER_INFO_MAP; @Override From 03712e5906a7b0bcecab6cfdfa44d458beb111e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=BF=E5=98=89=E4=BF=8A?= <47295240+cmgyqjj@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:26:52 +0800 Subject: [PATCH 2/8] Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java Co-authored-by: crossoverJie --- .../main/java/com/crossoverjie/cim/route/config/BeanConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java index 5414cd84..5461d219 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java @@ -115,7 +115,7 @@ public RouteHandle buildRouteHandle() throws Exception { } @Bean - public LoadingCache USER_INFO_MAP(RedisTemplate redisTemplate) { + public LoadingCache userInfoCache(RedisTemplate redisTemplate) { return CacheBuilder.newBuilder() .initialCapacity(64) .maximumSize(1024) From 6b1a1fbb6bf43ae897129e7741ad98076298a269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=BF=E5=98=89=E4=BF=8A?= <47295240+cmgyqjj@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:26:59 +0800 Subject: [PATCH 3/8] Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java Co-authored-by: crossoverJie --- .../main/java/com/crossoverjie/cim/route/config/BeanConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java index 5461d219..44c0450c 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java @@ -114,7 +114,7 @@ public RouteHandle buildRouteHandle() throws Exception { } - @Bean + @Bean("userInfoCache") public LoadingCache userInfoCache(RedisTemplate redisTemplate) { return CacheBuilder.newBuilder() .initialCapacity(64) From 65d0c2daf6bde1aff6b428a1592190d08d0153fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=BF=E5=98=89=E4=BF=8A?= <47295240+cmgyqjj@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:27:06 +0800 Subject: [PATCH 4/8] Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java Co-authored-by: crossoverJie --- .../main/java/com/crossoverjie/cim/route/config/BeanConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java index 44c0450c..fd120eeb 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java @@ -121,7 +121,7 @@ public LoadingCache userInfoCache(RedisTemplate() { + .build(new CacheLoader<>() { @Override public CIMUserInfo load(Long userId) throws Exception { CIMUserInfo cimUserInfo = null; From 39615429eb4dca7bd3a5c3fe39d6bae8170ab75e Mon Sep 17 00:00:00 2001 From: cmgyqjj Date: Sun, 22 Sep 2024 17:28:40 +0800 Subject: [PATCH 5/8] feature_Support_LRU_User_Info_Cache --- .../cim/route/service/impl/UserInfoCacheServiceImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index abd633ac..c28d49fd 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -5,6 +5,7 @@ import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; +import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; From 6e08eb7022e3b04f94833fd799f7dab283028ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=BF=E5=98=89=E4=BF=8A?= <47295240+cmgyqjj@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:30:09 +0800 Subject: [PATCH 6/8] Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java Co-authored-by: crossoverJie --- .../main/java/com/crossoverjie/cim/route/config/BeanConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java index fd120eeb..f486354d 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java @@ -127,7 +127,7 @@ public CIMUserInfo load(Long userId) throws Exception { CIMUserInfo cimUserInfo = null; String sendUserName = redisTemplate.opsForValue().get(ACCOUNT_PREFIX + userId); if (sendUserName == null) { - sendUserName = "unLoginUser"; + return null; } cimUserInfo = new CIMUserInfo(userId, sendUserName); return cimUserInfo; From a727b1b108f9f3ac9967cc8e85dc4fe70c6ec73a Mon Sep 17 00:00:00 2001 From: cmgyqjj Date: Sun, 22 Sep 2024 17:30:31 +0800 Subject: [PATCH 7/8] feature_Support_LRU_User_Info_Cache --- .../cim/route/service/impl/UserInfoCacheServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index c28d49fd..eadbb52f 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -36,12 +36,12 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { private RedisTemplate redisTemplate ; @Resource(name = "userInfoCache") - private LoadingCache USER_INFO_MAP; + private LoadingCache userInfoMap; @Override public Optional loadUserInfoByUserId(Long userId) { //Retrieve user information using a second-level cache. - CIMUserInfo cimUserInfo = USER_INFO_MAP.getUnchecked(userId); + CIMUserInfo cimUserInfo = userInfoMap.getUnchecked(userId); return Optional.ofNullable(cimUserInfo); } From 4ed3e18bbe680d3316b73b47c731fe29aa297e96 Mon Sep 17 00:00:00 2001 From: cmgyqjj Date: Sun, 22 Sep 2024 17:42:34 +0800 Subject: [PATCH 8/8] feature_Support_LRU_User_Info_Cache --- .../com/crossoverjie/cim/route/config/BeanConfig.java | 9 +++++---- .../cim/route/service/impl/UserInfoCacheServiceImpl.java | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java index f486354d..8188b120 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java @@ -27,6 +27,7 @@ import java.lang.reflect.Method; import java.time.Duration; +import java.util.Optional; import java.util.concurrent.TimeUnit; import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX; @@ -115,7 +116,7 @@ public RouteHandle buildRouteHandle() throws Exception { } @Bean("userInfoCache") - public LoadingCache userInfoCache(RedisTemplate redisTemplate) { + public LoadingCache> userInfoCache(RedisTemplate redisTemplate) { return CacheBuilder.newBuilder() .initialCapacity(64) .maximumSize(1024) @@ -123,14 +124,14 @@ public LoadingCache userInfoCache(RedisTemplate() { @Override - public CIMUserInfo load(Long userId) throws Exception { + public Optional load(Long userId) throws Exception { CIMUserInfo cimUserInfo = null; String sendUserName = redisTemplate.opsForValue().get(ACCOUNT_PREFIX + userId); if (sendUserName == null) { - return null; + return Optional.empty(); } cimUserInfo = new CIMUserInfo(userId, sendUserName); - return cimUserInfo; + return Optional.of(cimUserInfo); } }); } diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index eadbb52f..76a1e8b7 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -36,13 +36,13 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { private RedisTemplate redisTemplate ; @Resource(name = "userInfoCache") - private LoadingCache userInfoMap; + private LoadingCache> userInfoMap; @Override public Optional loadUserInfoByUserId(Long userId) { //Retrieve user information using a second-level cache. - CIMUserInfo cimUserInfo = userInfoMap.getUnchecked(userId); - return Optional.ofNullable(cimUserInfo); + Optional cimUserInfo = userInfoMap.getUnchecked(userId); + return cimUserInfo; } @Override