Commit 13addc6b by fukai

更换实现分页信息缓存的方式

parent 9c63b41f
......@@ -3,12 +3,16 @@ package org.sss.presentation.noui.cache;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.sss.common.model.CacheController;
import org.sss.common.model.IModuleList;
import org.sss.presentation.noui.common.Constants;
import java.util.List;
import java.util.Map;
public abstract class AbstractCache implements CacheController {
protected final String cacheName;
final String SPLIT = "__";
public static final ThreadLocal paginationMap = new ThreadLocal();
public AbstractCache(String redis_cache) {
this.cacheName = redis_cache;
......@@ -59,6 +63,20 @@ public abstract class AbstractCache implements CacheController {
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)
return;
Map<String,Integer> paginationItem = paginationCache.remove(moduleList.getUrl());
if(paginationCache.size() ==0)
paginationMap.remove();
if(paginationItem == null)
return;
moduleList.setPageSize(paginationItem.get(Constants.PAGINATION_PAGESIZE));
moduleList.setPage(paginationItem.get(Constants.PAGINATION_INDEX));
}
protected abstract List doCacheRead(String key);
protected abstract void doCacheWrite(String key, List lst, long keepAlive);
......
package org.sss.presentation.noui.jwt;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class VisitCounter{
private int timeSpan;
private int count;
private Map<String,VisitCounterItem> couterMap = new ConcurrentHashMap<>();
public VisitCounter(int timeSpan,int count){
this.timeSpan = timeSpan;
this.count = count;
}
public boolean addCount(String token)
{
VisitCounterItem item = null;
synchronized (couterMap)
{
item = couterMap.get(token);
if(item == null)
{
item = new VisitCounterItem(timeSpan,count);
couterMap.put(token,item);
}
}
return item.addCount();
}
public void removeCount(String token)
{
}
public static class VisitCounterItem {
private int timeSpan;
private int count;
private long timestamp;
private int visited;
public VisitCounterItem(int timeSpan, int count) {
this.timeSpan = timeSpan * 1000;
this.count = count;
this.timestamp = new Date().getTime();
}
public synchronized boolean addCount() {
long time = new Date().getTime();
if (time - this.timestamp > timeSpan) {
resetCount();
this.visited++;
return true;
} else {
this.visited++;
if (this.visited > count) //超过单位时间内访问次数
{
return false;
} else
return true;
}
}
private void resetCount() {
this.timestamp = new Date().getTime();
this.visited = 0;
}
}
}
......@@ -11,6 +11,7 @@ import org.sss.presentation.noui.api.exception.NoUiException;
import org.sss.presentation.noui.api.model.Alias;
import org.sss.presentation.noui.api.request.NoUiRequest;
import org.sss.presentation.noui.api.response.ErrorCodes;
import org.sss.presentation.noui.cache.AbstractCache;
import org.sss.presentation.noui.common.Constants;
import org.sss.presentation.noui.context.NoUiContext;
import org.sss.presentation.noui.context.NoUiPresentation;
......@@ -93,8 +94,7 @@ public class NoUiPresentationUtil {
//处理分页信息
IModuleList moduleList = (IModuleList) baseObject;
Map<String,Integer> paginationItem = (Map<String,Integer>)paginationMap.get(aliasKey);
moduleList.setPageSize(paginationItem.get(Constants.PAGINATION_PAGESIZE));
moduleList.setPage(paginationItem.get(Constants.PAGINATION_INDEX));
putPaginationCache(paginationItem,moduleList);
} catch (Exception e) {
log.error("Pagination command error", e);
throw new NoUiException("Pagination command error", e);
......@@ -103,6 +103,17 @@ public class NoUiPresentationUtil {
}
private static void putPaginationCache(Map<String,Integer> paginationItem,IModuleList moduleList)
{
Map<String,Map<String,Integer>> paginationCache = (Map<String,Map<String,Integer>>)AbstractCache.paginationMap.get();
if(paginationCache == null)
{
paginationCache = new HashMap<>();
AbstractCache.paginationMap.set(paginationCache);
}
paginationCache.put(moduleList.getUrl(),paginationItem);
}
private static void handleDatafield(IContext context, IDatafield<Object> dataField, Object value) {
if (null == value || dataField == null)
return;
......
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