配置Spring Data Redis

Posted Deolin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置Spring Data Redis相关的知识,希望对你有一定的参考价值。

事前准备

1、下载redis

https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100

 

2、下载redis可视化工具

https://redisdesktop.com/download

 

3、启动redis

cd %redis%

redis-server.exe

 

配置

4、pom.xml追加

spring-data-redis

jedis

 

5、新建redis/redis.properties

redis.hostname=localhost
redis.port=6379
#redis.password=

 

6、新建springframework/spring-redis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:redis/redis.properties"
        ignore-unresolvable="true" />

    <bean id="redisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostname}" />
        <property name="port" value="${redis.port}" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="ConnectionFactory" ref="redisConnectionFactory" />
    </bean>

    <bean id="cacheManager"
        class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="redisTemplate" />
        <property name="defaultExpiration" value="3000" />
    </bean>

</beans>

 

8、确保spring-redis.xml能被引入application-context.xml中

<import resource="classpath:springframework/spring-*.xml" />

 

7、至此,可以通过在ServiceImpl类的方法上追加@Cacheable @CachePut @CacheEvict 来实现缓存了。

以上是关于配置Spring Data Redis的主要内容,如果未能解决你的问题,请参考以下文章

Spring Data Redis入门示例:程序配置

Spring Data JPA整合Redis缓存的配置

spring-data-redis 可以实现消息队列吗

【原创】Springboot Redis配置总结( 基于spring-boot-data-redis-stater )

spring data redis的配置类RedisConfig

Spring Boot + spring-data-redis