Commit 75494fa3 by WeiCong

增加缓存支持模式

parent 3ce9653a
......@@ -5,15 +5,19 @@ import org.hibernate.Query;
import org.hibernate.internal.QueryImpl;
import org.sss.common.model.CacheController;
import org.sss.common.model.IModuleList;
import org.sss.module.pojo.EibsResultSet;
import org.sss.presentation.noui.api.exception.NoUiException;
import org.sss.presentation.noui.common.Constants;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public abstract class AbstractCache implements CacheController {
public static final ThreadLocal paginationMap = new ThreadLocal();
protected final String cacheName;
final String SPLIT = "__";
public static final ThreadLocal paginationMap = new ThreadLocal();
final String NULL ="NULL";
public AbstractCache(String redis_cache) {
this.cacheName = redis_cache;
......@@ -43,6 +47,19 @@ public abstract class AbstractCache implements CacheController {
query.setFirstResult(offset);
}
lst = query.list();
} else if (o instanceof EibsResultSet) {
EibsResultSet eibsResultSet = (EibsResultSet) o;
if (maxSize > 0) {
eibsResultSet.setMaxResults(maxSize);
}
if (offset > 0) {
eibsResultSet.setFirstResult(offset);
}
try {
lst = eibsResultSet.list();
} catch (SQLException e) {
throw new NoUiException("[" + this.cacheName + "]执行sql出现异常", e);
}
}
doCacheWrite(key, lst, keepAlive);
}
......@@ -52,7 +69,23 @@ public abstract class AbstractCache implements CacheController {
private String generateKey(Object o, int maxSize, int offset) {
StringBuffer sb = new StringBuffer(cacheName);
sb.append(SPLIT.intern());
sb.append(o.toString());
if (o instanceof EibsResultSet) {
sb.append(((EibsResultSet) o).getSql());
} else {
sb.append(o.toString());
}
if (o instanceof EibsResultSet || o instanceof QueryImpl) {
Object[] values = o instanceof EibsResultSet ? ((EibsResultSet) o).getArgs() : ((QueryImpl) o).valueArray();
if (values != null) {
for (Object val : values) {
sb.append(SPLIT.intern());
if (val == null) {
sb.append(NULL.intern());
} else
sb.append(val.toString());
}
}
}
if (maxSize > 0) {
sb.append(SPLIT.intern());
sb.append(maxSize);
......@@ -61,36 +94,18 @@ public abstract class AbstractCache implements CacheController {
sb.append(SPLIT.intern());
sb.append(offset);
}
//添加数据计算key
if(o instanceof QueryImpl)
{
Object[] values = ((QueryImpl)o).valueArray();
if(values != null)
{
for (Object val:values) {
if(val == null)
{
sb.append("null");
}
else
sb.append(val.toString());
sb.append(',');
}
}
}
return sb.toString();
}
@Override
public void prepareReadset(IModuleList moduleList) {
Map<String, Map<String,Integer>> paginationCache = (Map<String, Map<String,Integer>>)paginationMap.get();
if(paginationCache == null)
Map<String, Map<String, Integer>> paginationCache = (Map<String, Map<String, Integer>>) paginationMap.get();
if (paginationCache == null)
return;
Map<String,Integer> paginationItem = paginationCache.remove(moduleList.getUrl());
if(paginationCache.size() ==0)
Map<String, Integer> paginationItem = paginationCache.remove(moduleList.getUrl());
if (paginationCache.size() == 0)
paginationMap.remove();
if(paginationItem == null)
if (paginationItem == null)
return;
moduleList.setPageSize(paginationItem.get(Constants.PAGINATION_PAGESIZE));
moduleList.setPage(paginationItem.get(Constants.PAGINATION_INDEX));
......
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