上节课我们一起学习了如何用Spring容器来管理Redis单机版和集群版实现。这节我们来学习下在业务中添加缓存。
Redis添加缓存有两种方式,一种是set,另一种是hset,这两种方式的不同之处是hset可以对key进行分类,查询的时候先查询类,然后再在该类下查询某个key的值,这样的效率肯定要比set方式存储的数据查询效率高。因此我们选择hset的方式来存储缓存信息。
我们是以首页大广告位的展示为例,我们便给hkey(分类)起个名字,叫"INDEX_CONTENT",为了不把代码写死,我们把它放到配置文件当中,如下图所示。
添加了一个resource.properties文件,需要在Spring容器中扫描进去,我们把applicationContext-dao.xml文件中加载配置文件这一行做下修改,将db.properties修改为*.properties。这样properties目录下所有的以properties结尾的文件都会被加载进来。
下面我们修改taotao-content-service工程下的ContentServiceImpl.java中的getContentListByCid方法,如下所示。我们可以打断点查看一下第一次访问首页大广告位和第二次访问首页大广告位的情况。
package com.taotao.content.service.impl;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentService;
import com.taotao.jedis.service.JedisClient;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbContentExample.Criteria;
/**
* @author gzm
* @date 2018年7月31日 上午9:01:14
*
*/
@Service
public class ContentServiceImpl implements ContentService {
@Autowired
private TbContentMapper contentMapper;
@Value("${INDEX_CONTENT}")
private String INDEX_CONTENT;
@Autowired
private JedisClient jedisClient;
//.................省略中间部分
@Override
public List<TbContent> getContentListByCid(long cid) {
//首先查询缓存,如果缓存中存在的话,就直接将结果返回给前台展示,查询缓存不能影响业务流程
try {
String json = jedisClient.hget(INDEX_CONTENT, cid+"");
//如果从缓存中查到了结果
if(StringUtils.isNotBlank(json)){
//将json串转化为List<TbContent>
List<TbContent> list = JSON.parseArray(json, TbContent.class);
return list;
}
} catch (Exception e) {
e.printStackTrace();
}
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(cid);
List<TbContent> list = contentMapper.selectByExample(example);
//添加缓存,不能影响业务流程
try {
String json = JSON.toJSONString(list);
jedisClient.hset(INDEX_CONTENT, cid+"", json);
} catch (Exception e) {
e.printStackTrace();
}
//返回结果
return list;
}
}
缓存有个问题就是如果数据库中的数据做了修改,缓存是需要同步的,否则查询的还是老数据,因此凡是涉及增、删、改的操作都需要同步缓存。我们以添加内容为例。如下所示。
@Override
public TaotaoResult addContent(TbContent content) {
//补充属性
content.setCreated(new Date());
content.setUpdated(new Date());
//添加
contentMapper.insert(content);
//同步缓存,由于首页大广告位的分类ID为89,content.getCategoryId()得到的便是89
jedisClient.hdel(INDEX_CONTENT, content.getCategoryId().toString());
//返回结果
return TaotaoResult.ok();
}
下面我们便来添加一条广告,会先清除缓存然后会去查询数据库。