瀏覽代碼

小优化

xiaochan 1 年之前
父節點
當前提交
bda81e7545

+ 6 - 11
src/main/java/thyyxxk/webserver/service/RedisServer.java

@@ -1,6 +1,7 @@
 package thyyxxk.webserver.service;
 
 
+import cn.hutool.core.util.StrUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
@@ -16,6 +17,10 @@ public class RedisServer {
         T accept();
     }
 
+    public static String USER_INFO(String code) {
+        return StrUtil.format("HIS_USERINFO-{}", code);
+    }
+
     private final RedisTemplate<String, Object> redisTemplate;
 
     public RedisServer(RedisTemplate<String, Object> redisTemplate) {
@@ -53,19 +58,9 @@ public class RedisServer {
 
     @SuppressWarnings("unchecked")
     public <T> T getData(String key, CyConsumer<T> function) {
-        Object o = redisTemplate.opsForValue().get(key);
-        if (o == null) {
-            if (function == null) {
-                throw new RuntimeException("function should not be null");
-            }
-            T data = function.accept();
-            setData(key, data);
-            return data;
-        }
-        return (T) o;
+        return getData(key, (k) -> function.accept());
     }
 
-
     public Boolean delData(String key) {
         return redisTemplate.delete(key);
     }

+ 4 - 3
src/main/java/thyyxxk/webserver/service/redislike/RedisLikeService.java

@@ -32,6 +32,7 @@ import java.util.stream.Collectors;
 @Component
 public class RedisLikeService {
 
+
     private final RedisServer redisServer;
 
     public final static String SCROLLING_MESSAGES = "scrollingMessages";
@@ -222,7 +223,7 @@ public class RedisLikeService {
     }
 
     public void setUserInfo(UserInfo us) {
-        redisServer.setData(StrUtil.format(US_INFO_KEY, us.getCode()), us);
+        redisServer.setData(RedisServer.USER_INFO(us.getCode()), us);
     }
 
     public Set<String> getDept(boolean needPartTimeDept, UserInfo userInfo) {
@@ -274,7 +275,7 @@ public class RedisLikeService {
         if (StringUtil.invalidValue(code)) {
             return null;
         }
-        return redisServer.getData(StrUtil.format(US_INFO_KEY, code), () -> dbUserInfo(code));
+        return redisServer.getData(RedisServer.USER_INFO(code), () -> dbUserInfo(code));
     }
 
     public UserInfo getUserInfoByToken() {
@@ -328,7 +329,7 @@ public class RedisLikeService {
     }
 
     public void updateUserInfo(String code) {
-        String key = StrUtil.format(US_INFO_KEY, code);
+        String key = RedisServer.USER_INFO(code);
         UserInfo us = redisServer.getData(key);
         if (us == null) {
             return;

+ 0 - 2
src/main/java/thyyxxk/webserver/service/settings/SettingsService.java

@@ -77,8 +77,6 @@ public class SettingsService {
         if (param.getNewPassword().trim().isEmpty()) {
             return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "新密码不能包含空格,请检查!");
         }
-
-
         dao.updatePassword(code, newPwd);
         redis.handleUserPwdChanged(code, newPwd);
         return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "密码修改成功。");