一、缓存
我们知道使用缓存,可以提高查询效率,那什么情况下需要使用缓存呢?通常而言,使用缓存需满足以下两个条件:
(1)查询频率较高的数据。
(2)修改频率较低的数据。
对于第一点,在我们开发过程中,如果查询业务比较多,需要频繁的连接数据库,这会对数据库的性能带来极大的损耗;这个时候可以考虑对这部分数据添加缓存。
对于第二点,如果我们业务中,需要频繁的修改某个数据,这个时候是不适合给它添加缓存的,因为每次修改了数据,都需要去更新缓存。
综合上面两点,我们需要给导航菜单、广告位投放的内容添加缓存。
[if !supportLists]1.2 [endif]缓存逻辑实现
[if !supportLists]1.2.1 [endif]第一部分:添加缓存
需求:在REST接口中,给导航菜单、首页大广告位内容添加缓存。缓存逻辑在ego-rest工程中实现。
[if !vml]
[endif]
[if !supportLists]1.2.1.1 [endif]第一步:添加redis的jar依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
[if !supportLists]1.2.1.2 [endif]第二步:Spring整合Redis集群
在src路径下,添加spring-jedis.xml配置文件,整合redis。
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 连接池配置 -->
<beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<propertyname="maxTotal"value="30"/>
<!-- 最大空闲连接数 -->
<propertyname="maxIdle"value="10"/>
<!-- 每次释放连接的最大数目 -->
<propertyname="numTestsPerEvictionRun"value="1024"/>
<!-- 释放连接的扫描间隔(毫秒) -->
<propertyname="timeBetweenEvictionRunsMillis"value="30000"/>
<!-- 连接最小空闲时间 -->
<propertyname="minEvictableIdleTimeMillis"value="1800000"/>
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<propertyname="softMinEvictableIdleTimeMillis"value="10000"/>
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<propertyname="maxWaitMillis"value="1500"/>
<!-- 在获取连接的时候检查有效性, 默认false
-->
<propertyname="testOnBorrow"value="true"/>
<!-- 在空闲时检查有效性, 默认false
-->
<propertyname="testWhileIdle"value="true"/>
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<propertyname="blockWhenExhausted"value="false"/>
</bean>
<beanid="jedisCluster"class="redis.clients.jedis.JedisCluster">
<constructor-argindex="0">
<set>
<beanclass="redis.clients.jedis.HostAndPort">
<constructor-argindex="0"value="192.168.23.12"></constructor-arg>
<constructor-argindex="1"value="7001"></constructor-arg>
</bean>
<beanclass="redis.clients.jedis.HostAndPort">
<constructor-argindex="0"value="192.168.23.12"></constructor-arg>
<constructor-argindex="1"value="7002"></constructor-arg>
</bean>
<beanclass="redis.clients.jedis.HostAndPort">
<constructor-argindex="0"value="192.168.23.12"></constructor-arg>
<constructor-argindex="1"value="7003"></constructor-arg>
</bean>
<beanclass="redis.clients.jedis.HostAndPort">
<constructor-argindex="0"value="192.168.23.12"></constructor-arg>
<constructor-argindex="1"value="7004"></constructor-arg>
</bean>
<beanclass="redis.clients.jedis.HostAndPort">
<constructor-argindex="0"value="192.168.23.12"></constructor-arg>
<constructor-argindex="1"value="7005"></constructor-arg>
</bean>
<beanclass="redis.clients.jedis.HostAndPort">
<constructor-argindex="0"value="192.168.23.12"></constructor-arg>
<constructor-argindex="1"value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-argindex="1"ref="jedisPoolConfig"></constructor-arg>
</bean>
</beans>
[if !supportLists]1.2.1.3 [endif]第三步:修改ContentServiceImpl类
@Service
public class ContentServiceImpl implements ContentService{
private String EGO_CONTENT = "EGO_CONTENT";
@Autowired
private JedisCluster jedisCluster;
@Autowired
private ContentMapper mapper;
/*
*缓存逻辑
*
(1)首先查找缓存。
*
(2)如果缓存有数据,则直接返回数据,不需要查询数据库
*
(3)如果缓存中没有数据,则查询数据库。并且将数据放入缓存中。
*
*
(4)缓存不能影响正常的业务执行,即当前缓存无法使用,则直接查询数据库
*
*确定选用哪一个数据结构类型(string,list,set,hash)
*选用数据结构类型的时候,有一个原则:能用hash尽量使用hash。原因:减少key的数量,在寻址的时候速度更快。
*
* {key:{field:value}}
*我们这里使用hash数据结构类型,key定义成一个常量EGO_CONTENT field就使用类型分类的id,value使用内容列表的json格式。
*
*注意:hash结构只能存储string格式的数据
*
*/
@Override
public EgoResult getContentByCatId(Long catId) {
Listlist = null;
try {
//1、查找缓存
StringjsonData = jedisCluster.hget(EGO_CONTENT, catId+"");
if(null!=jsonData && !"".equals(jsonData)){
//缓存里面有数据,则直接返回数据即可
list= JsonUtils.jsonToList(jsonData, Content.class);
}else{
//如果缓存中查不到数据,则查询数据库
MapcolumnMap = new HashMap<>();
columnMap.put("category_id", catId);
list = mapper.selectByMap(columnMap);
//再将数据放入缓存中
jedisCluster.hset(EGO_CONTENT, catId+"", JsonUtils.objectToJson(list));
}
}catch (Exception e) {
e.printStackTrace();
//如果缓存中查不到数据,则查询数据库
MapcolumnMap = new HashMap<>();
columnMap.put("category_id", catId);
list = mapper.selectByMap(columnMap);
}
returnEgoResult.ok(list);
}
}
[if !supportLists]1.2.1.4 [endif]第四步:测试
(1)重启rest工程
(2)访问portal工程首页。将缓存添加到redis中。
(3)再次访问portal工程首页。查看缓存是否生效。
[if !supportLists]1.2.2 [endif]第二部分:缓存同步
修改、更新导航菜单、网站内容后,要同步修改缓存,或者清空对应的缓存。
[if !vml]
[endif]
[if !supportLists]2 [endif]搜索系统实现
[if !supportLists]2.1 [endif]系统架构
在本项目中,我们将搜索业务独立出来,创建搜索子系统。这样做,既能提高系统的拓展能力,也能灵活的对系统进行分布式部署。
[if !vml]
[endif]
[if !supportLists]2.2 [endif]实现思路
(1)搭建搜索服务器。
(2)创建搜索系统。
(3)发布搜索服务的公共接口。
(4)在门户系统调用该接口,实现搜索。
网友评论