|
@@ -3,6 +3,7 @@ package cn.com.qmth.examcloud.web.cache;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.assertj.core.util.Arrays;
|
|
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.redis.RedisClient;
|
|
import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
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.
|
|
* @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
* @param <T>
|
|
* @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;
|
|
private RedisClient redisClient;
|
|
|
|
|
|
@@ -27,6 +28,8 @@ public abstract class RandomObjectCache<T extends RandomCacheBean> implements Ob
|
|
|
|
|
|
protected abstract String getKeyPrefix();
|
|
protected abstract String getKeyPrefix();
|
|
|
|
|
|
|
|
+ protected abstract int getTimeout();
|
|
|
|
+
|
|
protected String buildKey(Object... keys) {
|
|
protected String buildKey(Object... keys) {
|
|
String key = getKeyPrefix() + StringUtils.join(Arrays.asList(keys), '_');
|
|
String key = getKeyPrefix() + StringUtils.join(Arrays.asList(keys), '_');
|
|
return key;
|
|
return key;
|
|
@@ -53,14 +56,24 @@ public abstract class RandomObjectCache<T extends RandomCacheBean> implements Ob
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void remove(Object... keys) {
|
|
public void remove(Object... keys) {
|
|
- // TODO Auto-generated method stub
|
|
|
|
-
|
|
|
|
|
|
+ String key = buildKey(keys);
|
|
|
|
+ getRedisClient().delete(key);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void refresh(Object... keys) {
|
|
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);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|