参考代码:
public String getNewCenter(HttpServletRequest request,HttpServletResponse resonse){
JSONObject result = new JSONObject();
Jedis jedis = null;
try{
jedis = JedisPoolManager.getResource();
List<BoardBean> listSelect = new ArrayList<BoardBean>();
List<BoardBean> listTemp = new ArrayList<BoardBean>();
List<BoardBean> listTemp2 = new ArrayList<BoardBean>();
List<BoardBean> listNew = new ArrayList<BoardBean>();
boolean flag = false;
//从redis中获取临时存的数据
String strJson = jedis.get(NianHuoJieConstant.NEW_MEIWU_CENTER_KEY);
if(strJson !=null && strJson != ""){
listTemp = JSONArray.parseArray(strJson, BoardBean.class);
}
//从库中获取所有的数据
BoardQueryTerms bqt = this.getBoardQueryTerms(request);
listSelect = this.boardService.getMapBoardBeansByQueryTerms(bqt);
//把新增的数据添加到listNew中,根据查出所有的数据和临时数据做比较,把最新的数据取出来
if(listTemp!=null){
if(listSelect.size()>0 && listTemp.size()>0){
for(BoardBean a:listSelect){
for(BoardBean b:listTemp){
if(a.getBoard_id()==b.getBoard_id()){
flag=true;
break;
}
}
if(!flag){
listNew.add(a);
}
flag=false;
}
}
}
//把查出来的所有数据放到临时list存储
if(listSelect!=null){
if(listSelect.size()>0){
for(int i=0;i<listSelect.size();i++){
listTemp2.add(listSelect.get(i));
}
jedis.del(NianHuoJieConstant.NEW_MEIWU_CENTER_KEY);//清空上一次存储redis的数据
String jsonstr = JSONArray.toJSONString(listTemp2);
jedis.set(NianHuoJieConstant.NEW_MEIWU_CENTER_KEY, jsonstr);//把临时存的数据放到redis中
}
}
result.put("listNew", listNew);//去掉重复后库中新增的数据
result.put("curBoardQueryTerms", bqt);
}catch(Exception e){
e.printStackTrace();
}finally{
JedisPoolManager.returnResource(jedis);
}
return result.toString();
}