一、代码编写
package cn.sp.component;
public interface JedisClient {
public String set(String key,String value);
public String get(String key);
public Long hset(String key,String item,String value);
public String hget(String key,String item);
//删除
public Long del(String key);
}
package cn.sp.component.imp;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sp.component.JedisClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClientImpl implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
}
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
}
@Override
public Long hset(String key, String item, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(key, item, value);
jedis.close();
return result;
}
@Override
public String hget(String key, String item) {
Jedis jedis = jedisPool.getResource();
String result = jedis.hget(key, item);
jedis.close();
return result;
}
@Override
public Long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
jedis.close();
return result;
}
}
@Resource
private JedisClient jedisClient;
@RequestMapping(value="index")
public String index(@RequestParam(value="page",required=false)String page,@RequestParam(value="typeId",required=false)String typeId,
@RequestParam(value="releaseDateStr",required=false)String releaseDateStr,ModelMap m,HttpServletRequest request){
Gson gson = new Gson();
if (StringUtils.isBlank(page)) {
page = "1";
}
Map<String, Object> map = new HashMap<String,Object>();
PageBean pageBean = new PageBean(Integer.parseInt(page),10);
map.put("size", pageBean.getPageSize());
map.put("start", pageBean.getStart());
map.put("typeId", typeId);
map.put("releaseDateStr", releaseDateStr);
//先从缓存中取数据
String json2 = jedisClient.hget(Constant.REDIS_LIST, page);
Type type = new TypeToken<ArrayList<Blog>>(){}.getType();
List<Blog> bloglist2 = gson.fromJson(json2, type);
if(bloglist2 != null && bloglist2.size() > 0 && StringUtil.isEmpty(typeId) && StringUtil.isEmpty(releaseDateStr)){//搜索条件为空时
m.put("blogList", bloglist2);
}else{
List<Blog> blogList = blogService.list(map);
//使用jsoup抓取图片
for (Blog blog : blogList) {
String content = blog.getContent();
Document doc = Jsoup.parse(content);
Elements jpgs = doc.select("img[src$=.jpg]");
List<String> imageList = blog.getImageList();
for(int i=0 ; i<jpgs.size() ; i++){
Element jpg = jpgs.get(i);
imageList.add(jpg.toString());
if (i == 2) {
//最多三个图
break;
}
}
}
//将blogList加入缓存,当搜索条件为空时
if(StringUtil.isEmpty(typeId) && StringUtil.isEmpty(releaseDateStr)){
String json = gson.toJson(blogList);
jedisClient.hset(Constant.REDIS_LIST, page, json);
}
m.put("blogList", blogList);
}
//先判断有无
String result = jedisClient.hget(Constant.REDIS_KEY, page);
if(StringUtil.isNotEmpty(result)){
m.put("pageCode", result);
}else{
//跳转的url
String url =request.getContextPath()+"/index.html";
//总记录数
Long totalNum = blogService.getTotal(map);
StringBuffer param = new StringBuffer();
//分页条件回显
if (StringUtils.isNotBlank(typeId)) {
param.append("typeId="+typeId+"&");
}
if (StringUtils.isNotBlank(releaseDateStr)) {
param.append("releaseDateStr="+releaseDateStr);
}
//生成分页html
String pageCode = PageUtil.genPagination(url, totalNum, Integer.parseInt(page), 10, param.toString());
//放入redis缓存,以当前页没 item
jedisClient.hset(Constant.REDIS_KEY, page, pageCode);
m.put("pageCode", pageCode);
}
m.put("mainPage", "/front/common/list.jsp");
m.put("title", "开源博客系统");
return "index";
}
谷歌的gson就是强大。这里把分页数据和分页html代码放进了redis缓存中。
二、applicationContext.xml
2.1单机版
<!-- jedis配置 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.75.129"></constructor-arg>
<constructor-arg name="port" value="6379" ></constructor-arg>
</bean>
<bean id="jedisClient" class="cn.sp.component.imp.JedisClientImpl"></bean>
结果使用ab压力测试结果,不加缓存的情况1000次请求10个并发,共需要16s,加缓存后大概只要5s的时间。
刷新缓存的话,只需要删除对应的key即可。
2.2集群版配置
<!-- 配置redis客户端集群版 -->
<!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"/>
<constructor-arg name="port" value="7005"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.153"/>
<constructor-arg name="port" value="7006"/>
</bean>
</set>
</constructor-arg>
</bean>
<bean id="jedisClientCluster" class="com.taotao.rest.component.impl.JedisClientCluster"/> -->