spring集成redis

在这里会以注解的方式在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=localhost
redis.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值