فهرست منبع

rename cache keys.

deason 2 سال پیش
والد
کامیت
40d84ddc92

+ 46 - 1
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/CacheConstants.java → examcloud-web/src/main/java/cn/com/qmth/examcloud/support/CacheConstants.java

@@ -17,7 +17,27 @@ public interface CacheConstants {
     /**
      * 缓存 - 锁前缀
      */
-    String CACHE_LOCK = "$LOCK:";
+    String LOCK_PREFIX = "$LOCK:";
+
+    /**
+     * 请求锁:{mapping}
+     */
+    String LOCK_SEQ_GLOBAL = LOCK_PREFIX + "SEQ_GLOBAL:";
+
+    /**
+     * 请求锁:{userKey}
+     */
+    String LOCK_SEQ_SESSION = LOCK_PREFIX + "SEQ_SESSION:";
+
+    /**
+     * 考试控制锁:{studentId}
+     */
+    String LOCK_EXAM_CONTROL = LOCK_PREFIX + "OE:EXAM_CONTROL:";
+
+    /**
+     * 考试交卷锁:{examRecordDataId}
+     */
+    String LOCK_HAND_IN_EXAM = LOCK_PREFIX + "OE:HAND_IN_EXAM:";
 
 
     /* #################### 基础模块 #################### */
@@ -93,6 +113,18 @@ public interface CacheConstants {
     String CACHE_B_STUDENT = "$B:STUDENT:";
 
 
+    /**
+     * 缓存 - 学习中心安全策略IP:{orgId}
+     */
+    String CACHE_B_SECURITY_IP = "$B:SECURITY_IP:";
+
+
+    /**
+     * 缓存 - 登录错误信息:{accountType}_{account}_{ip}
+     */
+    String CACHE_B_LOGIN_ERR = "$B:LOGIN_ERR:";
+
+
     /* #################### 考务模块 #################### */
 
     /**
@@ -231,4 +263,17 @@ public interface CacheConstants {
      */
     String CACHE_OE_SCORE_STATISTIC_STATUS = "$OE:SCORE_STATISTIC_STATUS:";
 
+
+    /* #################### Exchange模块 #################### */
+
+    /**
+     * 缓存 - 课程组下课程列表:{rootOrgId}_{courseGroupName}
+     */
+    String CACHE_EX_COURSE_LIST = "$EX:COURSE_LIST:";
+
+    /**
+     * 缓存 - 短信验证码:{phone}
+     */
+    String CACHE_EX_SMS = "$EX:SMS:";
+
 }

+ 0 - 15
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/Constants.java → examcloud-web/src/main/java/cn/com/qmth/examcloud/support/Constants.java

@@ -13,16 +13,6 @@ public interface Constants {
      */
     String OE_CODE_400 = "000400";
 
-    /**
-     * 考试控制锁_{studentId}
-     */
-    String EXAM_CONTROL_LOCK_PREFIX = "$oe_lock:student_id_";
-
-    /**
-     * 考试交卷锁前缀_{examRecordDataId}
-     */
-    String HAND_IN_EXAM_LOCK_PREFIX = "$oe_lock:hand_in_exam_";
-
     /**
      * 获取人脸活体检测基本信息前缀_{studentId}
      */
@@ -40,11 +30,6 @@ public interface Constants {
 
     String ERROR_MSG = "error_message";
 
-    /**
-     * 学生考试session前缀
-     */
-    String OE_STUDENT_EXAM_SESSION_PREFIX = "oe_student:exam_session_";
-
     String isTrue = "true";
 
     String isFalse = "false";

+ 81 - 88
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/helpers/SequenceLockHelper.java

@@ -1,13 +1,5 @@
 package cn.com.qmth.examcloud.web.helpers;
 
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.commons.lang.StringUtils;
-import org.assertj.core.util.Arrays;
-
-import com.google.common.collect.Lists;
-
 import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
 import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
 import cn.com.qmth.examcloud.web.exception.SequenceLockException;
@@ -15,99 +7,100 @@ import cn.com.qmth.examcloud.web.interceptor.SeqlockInterceptor;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.support.ServletUtil;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
+import org.assertj.core.util.Arrays;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
 /**
  * 并发顺序锁
  *
  * @author WANGWEI
  * @date 2019年2月22日
- * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  */
 public class SequenceLockHelper {
 
-	private static final Logger LOG = LoggerFactory.getLogger(SequenceLockHelper.class);
-
-	private static final String LOCK_PREFIX = "$_LOCK_C:";
-
-	private static RedisClient redisClient;
-
-	private static RedisClient getRedisClient() {
-		if (null == redisClient) {
-			redisClient = SpringContextHolder.getBean(RedisClient.class);
-		}
-
-		return redisClient;
-	}
-
-	/**
-	 * 获取顺序请求锁<br>
-	 * 
-	 * @param args
-	 * @throws SequenceLockException
-	 *             获取锁失败时抛出异常
-	 */
-	public static void getLock(Object... args) throws SequenceLockException {
-
-		String key = LOCK_PREFIX + StringUtils.join(Arrays.asList(args), "_");
-
-		int timeout = 60 * 2;
-
-		if (getRedisClient().setIfAbsent(key, ThreadLocalUtil.getTraceId(), timeout)) {
-			if (LOG.isDebugEnabled()) {
-				LOG.debug("locked");
-			}
-			@SuppressWarnings("unchecked")
-			List<String> keyList = (List<String>) ServletUtil.getRequest()
-					.getAttribute(HttpServletRequestAttribute.$_CUSTOM_SEQUENCE_LOCK.name());
-			if (null == keyList) {
-				keyList = Lists.newArrayList();
-			}
-			keyList.add(key);
-			ServletUtil.getRequest().setAttribute(
-					HttpServletRequestAttribute.$_CUSTOM_SEQUENCE_LOCK.name(), keyList);
-		} else {
-			Long expire = getRedisClient().getExpire(key, TimeUnit.SECONDS);
-			if (null == expire || expire > timeout) {
-				getRedisClient().delete(key);
-			}
-
-			throw new SequenceLockException("请求等待,请稍后重试!");
-		}
-	}
-	
-
-	/**
-	 * 释放锁<br>
-	 * 接口调用不要调用此方法释放锁,此锁会在拦截器<{@link SeqlockInterceptor}中释放.
-	 *
-	 * @author WANGWEI
-	 * @param args
-	 */
-	public static void releaseLock(Object... args) {
-		String key = LOCK_PREFIX + StringUtils.join(Arrays.asList(args), "_");
-		getRedisClient().expire((String) key, 100, TimeUnit.MILLISECONDS);
-		@SuppressWarnings("unchecked")
-		List<String> keyList = (List<String>) ServletUtil.getRequest()
-				.getAttribute(HttpServletRequestAttribute.$_CUSTOM_SEQUENCE_LOCK.name());
-		if (null != keyList) {
-			if (keyList.contains(key)) {
-				keyList.remove(key);
-			}
-		}
-	}
-	
-	   /**
+    private static final Logger LOG = LoggerFactory.getLogger(SequenceLockHelper.class);
+
+    private static RedisClient redisClient;
+
+    private static RedisClient getRedisClient() {
+        if (null == redisClient) {
+            redisClient = SpringContextHolder.getBean(RedisClient.class);
+        }
+
+        return redisClient;
+    }
+
+    /**
      * 获取顺序请求锁<br>
-     * 
+     *
      * @param args
-     * @throws SequenceLockException
-     *             获取锁失败时抛出异常
+     * @throws SequenceLockException 获取锁失败时抛出异常
+     */
+    public static void getLock(Object... args) throws SequenceLockException {
+
+        String key = StringUtils.join(Arrays.asList(args), "_");
+
+        int timeout = 60 * 2;
+
+        if (getRedisClient().setIfAbsent(key, ThreadLocalUtil.getTraceId(), timeout)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("locked");
+            }
+            @SuppressWarnings("unchecked")
+            List<String> keyList = (List<String>) ServletUtil.getRequest()
+                    .getAttribute(HttpServletRequestAttribute.$_CUSTOM_SEQUENCE_LOCK.name());
+            if (null == keyList) {
+                keyList = Lists.newArrayList();
+            }
+            keyList.add(key);
+            ServletUtil.getRequest().setAttribute(
+                    HttpServletRequestAttribute.$_CUSTOM_SEQUENCE_LOCK.name(), keyList);
+        } else {
+            Long expire = getRedisClient().getExpire(key, TimeUnit.SECONDS);
+            if (null == expire || expire > timeout) {
+                getRedisClient().delete(key);
+            }
+
+            throw new SequenceLockException("请求等待,请稍后重试!");
+        }
+    }
+
+
+    /**
+     * 释放锁<br>
+     * 接口调用不要调用此方法释放锁,此锁会在拦截器<{@link SeqlockInterceptor}中释放.
+     *
+     * @param args
+     * @author WANGWEI
+     */
+    public static void releaseLock(Object... args) {
+        String key = StringUtils.join(Arrays.asList(args), "_");
+        getRedisClient().expire((String) key, 100, TimeUnit.MILLISECONDS);
+        @SuppressWarnings("unchecked")
+        List<String> keyList = (List<String>) ServletUtil.getRequest()
+                .getAttribute(HttpServletRequestAttribute.$_CUSTOM_SEQUENCE_LOCK.name());
+        if (null != keyList) {
+            if (keyList.contains(key)) {
+                keyList.remove(key);
+            }
+        }
+    }
+
+    /**
+     * 获取顺序请求锁<br>
+     *
+     * @param args
+     * @throws SequenceLockException 获取锁失败时抛出异常
      */
     public static void getLockSimple(Object... args) throws SequenceLockException {
 
-        String key = LOCK_PREFIX + StringUtils.join(Arrays.asList(args), "_");
+        String key = StringUtils.join(Arrays.asList(args), "_");
 
         int timeout = 60 * 2;
 
@@ -120,16 +113,16 @@ public class SequenceLockHelper {
             throw new SequenceLockException("请求等待,请稍后重试!");
         }
     }
-    
+
     /**
      * 释放锁<br>
      * 接口调用不要调用此方法释放锁,此锁会在拦截器<{@link SeqlockInterceptor}中释放.
      *
-     * @author WANGWEI
      * @param args
+     * @author WANGWEI
      */
     public static void releaseLockSimple(Object... args) {
-        String key = LOCK_PREFIX + StringUtils.join(Arrays.asList(args), "_");
+        String key = StringUtils.join(Arrays.asList(args), "_");
         getRedisClient().delete(key);
     }
 

+ 4 - 8
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/interceptor/SeqlockInterceptor.java

@@ -2,6 +2,7 @@ package cn.com.qmth.examcloud.web.interceptor;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
 import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
+import cn.com.qmth.examcloud.support.CacheConstants;
 import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.support.ServletUtil;
@@ -19,11 +20,10 @@ import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 
 /**
- * 顺序锁拦截器
+ * 请求顺序锁拦截器
  *
  * @author WANGWEI
  * @date 2019年2月22日
- * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  */
 public class SeqlockInterceptor implements HandlerInterceptor {
 
@@ -34,10 +34,6 @@ public class SeqlockInterceptor implements HandlerInterceptor {
      */
     private RedisClient redisClient;
 
-    private static final String GLOBAL_LOCK_PREFIX = "$_LOCK_G:";
-
-    private static final String SESSION_LOCK_PREFIX = "$_LOCK_S:";
-
     private static final String LOCK_ATTRIBUTE = "$sequenceLock";
 
     /**
@@ -68,7 +64,7 @@ public class SeqlockInterceptor implements HandlerInterceptor {
             String mapping = (String) request.getAttribute(HttpServletRequestAttribute.$_MAPPING.name());
 
             if (null != globalSeqlock) {
-                String key = GLOBAL_LOCK_PREFIX + mapping;
+                String key = CacheConstants.LOCK_SEQ_GLOBAL + mapping;
 
                 if (redisClient.setIfAbsent(key, ThreadLocalUtil.getTraceId(), 60 * 5)) {
                     LOG.info("{} global locked", key);
@@ -84,7 +80,7 @@ public class SeqlockInterceptor implements HandlerInterceptor {
             } else if (null != sessionSeqlock) {
                 User user = (User) request.getAttribute(HttpServletRequestAttribute.$_ACCESS_USER.name());
 
-                String key = SESSION_LOCK_PREFIX + user.getKey();
+                String key = CacheConstants.LOCK_SEQ_SESSION + user.getKey();
 
                 if (redisClient.setIfAbsent(key, ThreadLocalUtil.getTraceId(), 60 * 5)) {
                     LOG.info("{} session locked", key);