1. 程式人生 > >cache完整的快取配置

cache完整的快取配置

完整的快取配置:

切換快取的時候cacheManager他的管理器變下即可,註解其他照用。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

  xmlns:cache="http://www.springframework.org/schema/cache"

  xmlns:c="http://www.springframework.org/schema/c"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xmlns:p="http://www.springframework.org/schema/p"

  xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 開啟快取註解 -->

  <cache:annotation-driven/>

<!-- redis開始 -->

    <bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="order" value="1" />

        <property name="ignoreUnresolvablePlaceholders" value="true" />

        <property name="locations">

            <list>

                <value>classpath:config_spring/redis.properties</value>

            </list>

        </property>

    </bean>

    <!-- Jedis ConnectionFactory -->

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

        <property name="maxTotal" value="${redis.maxTotal}" />

        <property name="maxIdle" value="${redis.maxIdle}" />

        <property name="timeBetweenEvictionRunsMillis" value="${redis.minEvictableIdleTimeMillis}" />

        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />

        <property name="testOnBorrow" value="${redis.testOnBorrow}" />

    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

        <property name="hostName" value="${redis.host}" />

        <property name="port" value="${redis.port}" />     

        <!-- <property name="password" value="${redis.pass}" /> -->

        <property name="poolConfig" ref="jedisPoolConfig" />

    </bean>

<!-- 在spring-cache。xml裡已經做了序列化,這裡就註釋掉了 -->

<!-- Redis序列化,用於轉換N個物件存在1個Key裡 -->

<bean id="redisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>

<!-- 消除Redis的key前面的\xac\xed\x00\x05t\x00\t這些東西 -->

<bean id="stringRedisSerializer"

class="org.springframework.data.redis.serializer.StringRedisSerializer" />

<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"

p:keySerializer-ref="stringRedisSerializer" p:hashKeySerializer-ref="stringRedisSerializer"

p:connection-factory-ref="jedisConnectionFactory" />

<!-- redis快取管理器 -->

<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"

c:template-ref="jedisTemplate" /> 

<!-- redis結束 -->

</beans>