deason 7 жил өмнө
parent
commit
29640cf0d6

+ 2 - 0
src/main/java/cn/com/qmth/examcloud/app/core/config/RedisConfig.java

@@ -9,6 +9,7 @@ package cn.com.qmth.examcloud.app.core.config;
 
 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -31,6 +32,7 @@ public class RedisConfig {
         ObjectMapper objectMapper = new ObjectMapper();
         objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
         objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
+        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
         jacksonSerializer.setObjectMapper(objectMapper);
         redisTemplate.setValueSerializer(jacksonSerializer);
         redisTemplate.setKeySerializer(new StringRedisSerializer());

+ 21 - 0
src/main/java/cn/com/qmth/examcloud/app/service/RedisService.java

@@ -7,6 +7,7 @@
 
 package cn.com.qmth.examcloud.app.service;
 
+import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,14 @@ public class RedisService {
     @Autowired
     private RedisTemplate<String, Object> redisTemplate;
 
+    public void setObj(String key, Object value) {
+        redisTemplate.opsForValue().set(key, value);
+    }
+
+    public void setObj(String key, Object value, long seconds) {
+        redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
+    }
+
     public void set(String key, String value) {
         stringRedisTemplate.opsForValue().set(key, value);
     }
@@ -42,6 +51,18 @@ public class RedisService {
         return stringRedisTemplate.opsForValue().get(key);
     }
 
+    public <T> T getObj(String key, Class<T> clazz) {
+        return (T) redisTemplate.opsForValue().get(key);
+    }
+
+    public <T> T fromJson(String key, Class<T> clazz) {
+        String jsonStr = stringRedisTemplate.opsForValue().get(key);
+        if (jsonStr != null) {
+            return new JsonMapper().fromJson(jsonStr, clazz);
+        }
+        return null;
+    }
+
     public boolean exist(String key) {
         return stringRedisTemplate.hasKey(key);
     }

+ 3 - 6
src/main/java/cn/com/qmth/examcloud/app/service/impl/UserAuthServiceImpl.java

@@ -142,7 +142,8 @@ public class UserAuthServiceImpl implements UserAuthService {
         if (StringUtils.isEmpty(key)) {
             throw new ApiException("Key must be not empty.");
         }
-        redisService.set(APP_SESSION_USER_KEY_PREFIX + key, new JsonMapper().toJson(loginInfo), APP_SESSION_EXPIRE_TIME);
+        String jsonStr = new JsonMapper().toJson(loginInfo);
+        redisService.set(APP_SESSION_USER_KEY_PREFIX + key, jsonStr, APP_SESSION_EXPIRE_TIME);
     }
 
     /**
@@ -153,11 +154,7 @@ public class UserAuthServiceImpl implements UserAuthService {
         if (StringUtils.isEmpty(key)) {
             throw new ApiException("Key must be not empty.");
         }
-        String loginInfo = redisService.get(APP_SESSION_USER_KEY_PREFIX + key);
-        if (loginInfo != null) {
-            return new JsonMapper().fromJson(loginInfo, LoginInfo.class);
-        }
-        return null;
+        return redisService.fromJson(APP_SESSION_USER_KEY_PREFIX + key, LoginInfo.class);
     }
 
     /**

+ 10 - 4
src/test/java/cn/com/qmth/examcloud/app/ServiceTest.java

@@ -1,6 +1,9 @@
 package cn.com.qmth.examcloud.app;
 
+import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
 import cn.com.qmth.examcloud.app.core.utils.StrUtils;
+import cn.com.qmth.examcloud.app.model.LoginInfo;
+import cn.com.qmth.examcloud.app.model.UserInfo;
 import cn.com.qmth.examcloud.app.service.RedisService;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -19,11 +22,14 @@ public class ServiceTest {
     @Test
     public void demo() throws Exception {
         String key = "U_C_109_18809";
-        Object obj = redisService.getRedisTemplate().opsForValue().get(key);
-        System.out.println(obj);
+        redisService.setObj(key, new UserInfo(), 30);
 
-        String loginInfo = redisService.get(APP_SESSION_USER_KEY_PREFIX + StrUtils.md5Key(key));
-        System.out.println(loginInfo);
+        UserInfo user = redisService.getObj(key, UserInfo.class);
+        System.out.println(new JsonMapper().toJson(user));
+
+        key = APP_SESSION_USER_KEY_PREFIX + StrUtils.md5Key(key);
+        LoginInfo loginInfo = redisService.fromJson(key, LoginInfo.class);
+        System.out.println(new JsonMapper().toJson(loginInfo));
     }
 
 }