spring-cacheAnnotations.xml 3.94 KB
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       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">

    <!-- 启用 缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
    <cache:annotation-driven cache-manager="cacheManager"/>

    <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.data.redis.cache.RedisCache">
                    <constructor-arg name="name" value="rc"/>
                    <constructor-arg name="expiration" value="1800000"/>
                    <!--<constructor-arg name="expiration" value="-1"/>-->
                    <constructor-arg name="prefix" value=""/>
                    <constructor-arg name="template" ref="cache.redisTemplate"/>
                    <!--<property name="redisTemplate" ref="redisTemplate" />-->
                    <!--<property name="name" value="default"/>-->
                </bean>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="default"/>
                </bean>
            </set>
        </property>
    </bean>

    <!-- redis 相关配置 -->

    <bean id="cache.jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- <property name="maxActive" value="1000" /> -->
        <property name="maxIdle" value="500"/>
        <!-- <property name="maxWait" value="1000" /> -->
        <property name="testOnBorrow" value="true"/>
    </bean>

    <bean id="cache.jedis.shardInfo1" class="redis.clients.jedis.JedisShardInfo">
        <constructor-arg index="0" value="${project.redis.host1}"/>
        <constructor-arg index="1" value="${project.redis.port1}"/>
    </bean>
    <bean id="cache.connectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="usePool" value="true"/>
        <property name="poolConfig" ref="cache.jedisPoolConfig"/>
        <property name="shardInfo" ref="cache.jedis.shardInfo1"/>
    </bean>

    <bean id="cache.redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="cache.connectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>


    <!-- redis custom 配置 -->
    <bean id="customJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 	<property name="maxTotal" value="1000" />   -->
        <property name="maxIdle" value="500" />
        <property name="maxWaitMillis" value="1000" />
        <property name="testOnBorrow" value="true" />
    </bean>

    <bean id="customShardInfo" class="redis.clients.jedis.JedisShardInfo">
        <constructor-arg index="0" value="${project.redis.host1}" />
        <constructor-arg index="1" value="${project.redis.port1}" />
    </bean>

    <bean id="customShardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="customJedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <ref bean="customShardInfo" />
            </list>
        </constructor-arg>
    </bean>

</beans>