WANG il y a 6 ans
Parent
commit
a55acff9bd

+ 0 - 13
src/main/java/cn/com/qmth/examcloud/web/cache/RandomCacheBean.java

@@ -6,17 +6,4 @@ public abstract class RandomCacheBean implements Serializable {
 
 	private static final long serialVersionUID = 5409197052989051020L;
 
-	/**
-	 * 是否存在
-	 */
-	private Boolean existing;
-
-	public Boolean getExisting() {
-		return existing;
-	}
-
-	public void setExisting(Boolean existing) {
-		this.existing = existing;
-	}
-
 }

+ 18 - 5
src/main/java/cn/com/qmth/examcloud/web/cache/RandomObjectCache.java → src/main/java/cn/com/qmth/examcloud/web/cache/RandomObjectRedisCache.java

@@ -3,6 +3,7 @@ package cn.com.qmth.examcloud.web.cache;
 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.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
 
@@ -14,7 +15,7 @@ import cn.com.qmth.examcloud.web.support.SpringContextHolder;
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  * @param <T>
  */
-public abstract class RandomObjectCache<T extends RandomCacheBean> implements ObjectCache<T> {
+public abstract class RandomObjectRedisCache<T extends RandomCacheBean> implements ObjectCache<T> {
 
 	private RedisClient redisClient;
 
@@ -27,6 +28,8 @@ public abstract class RandomObjectCache<T extends RandomCacheBean> implements Ob
 
 	protected abstract String getKeyPrefix();
 
+	protected abstract int getTimeout();
+
 	protected String buildKey(Object... keys) {
 		String key = getKeyPrefix() + StringUtils.join(Arrays.asList(keys), '_');
 		return key;
@@ -53,14 +56,24 @@ public abstract class RandomObjectCache<T extends RandomCacheBean> implements Ob
 
 	@Override
 	public void remove(Object... keys) {
-		// TODO Auto-generated method stub
-
+		String key = buildKey(keys);
+		getRedisClient().delete(key);
 	}
 
 	@Override
 	public void refresh(Object... keys) {
-		// TODO Auto-generated method stub
-
+		String key = buildKey(keys);
+		T t = null;
+		try {
+			t = loadFromResource(keys);
+		} catch (Exception e) {
+			throw new ExamCloudRuntimeException("fail to load data. key=" + key, e);
+		}
+		if (null != t) {
+			getRedisClient().set(key, t, getTimeout());
+		} else {
+			getRedisClient().delete(key);
+		}
 	}
 
 }