Commit 59bd9a1b by WeiCong

优化缓存模块

parent 4f89efa8
...@@ -4,6 +4,8 @@ import org.hibernate.Criteria; ...@@ -4,6 +4,8 @@ import org.hibernate.Criteria;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.internal.QueryImpl; import org.hibernate.internal.QueryImpl;
import org.sss.common.model.CacheController; import org.sss.common.model.CacheController;
import org.sss.common.model.CacheKey;
import org.sss.common.model.CacheList;
import org.sss.common.model.IModuleList; import org.sss.common.model.IModuleList;
import org.sss.module.pojo.EibsResultSet; import org.sss.module.pojo.EibsResultSet;
import org.sss.presentation.noui.api.exception.NoUiException; import org.sss.presentation.noui.api.exception.NoUiException;
...@@ -17,17 +19,27 @@ public abstract class AbstractCache implements CacheController { ...@@ -17,17 +19,27 @@ public abstract class AbstractCache implements CacheController {
public static final ThreadLocal paginationMap = new ThreadLocal(); public static final ThreadLocal paginationMap = new ThreadLocal();
protected final String cacheName; protected final String cacheName;
final String SPLIT = "__"; final String SPLIT = "__";
final String NULL ="NULL"; final String NULL = "NULL";
public AbstractCache(String redis_cache) { public AbstractCache(String redis_cache) {
this.cacheName = redis_cache; this.cacheName = redis_cache;
} }
@Override @Override
public List cacheRead(Object o, long keepAlive, int maxSize, int offset) { public CacheList cacheRead(CacheKey cacheKey, long keepAlive, int maxSize, int offset) {
List lst = null; Object o = cacheKey.getKey();
String key = generateKey(o, maxSize, offset); String fullSizeKey = generateKey(o);
lst = doCacheRead(key); int fullSize = (int) doCacheRead(fullSizeKey);
if (fullSize == 0) {
try {
fullSize = cacheKey.getFullSize();
doCacheWrite(fullSizeKey, fullSize, keepAlive);
} catch (Exception e) {
throw new NoUiException("[" + this.cacheName + "]执行getFullSize时出现异常", e);
}
}
String valKey = generateValKey(o, maxSize, offset);
List lst = (List) doCacheRead(valKey);
if (lst == null) { if (lst == null) {
if (o instanceof Criteria) { if (o instanceof Criteria) {
Criteria criteria = (Criteria) o; Criteria criteria = (Criteria) o;
...@@ -61,12 +73,25 @@ public abstract class AbstractCache implements CacheController { ...@@ -61,12 +73,25 @@ public abstract class AbstractCache implements CacheController {
throw new NoUiException("[" + this.cacheName + "]执行sql出现异常", e); throw new NoUiException("[" + this.cacheName + "]执行sql出现异常", e);
} }
} }
doCacheWrite(key, lst, keepAlive); doCacheWrite(valKey, lst, keepAlive);
}
return new CacheList(lst, fullSize);
}
private String generateValKey(Object o, int maxSize, int offset) {
StringBuffer sb = new StringBuffer(generateKey(o));
if (maxSize > 0) {
sb.append(SPLIT.intern());
sb.append(maxSize);
}
if (offset > 0) {
sb.append(SPLIT.intern());
sb.append(offset);
} }
return lst; return sb.toString();
} }
private String generateKey(Object o, int maxSize, int offset) { private String generateKey(Object o) {
StringBuffer sb = new StringBuffer(cacheName); StringBuffer sb = new StringBuffer(cacheName);
sb.append(SPLIT.intern()); sb.append(SPLIT.intern());
sb.append(o.getClass().getSimpleName()).append(SPLIT.intern()); sb.append(o.getClass().getSimpleName()).append(SPLIT.intern());
...@@ -87,14 +112,6 @@ public abstract class AbstractCache implements CacheController { ...@@ -87,14 +112,6 @@ public abstract class AbstractCache implements CacheController {
} }
} }
} }
if (maxSize > 0) {
sb.append(SPLIT.intern());
sb.append(maxSize);
}
if (offset > 0) {
sb.append(SPLIT.intern());
sb.append(offset);
}
return sb.toString(); return sb.toString();
} }
...@@ -112,7 +129,7 @@ public abstract class AbstractCache implements CacheController { ...@@ -112,7 +129,7 @@ public abstract class AbstractCache implements CacheController {
moduleList.setPage(paginationItem.get(Constants.PAGINATION_INDEX)); moduleList.setPage(paginationItem.get(Constants.PAGINATION_INDEX));
} }
protected abstract List doCacheRead(String key); protected abstract Object doCacheRead(String key);
protected abstract void doCacheWrite(String key, List lst, long keepAlive); protected abstract void doCacheWrite(String key, Object lst, long keepAlive);
} }
...@@ -3,24 +3,22 @@ package org.sss.presentation.noui.cache; ...@@ -3,24 +3,22 @@ package org.sss.presentation.noui.cache;
import org.sss.presentation.noui.api.exception.NoUiException; import org.sss.presentation.noui.api.exception.NoUiException;
import org.sss.presentation.noui.util.RedisUtil; import org.sss.presentation.noui.util.RedisUtil;
import java.util.List;
public class RedisCache extends AbstractCache { public class RedisCache extends AbstractCache {
public RedisCache() { public RedisCache() {
super("REDIS_CACHE"); super("REDIS_CACHE");
} }
@Override @Override
protected List doCacheRead(String key) { protected Object doCacheRead(String key) {
try { try {
return (List) RedisUtil.get(key); return RedisUtil.get(key);
} catch (Exception e) { } catch (Exception e) {
throw new NoUiException("从缓存[" + this.cacheName + "]中获取key=" + key + "的值出现异常", e); throw new NoUiException("从缓存[" + this.cacheName + "]中获取key=" + key + "的值出现异常", e);
} }
} }
@Override @Override
protected void doCacheWrite(String key, List lst, long keepAlive) { protected void doCacheWrite(String key, Object lst, long keepAlive) {
try { try {
RedisUtil.set(key, lst, (int) (keepAlive / 1000)); RedisUtil.set(key, lst, (int) (keepAlive / 1000));
} catch (Exception e) { } catch (Exception e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment