e3mall项目:前台广告加载时,缓存的应用
一、导包
<!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
二、jedis接口与实现类
三、e3-content-service新增配置文件:applicationContext-redis.xml 以及 resource.properties
(1)applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!--配置单机版redis--> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="${redis.host}"/> <constructor-arg name="port" value="${redis.port}"/> </bean> <bean id="jedisClientPool" class="cn.e3mall.common.redis.JedisClientPool"> <property name="jedisPool" ref="jedisPool" /> </bean> <!--配置集群版redis--> <!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.host1}"/> <constructor-arg name="port" value="${redis.port1}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.host2}"/> <constructor-arg name="port" value="${redis.port2}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.host3}"/> <constructor-arg name="port" value="${redis.port3}"/> </bean> </set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="cn.e3mall.common.redis.JedisClientCluster"> <property name="jedisCluster" ref="jedisCluster"/> </bean> --> </beans>
(2)resource.properties
## redis单机版连接信息 redis.host=192.168.25.130 redis.port=6379 ## redis集群版连接信息 # redis.host1=192.168.25.130 # redis.port1=6379 # redis.host2=192.168.25.131 # redis.port2=6379 # redis.host3=192.168.25.132 # redis.port3=6379 ## 内容对应的redis-key CONTENT_LIST=CONTENT_LIST
四、代码修改,只需要修改ContentServiceImpl中的getContentList(Long categoryId)方法
@Autowired private JedisClient jedisClient; @Value("${CONTENT_LIST}") private String CONTENT_LIST;
@Override public List<TbContent> getContentList(Long categoryId) { //创建结果集对象 List<TbContent> tbContents = null; //缓存查询操作 String json = jedisClient.hget(CONTENT_LIST, categoryId + ""); //判断查询出来的结果是否为空 if(StringUtils.isNotBlank(json)){ //查询出来的结果不为空,将查询结果转换并复制给结果集 tbContents = JsonUtils.jsonToList(json, TbContent.class); return tbContents; } //创建查询条件对象 TbContentExample example = new TbContentExample(); //封装查询条件 example.createCriteria().andCategoryIdEqualTo(categoryId); //执行查询获取结果 tbContents = contentMapper.selectByExampleWithBLOBs(example); //将从数据库中查询到的结果保存到redis缓存当中 jedisClient.hset(CONTENT_LIST,categoryId + "", JsonUtils.objectToJson(tbContents)); return tbContents; }
五、后台新增广告数据时,缓存同步(在增删改操作时,删除缓存即可)
(1)在ContentServiceImpl中新增一个方法:redisSync(Long categoryId)
/* * redis缓存同步 */ private void redisSync(Long categoryId) { //删除对应field缓存即可 jedisClient.hdel(CONTENT_LIST,categoryId + ""); }
(2)在增删改操作时完成时,执行删除对应缓存操作