|
@@ -0,0 +1,49 @@
|
|
|
+package cn.com.qmth.examcloud.tool.config;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class RedisClient {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ public void set(String key, Object value) {
|
|
|
+ redisTemplate.opsForValue().set(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void set(String key, Object value, long timeout) {
|
|
|
+ redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setForHash(String key, String hashKey, Object value, long timeout) {
|
|
|
+ redisTemplate.opsForHash().put(key, hashKey, value);
|
|
|
+ this.expire(key, timeout);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> T get(String key) {
|
|
|
+ return (T) redisTemplate.opsForValue().get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> T getForHash(String key, String hashKey) {
|
|
|
+ return (T) redisTemplate.opsForHash().get(key, hashKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void delete(String key) {
|
|
|
+ this.expire(key, 0);
|
|
|
+ // redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteForHash(String key, String hashKey) {
|
|
|
+ redisTemplate.opsForHash().delete(key, hashKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void expire(String key, long timeout) {
|
|
|
+ redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|