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)在增删改操作時完成時,執行删除對應緩存操作