Spring Session實(shí)現(xiàn)無(wú)侵入性Redis緩存

在之前的小結(jié)Redis分布式實(shí)現(xiàn)(原生實(shí)現(xiàn))中實(shí)現(xiàn)了原生的分布式redis緩存方案,但它的侵入性太強(qiáng),對(duì)于已有的項(xiàng)目改造起來(lái)成本較大。今天就來(lái)通過(guò)Spring Session來(lái)實(shí)現(xiàn)無(wú)侵入性方案。

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無(wú)法寫入)
開(kāi)始調(diào)用接口測(cè)試:
session

這里看到通過(guò)在wen.xml文件的設(shè)置session已經(jīng)被轉(zhuǎn)化成redissession,并設(shè)置了創(chuàng)建時(shí)間等:


session

通過(guò)redis可視化工具看到需要被存儲(chǔ)等bean已經(jīng)被存入,并且還添加了expires等關(guān)聯(lián)。當(dāng)關(guān)聯(lián)時(shí)間到期時(shí)會(huì)被清除,緩存的bean不會(huì),它會(huì)到自己過(guò)期才清除。但由于關(guān)聯(lián)過(guò)期,緩存也自然無(wú)法獲取。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容