|
@@ -2,10 +2,12 @@ package cn.com.qmth.examcloud.app.service;
|
|
|
|
|
|
import cn.com.qmth.examcloud.app.core.exception.ApiException;
|
|
|
import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
|
|
|
+import cn.com.qmth.examcloud.app.core.utils.ThreadUtils;
|
|
|
import cn.com.qmth.examcloud.app.model.UserToken;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -13,32 +15,41 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static cn.com.qmth.examcloud.app.model.Constants.REDIS_KEY_PREFIX;
|
|
|
|
|
|
+/**
|
|
|
+ * @author: fengdesheng
|
|
|
+ * @since: 2018/7/16
|
|
|
+ */
|
|
|
@Service
|
|
|
public class RedisService {
|
|
|
private static Logger log = LoggerFactory.getLogger(RedisService.class);
|
|
|
@Autowired
|
|
|
- private StringRedisTemplate redisTemplate;
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<Object, Object> redisTemplate;
|
|
|
|
|
|
public void set(String key, String value) {
|
|
|
- redisTemplate.opsForValue().set(key, value);
|
|
|
+ stringRedisTemplate.opsForValue().set(key, value);
|
|
|
}
|
|
|
|
|
|
public void set(String key, String value, long seconds) {
|
|
|
- redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
|
|
|
+ stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
public String get(String key) {
|
|
|
- return redisTemplate.opsForValue().get(key);
|
|
|
+ return stringRedisTemplate.opsForValue().get(key);
|
|
|
}
|
|
|
|
|
|
public boolean exist(String key) {
|
|
|
- return redisTemplate.hasKey(key);
|
|
|
+ return stringRedisTemplate.hasKey(key);
|
|
|
}
|
|
|
|
|
|
public void delete(String key) {
|
|
|
- redisTemplate.delete(key);
|
|
|
+ stringRedisTemplate.delete(key);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 缓存用户登录信息
|
|
|
+ */
|
|
|
public void cacheUserToken(String key, UserToken value, long seconds) {
|
|
|
if (key == null) {
|
|
|
throw new ApiException("Key must be not null.");
|
|
@@ -46,6 +57,9 @@ public class RedisService {
|
|
|
this.set(REDIS_KEY_PREFIX + key, new JsonMapper().toJson(value), seconds);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取缓存中的用户登录信息
|
|
|
+ */
|
|
|
public UserToken getUserToken(String key) {
|
|
|
if (key == null) {
|
|
|
throw new ApiException("Key must be not null.");
|
|
@@ -57,4 +71,13 @@ public class RedisService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 初始化内部接口鉴权
|
|
|
+ */
|
|
|
+ public void initTraceRequest() {
|
|
|
+ String key = "C_" + ThreadUtils.getTraceID();
|
|
|
+ Long millis = System.currentTimeMillis();
|
|
|
+ stringRedisTemplate.opsForValue().set(key, millis.toString(), 60, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
}
|