spring集成redis

来源:互联网 发布:监理行业发展前景知乎 编辑:程序博客网 时间:2024/06/03 23:01

在这里会以注解的方式在springmvc中使用redis。
主要记录redis与spring的集成部分。

准备jar包

<dependency>    <groupId>org.springframework.data</groupId>    <artifactId>spring-data-redis</artifactId>    <version>${spring.data.redis.version}</version></dependency><dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>${jedis.version}</version></dependency>

准备资源文件

首先你需要一个资源文件用来配置redis的连接信息

redis.hostname=localhostredis.port=6379    redis.timeout=2000  redis.usePool=true  redis.default.db=0  redis.maxTotal=600  redis.maxIdle=300   redis.timeBetweenEvictionRunsMillis=30000    redis.minEvictableIdleTimeMillis=30000   redis.testOnBorrow=true   redis.encode=utf-8  redis.expire=604800000  redis.unlock=false

spring配置文件中配置redis连接池

<!-- jedis 配置 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">    <property name="maxIdle" value="${redis.maxIdle}" />    <!--<property name="maxWaitMillis" value="${redis.maxWait}" /> -->    <property name="testOnBorrow" value="${redis.testOnBorrow}" /></bean><!-- redis服务器中心 --><bean id="connectionFactory"    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    <property name="poolConfig" ref="poolConfig" />    <property name="port" value="${redis.port}" />    <property name="hostName" value="${redis.hostname}" />    <!-- <property name="password" value="${redis.password}" /> -->    <property name="timeout" value="${redis.timeout}"></property></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">    <property name="connectionFactory"   ref="connectionFactory" />       <property name="keySerializer">           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />       </property>          <property name="valueSerializer">           <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />       </property>       <property name="hashKeySerializer">            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>         </property>       <property name="hashValueSerializer">          <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>         </property></bean>

配置spring 缓存并且开启注解缓存

<!-- 开启注解缓存 --><cache:annotation-driven cache-manager="cacheManager" /><!-- 配置spring缓存管理为redis --><bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">    <constructor-arg ref="redisTemplate" /></bean>

如果在配置文件中没有开启注解缓存功能,那么使用注解来进行缓存将会失败

使用redis

用注解@Cacheable、@CachePut、@CacheEvict 将查询结果保存在redis服务器上。

redis的集群部署

<bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">    <property name="maxRedirects" value="3"></property>    <property name="clusterNodes">        <set> <!-- 放主服务器的配置 -->            <bean class="org.springframework.data.redis.connection.RedisNode">                <constructor-arg name="host" value="127.0.0.1"></constructor-arg>                <constructor-arg name="port" value="6379"></constructor-arg>            </bean>        </set>    </property></bean>

配置了这个bean之后就ok了

0 0
原创粉丝点击