wangwei 6 vuotta sitten
vanhempi
commit
6b82b341df

+ 81 - 0
src/main/java/cn/com/qmth/examcloud/web/cache/ObjectRedisCacheProcessor.java

@@ -0,0 +1,81 @@
+package cn.com.qmth.examcloud.web.cache;
+
+import org.apache.commons.lang3.StringUtils;
+import org.assertj.core.util.Arrays;
+
+import cn.com.qmth.examcloud.web.redis.RedisClient;
+import cn.com.qmth.examcloud.web.support.SpringContextHolder;
+
+/**
+ * redis 缓存处理器
+ *
+ * @author WANGWEI
+ * @date 2019年5月10日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+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<? extends RandomCacheBean> c) {
+		String key = keyPrefix + StringUtils.join(Arrays.asList(propKeys), '_');
+
+		RandomCacheBean t = getRedisClient().get(key, c);
+		@SuppressWarnings("unchecked")
+		T ret = (T) t;
+		return ret;
+	}
+
+	/**
+	 * 取缓存对象(不存在时远程或本地加载)
+	 *
+	 * @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) {
+			getObjectRedisCacheTrigger().fire(appName, className, propKeys);
+			t = getRedisClient().get(key, c);
+		}
+		@SuppressWarnings("unchecked")
+		T ret = (T) t;
+		return ret;
+	}
+
+}