Spring Data JPA整合Redis緩存的配置

1. 整合的配置文件以下
    <!-- Spring整合redis -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="30"/>
    </bean>

    <!-- 配置JedisConnectionFactoryBean對象 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="poolConfig"/>
    </bean>

    <!-- 配置RedisTemplate -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>

2. 測試代碼以下
    @Test
    public void run7() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-redis.xml");
        RedisTemplate<String, String> t = (RedisTemplate<String, String>) ac.getBean("redisTemplate");
        t.opsForValue().set("msg", "haha");
    }
相關文章
相關標籤/搜索