deason hai 1 ano
pai
achega
6519bc54c5

+ 28 - 0
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -0,0 +1,28 @@
+package com.qmth.exam.reserve.cache;
+
+/**
+ * 缓存相关常量
+ */
+public interface CacheConstants {
+
+    /**
+     * 缓存策略:禁止空值
+     */
+    String DISABLE_NULL_VALUE = "#result == null";
+
+    /**
+     * 用户登录会话缓存前缀
+     */
+    String CACHE_SESSION_PREFIX = "SESSION:";
+
+    /**
+     * 用户登录标识_{userId}
+     */
+    String CACHE_USER_LOGIN = "U_";
+
+    /**
+     * 学生登录标识_{studentId}
+     */
+    String CACHE_STUDENT_LOGIN = "S_";
+
+}

+ 20 - 0
src/main/java/com/qmth/exam/reserve/cache/CacheExpireTimeConfig.java

@@ -0,0 +1,20 @@
+package com.qmth.exam.reserve.cache;
+
+import com.qmth.boot.core.cache.service.CustomizeCacheConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 缓存过期时间配置
+ */
+@Configuration
+public class CacheExpireTimeConfig {
+
+    @Bean
+    public CustomizeCacheConfiguration customizeCacheConfiguration() {
+        return registration -> {
+            // registration.setCacheConfig(CacheConstants.XXX, Duration.ofMinutes(5));
+        };
+    }
+
+}

+ 52 - 0
src/main/java/com/qmth/exam/reserve/cache/RedisClient.java

@@ -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);
+    }
+
+}

+ 13 - 0
src/main/java/com/qmth/exam/reserve/cache/impl/CacheManager.java

@@ -0,0 +1,13 @@
+package com.qmth.exam.reserve.cache.impl;
+
+import com.qmth.exam.reserve.cache.CacheConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CacheManager implements CacheConstants {
+
+    private static final Logger log = LoggerFactory.getLogger(CacheManager.class);
+
+}