|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
package cn.com.qmth.examcloud.app.service;
|
|
package cn.com.qmth.examcloud.app.service;
|
|
|
|
|
|
|
|
+import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -30,6 +31,14 @@ public class RedisService {
|
|
@Autowired
|
|
@Autowired
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
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) {
|
|
public void set(String key, String value) {
|
|
stringRedisTemplate.opsForValue().set(key, value);
|
|
stringRedisTemplate.opsForValue().set(key, value);
|
|
}
|
|
}
|
|
@@ -42,6 +51,18 @@ public class RedisService {
|
|
return stringRedisTemplate.opsForValue().get(key);
|
|
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) {
|
|
public boolean exist(String key) {
|
|
return stringRedisTemplate.hasKey(key);
|
|
return stringRedisTemplate.hasKey(key);
|
|
}
|
|
}
|