一、保存一次查询结果至redis
String key=keyword+"_"+books+"_contents";
List<String> contents_json=new ArrayList<>();
for(ContentInfo c:contents){
contents_json.add(ObjectMapperUtil.toJSON(c));
}
RedisUtil.rpush(key,contents_json);
String key1=keyword+"_"+books+"_pageSum";
RedisUtil.set(key1, String.valueOf(x));
将结果以list格式保存至redis
List<String> contents_json=new ArrayList<>();
for(ContentInfo c:contents){
contents_json.add(ObjectMapperUtil.toJSON(c));
}
RedisUtil.rpush(key,contents_json);
二、redis分页查询
先在redis数据库查询
if(RedisUtil.exists(key)){
List<String> contends_json=RedisUtil.hZget(key,(page-1)*pagenum,page*pagenum);
List<ContentInfo> contents= JSONArray.parseArray(contends_json.toString(),ContentInfo.class);
return new R(true,Integer.parseInt(RedisUtil.get(key1)),contents);
}
若查询不到则在数据库查询并添加数据至redis,即调用上述方法
else{
return getCont2(keyword,page,books,request);
}