Browse Source

rename cache keys.

deason 2 years ago
parent
commit
616b15431a

+ 5 - 0
examcloud-web/src/main/java/cn/com/qmth/examcloud/support/CacheConstants.java

@@ -19,6 +19,11 @@ public interface CacheConstants {
      */
     String CACHE_APP_LOGIN_SESSION = "$APP:";
 
+    /**
+     * 缓存 - 异常状态:{cacheKey}
+     */
+    String CACHE_EXCEPTION = "$CACHE_EXCEPTION:";
+
     /**
      * 缓存 - 锁前缀
      */

+ 115 - 116
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/cache/HashRedisCacheProcessor.java

@@ -1,15 +1,15 @@
 package cn.com.qmth.examcloud.web.cache;
 
-import java.util.concurrent.TimeUnit;
-
-import org.apache.commons.lang3.StringUtils;
-import org.assertj.core.util.Arrays;
-
 import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
 import cn.com.qmth.examcloud.commons.util.Util;
+import cn.com.qmth.examcloud.support.CacheConstants;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
+import org.apache.commons.lang3.StringUtils;
+import org.assertj.core.util.Arrays;
+
+import java.util.concurrent.TimeUnit;
 
 /**
  * redis Hash缓存处理器
@@ -19,116 +19,115 @@ import cn.com.qmth.examcloud.web.support.SpringContextHolder;
  */
 public class HashRedisCacheProcessor {
 
-	private static RedisClient redisClient;
-
-	private static HashRedisCacheTrigger hashRedisCacheTrigger;
-
-	private static RedisClient getRedisClient() {
-		if (null == redisClient) {
-			redisClient = SpringContextHolder.getBean(RedisClient.class);
-		}
-		return redisClient;
-	}
-
-	private static HashRedisCacheTrigger getHashRedisCacheTrigger() {
-		if (null == hashRedisCacheTrigger) {
-			hashRedisCacheTrigger = SpringContextHolder.getBean(HashRedisCacheTrigger.class);
-		}
-		return hashRedisCacheTrigger;
-	}
-
-	/**
-	 * 获取缓存对象
-	 *
-	 * @author 
-	 * @param keyPrefix
-	 * @param propKeys
-	 * @param c
-	 * @return
-	 */
-	public static <T> T get(String keyPrefix, Object[] propKeys,Object[] subpropKeys, Class<T> c) {
-		String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
-		String hashKey=StringUtils.join(Arrays.asList(subpropKeys), '_');
-		T t = getRedisClient().get(key,hashKey, c);
-		return t;
-	}
-
-	/**
-	 * 取缓存对象(不存在时远程或本地加载)<br>
-	 * 缓存失效时,只允许一个线程加载缓存,防止缓存击穿<br>
-	 * 缓存加载时长不得超过10秒,否则所有取缓存线程无等待抛出异常,只到缓存被正确加载<br>
-	 * 
-	 *
-	 * @author 
-	 * @param keyPrefix
-	 * @param propKeys
-	 * @param c
-	 * @param appName
-	 * @param className
-	 * @return
-	 */
-	public static <T> T get(String keyPrefix, Object[] propKeys,Object[] subpropKeys, Class<? extends RandomCacheBean> c,
-			String appName, String className) {
-
-		String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
-		String subKey=StringUtils.join(Arrays.asList(subpropKeys), '_');
-
-		RandomCacheBean t = getRedisClient().get(key,subKey, c);
-
-		if (null == t) {
-
-			int count = 0;
-
-			String cacheLock = "$_CACHE_LOCK:" + key+"_"+subKey;
-			String cacheException = "$_CACHE_EXCEPTION:" + key+"_"+subKey;
-
-			while (true) {
-				count++;
-
-				t = getRedisClient().get(key,subKey, c);
-
-				if (null != t) {
-					break;
-				}
-
-				Boolean locked = getRedisClient().setIfAbsent(cacheLock,
-						ThreadLocalUtil.getTraceId(), 30);
-
-				if (locked) {
-					try {
-					    getHashRedisCacheTrigger().fire(appName, className, propKeys,subpropKeys);
-						getRedisClient().delete(cacheException);
-						t = getRedisClient().get(key,subKey, c);
-						break;
-					} catch (Exception e) {
-						getRedisClient().set(cacheException, true, 60);
-						throw e;
-					} finally {
-						getRedisClient().delete(cacheLock);
-					}
-				} else {
-
-					if (null != getRedisClient().get(cacheException, Boolean.class)) {
-						throw new ExamCloudRuntimeException(
-								"exception happened when loading cache. key=" + key+" subKey="+subKey);
-					}
-
-					// 10秒内未加载完缓存,抛出异常
-					if (200 < count) {
-						throw new ExamCloudRuntimeException(
-								"fail to load cache in 10 seconds. key=" + key+" subKey="+subKey);
-					}
-
-					Util.sleep(TimeUnit.MILLISECONDS, 50);
-
-				}
-			}
-
-		}
-
-		@SuppressWarnings("unchecked")
-		T ret = (T) t;
-		return ret;
-	}
+    private static RedisClient redisClient;
+
+    private static HashRedisCacheTrigger hashRedisCacheTrigger;
+
+    private static RedisClient getRedisClient() {
+        if (null == redisClient) {
+            redisClient = SpringContextHolder.getBean(RedisClient.class);
+        }
+        return redisClient;
+    }
+
+    private static HashRedisCacheTrigger getHashRedisCacheTrigger() {
+        if (null == hashRedisCacheTrigger) {
+            hashRedisCacheTrigger = SpringContextHolder.getBean(HashRedisCacheTrigger.class);
+        }
+        return hashRedisCacheTrigger;
+    }
+
+    /**
+     * 获取缓存对象
+     *
+     * @param keyPrefix
+     * @param propKeys
+     * @param c
+     * @return
+     * @author
+     */
+    public static <T> T get(String keyPrefix, Object[] propKeys, Object[] subpropKeys, Class<T> c) {
+        String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
+        String hashKey = StringUtils.join(Arrays.asList(subpropKeys), '_');
+        T t = getRedisClient().get(key, hashKey, c);
+        return t;
+    }
+
+    /**
+     * 取缓存对象(不存在时远程或本地加载)<br>
+     * 缓存失效时,只允许一个线程加载缓存,防止缓存击穿<br>
+     * 缓存加载时长不得超过10秒,否则所有取缓存线程无等待抛出异常,只到缓存被正确加载<br>
+     *
+     * @param keyPrefix
+     * @param propKeys
+     * @param c
+     * @param appName
+     * @param className
+     * @return
+     * @author
+     */
+    public static <T> T get(String keyPrefix, Object[] propKeys, Object[] subpropKeys, Class<? extends RandomCacheBean> c,
+                            String appName, String className) {
+
+        String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
+        String subKey = StringUtils.join(Arrays.asList(subpropKeys), '_');
+
+        RandomCacheBean t = getRedisClient().get(key, subKey, c);
+
+        if (null == t) {
+
+            int count = 0;
+
+            String cacheLock = CacheConstants.LOCK_PREFIX + key + "_" + subKey;
+            String cacheException = CacheConstants.CACHE_EXCEPTION + key + "_" + subKey;
+
+            while (true) {
+                count++;
+
+                t = getRedisClient().get(key, subKey, c);
+
+                if (null != t) {
+                    break;
+                }
+
+                Boolean locked = getRedisClient().setIfAbsent(cacheLock,
+                        ThreadLocalUtil.getTraceId(), 30);
+
+                if (locked) {
+                    try {
+                        getHashRedisCacheTrigger().fire(appName, className, propKeys, subpropKeys);
+                        getRedisClient().delete(cacheException);
+                        t = getRedisClient().get(key, subKey, c);
+                        break;
+                    } catch (Exception e) {
+                        getRedisClient().set(cacheException, true, 60);
+                        throw e;
+                    } finally {
+                        getRedisClient().delete(cacheLock);
+                    }
+                } else {
+
+                    if (null != getRedisClient().get(cacheException, Boolean.class)) {
+                        throw new ExamCloudRuntimeException(
+                                "exception happened when loading cache. key=" + key + " subKey=" + subKey);
+                    }
+
+                    // 10秒内未加载完缓存,抛出异常
+                    if (200 < count) {
+                        throw new ExamCloudRuntimeException(
+                                "fail to load cache in 10 seconds. key=" + key + " subKey=" + subKey);
+                    }
+
+                    Util.sleep(TimeUnit.MILLISECONDS, 50);
+
+                }
+            }
+
+        }
+
+        @SuppressWarnings("unchecked")
+        T ret = (T) t;
+        return ret;
+    }
 
 }

+ 113 - 114
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/cache/ObjectRedisCacheProcessor.java

@@ -1,15 +1,15 @@
 package cn.com.qmth.examcloud.web.cache;
 
-import java.util.concurrent.TimeUnit;
-
-import org.apache.commons.lang3.StringUtils;
-import org.assertj.core.util.Arrays;
-
 import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
 import cn.com.qmth.examcloud.commons.util.Util;
+import cn.com.qmth.examcloud.support.CacheConstants;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
+import org.apache.commons.lang3.StringUtils;
+import org.assertj.core.util.Arrays;
+
+import java.util.concurrent.TimeUnit;
 
 /**
  * redis 缓存处理器
@@ -20,114 +20,113 @@ import cn.com.qmth.examcloud.web.support.SpringContextHolder;
  */
 public class ObjectRedisCacheProcessor {
 
-	private static RedisClient redisClient;
-
-	private static ObjectRedisCacheTrigger objectRedisCacheTrigger;
-
-	private static RedisClient getRedisClient() {
-		if (null == redisClient) {
-			redisClient = SpringContextHolder.getBean(RedisClient.class);
-		}
-		return redisClient;
-	}
-
-	private static ObjectRedisCacheTrigger getObjectRedisCacheTrigger() {
-		if (null == objectRedisCacheTrigger) {
-			objectRedisCacheTrigger = SpringContextHolder.getBean(ObjectRedisCacheTrigger.class);
-		}
-		return objectRedisCacheTrigger;
-	}
-
-	/**
-	 * 获取缓存对象
-	 *
-	 * @author WANGWEI
-	 * @param keyPrefix
-	 * @param propKeys
-	 * @param c
-	 * @return
-	 */
-	public static <T> T get(String keyPrefix, Object[] propKeys, Class<T> c) {
-		String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
-		T t = getRedisClient().get(key, c);
-		return t;
-	}
-
-	/**
-	 * 取缓存对象(不存在时远程或本地加载)<br>
-	 * 缓存失效时,只允许一个线程加载缓存,防止缓存击穿<br>
-	 * 缓存加载时长不得超过10秒,否则所有取缓存线程无等待抛出异常,只到缓存被正确加载<br>
-	 * 
-	 *
-	 * @author WANGWEI
-	 * @param keyPrefix
-	 * @param propKeys
-	 * @param c
-	 * @param appName
-	 * @param className
-	 * @return
-	 */
-	public static <T> T get(String keyPrefix, Object[] propKeys, Class<? extends RandomCacheBean> c,
-			String appName, String className) {
-
-		String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
-
-		RandomCacheBean t = getRedisClient().get(key, c);
-
-		if (null == t) {
-
-			int count = 0;
-
-			String cacheLock = "$_CACHE_LOCK:" + key;
-			String cacheException = "$_CACHE_EXCEPTION:" + key;
-
-			while (true) {
-				count++;
-
-				t = getRedisClient().get(key, c);
-
-				if (null != t) {
-					break;
-				}
-
-				Boolean locked = getRedisClient().setIfAbsent(cacheLock,
-						ThreadLocalUtil.getTraceId(), 30);
-
-				if (locked) {
-					try {
-						getObjectRedisCacheTrigger().fire(appName, className, propKeys);
-						getRedisClient().delete(cacheException);
-						t = getRedisClient().get(key, c);
-						break;
-					} catch (Exception e) {
-						getRedisClient().set(cacheException, true, 60);
-						throw e;
-					} finally {
-						getRedisClient().delete(cacheLock);
-					}
-				} else {
-
-					if (null != getRedisClient().get(cacheException, Boolean.class)) {
-						throw new ExamCloudRuntimeException(
-								"exception happened when loading cache. key=" + key);
-					}
-
-					// 10秒内未加载完缓存,抛出异常
-					if (200 < count) {
-						throw new ExamCloudRuntimeException(
-								"fail to load cache in 10 seconds. key=" + key);
-					}
-
-					Util.sleep(TimeUnit.MILLISECONDS, 50);
-
-				}
-			}
-
-		}
-
-		@SuppressWarnings("unchecked")
-		T ret = (T) t;
-		return ret;
-	}
+    private static RedisClient redisClient;
+
+    private static ObjectRedisCacheTrigger objectRedisCacheTrigger;
+
+    private static RedisClient getRedisClient() {
+        if (null == redisClient) {
+            redisClient = SpringContextHolder.getBean(RedisClient.class);
+        }
+        return redisClient;
+    }
+
+    private static ObjectRedisCacheTrigger getObjectRedisCacheTrigger() {
+        if (null == objectRedisCacheTrigger) {
+            objectRedisCacheTrigger = SpringContextHolder.getBean(ObjectRedisCacheTrigger.class);
+        }
+        return objectRedisCacheTrigger;
+    }
+
+    /**
+     * 获取缓存对象
+     *
+     * @param keyPrefix
+     * @param propKeys
+     * @param c
+     * @return
+     * @author WANGWEI
+     */
+    public static <T> T get(String keyPrefix, Object[] propKeys, Class<T> c) {
+        String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
+        T t = getRedisClient().get(key, c);
+        return t;
+    }
+
+    /**
+     * 取缓存对象(不存在时远程或本地加载)<br>
+     * 缓存失效时,只允许一个线程加载缓存,防止缓存击穿<br>
+     * 缓存加载时长不得超过10秒,否则所有取缓存线程无等待抛出异常,只到缓存被正确加载<br>
+     *
+     * @param keyPrefix
+     * @param propKeys
+     * @param c
+     * @param appName
+     * @param className
+     * @return
+     * @author WANGWEI
+     */
+    public static <T> T get(String keyPrefix, Object[] propKeys, Class<? extends RandomCacheBean> c,
+                            String appName, String className) {
+
+        String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
+
+        RandomCacheBean t = getRedisClient().get(key, c);
+
+        if (null == t) {
+
+            int count = 0;
+
+            String cacheLock = CacheConstants.LOCK_PREFIX + key;
+            String cacheException = CacheConstants.CACHE_EXCEPTION + key;
+
+            while (true) {
+                count++;
+
+                t = getRedisClient().get(key, c);
+
+                if (null != t) {
+                    break;
+                }
+
+                Boolean locked = getRedisClient().setIfAbsent(cacheLock,
+                        ThreadLocalUtil.getTraceId(), 30);
+
+                if (locked) {
+                    try {
+                        getObjectRedisCacheTrigger().fire(appName, className, propKeys);
+                        getRedisClient().delete(cacheException);
+                        t = getRedisClient().get(key, c);
+                        break;
+                    } catch (Exception e) {
+                        getRedisClient().set(cacheException, true, 60);
+                        throw e;
+                    } finally {
+                        getRedisClient().delete(cacheLock);
+                    }
+                } else {
+
+                    if (null != getRedisClient().get(cacheException, Boolean.class)) {
+                        throw new ExamCloudRuntimeException(
+                                "exception happened when loading cache. key=" + key);
+                    }
+
+                    // 10秒内未加载完缓存,抛出异常
+                    if (200 < count) {
+                        throw new ExamCloudRuntimeException(
+                                "fail to load cache in 10 seconds. key=" + key);
+                    }
+
+                    Util.sleep(TimeUnit.MILLISECONDS, 50);
+
+                }
+            }
+
+        }
+
+        @SuppressWarnings("unchecked")
+        T ret = (T) t;
+        return ret;
+    }
 
 }

+ 0 - 50
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/cache/RandomCacheVersionHelper.java

@@ -1,50 +0,0 @@
-package cn.com.qmth.examcloud.web.cache;
-
-import cn.com.qmth.examcloud.web.redis.RedisClient;
-import cn.com.qmth.examcloud.web.support.SpringContextHolder;
-
-/**
- * 随机缓存版本控制
- *
- * @author WANGWEI
- * @date 2019年10月25日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class RandomCacheVersionHelper {
-
-	private static RedisClient redisClient;
-
-	private static RedisClient getRedisClient() {
-		if (null == redisClient) {
-			redisClient = SpringContextHolder.getBean(RedisClient.class);
-		}
-		return redisClient;
-	}
-
-	/**
-	 * 设置版本
-	 *
-	 * @author WANGWEI
-	 * @param c
-	 * @param version
-	 * @param timeout
-	 */
-	public static void setVersion(Class<? extends RandomCacheBean> c, String version, int timeout) {
-		String key = "$_V_" + c.getCanonicalName();
-		getRedisClient().set(key, version, timeout);
-	}
-
-	/**
-	 * 获取版本
-	 *
-	 * @author WANGWEI
-	 * @param c
-	 * @param version
-	 * @return
-	 */
-	public static String getVersion(Class<? extends RandomCacheBean> c, String version) {
-		String key = "$_V_" + c.getCanonicalName();
-		return getRedisClient().get(key, String.class);
-	}
-
-}