|
@@ -0,0 +1,52 @@
|
|
|
|
+package com.qmth.exam.reserve.cache;
|
|
|
|
+
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+public class RedisClient {
|
|
|
|
+
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(RedisClient.class);
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
+
|
|
|
|
+ // @Resource
|
|
|
|
+ // private RedissonClient redissonClient;
|
|
|
|
+
|
|
|
|
+ public void set(String key, Object value) {
|
|
|
|
+ redisTemplate.opsForValue().set(key, value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void set(String key, Object value, long timeout, TimeUnit timeUnit) {
|
|
|
|
+ redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public <T> T get(String key, Class<T> clazz) {
|
|
|
|
+ return (T) redisTemplate.opsForValue().get(key);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public <T> T get(String key, Class<T> clazz, long timeout, TimeUnit timeUnit) {
|
|
|
|
+ T object = (T) redisTemplate.opsForValue().get(key);
|
|
|
|
+ expire(key, timeout, timeUnit);
|
|
|
|
+ return object;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void expire(String key, long timeout, TimeUnit timeUnit) {
|
|
|
|
+ redisTemplate.expire(key, timeout, timeUnit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void delete(String key) {
|
|
|
|
+ redisTemplate.expire(key, 0, TimeUnit.SECONDS);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean exist(String key) {
|
|
|
|
+ return redisTemplate.hasKey(key);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|