在之前的小結(jié)Redis分布式實(shí)現(xiàn)(原生實(shí)現(xiàn))中實(shí)現(xiàn)了原生的分布式redis緩存方案情龄,但它的侵入性太強(qiáng)台丛,對(duì)于已有的項(xiàng)目改造起來成本較大聘萨。今天就來通過Spring Session來實(shí)現(xiàn)無侵入性方案浅蚪。
pom中導(dǎo)入需要的依賴:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>2.9.0</version>
</dependency>
web.xml中添加filter,這樣當(dāng)請(qǐng)求.do接口時(shí)httpsesion就會(huì)轉(zhuǎn)化成我們需要的redissession:
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
新建applicationContext-spring-session.xml用于對(duì)jedis的工廠痴颊、鏈接池設(shè)置(只列出基本的設(shè)置,詳細(xì)需要進(jìn)入代碼查看):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800"/>
</bean>
<bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="domainName" value=".happymmall.com"/>
<property name="useHttpOnlyCookie" value="true"/>
<property name="cookiePath" value="/"/>
<property name="cookieMaxAge" value="31536000"/>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="20"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
</beans>
最后將需要緩存的bean實(shí)現(xiàn)Serializable(否者redis無法寫入)
開始調(diào)用接口測(cè)試:
這里看到通過在wen.xml文件的設(shè)置session已經(jīng)被轉(zhuǎn)化成redissession屡贺,并設(shè)置了創(chuàng)建時(shí)間等:
通過redis可視化工具看到需要被存儲(chǔ)等bean已經(jīng)被存入蠢棱,并且還添加了expires等關(guān)聯(lián)。當(dāng)關(guān)聯(lián)時(shí)間到期時(shí)會(huì)被清除甩栈,緩存的bean不會(huì)裳扯,它會(huì)到自己過期才清除。但由于關(guān)聯(lián)過期谤职,緩存也自然無法獲取饰豺。