Redis的Spring配置

文章目录

一、redis.properties

复制代码
#redis集群数量
redis.maxRedirects=3
#redis集群ip
redis.host1=127.0.0.1
redis.host2=127.0.0.2
redis.host3=127.0.0.3

#host
redis.host=localhost
#访问端口
redis.port=6379
#redis密码
redis.password=123
#连接池的最大数据库连接数。设为0表示无限制
redis.maxTotal=1024
#最大空闲数,超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。  
redis.maxIdle=100
#最小空闲数,超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。  
redis.minIdle=1
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。  
redis.maxWaitMillis=5000
#在获取连接的时候检查有效性, 默认false 数据量大的时候建议关闭
redis.testOnBorrow=true
redis.testOnReturn=true

二、redis单机版

复制代码
<!-- redis连接池 -->
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxTotal" value="${redis.maxTotal}" />
	<property name="maxIdle" value="${redis.maxIdle}" />
	<property name="minIdle" value="${redis.minIdle}" />
	<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
	<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>

<!-- redis连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
	<property name="hostName" value="${redis.host}"></property>
	<property name="port" value="${redis.port}"></property>
	<property name="password" value="${redis.password}"></property>
	<property name="poolConfig" ref="jedisConfig"></property>
</bean>

<!-- RedisTemplate注入 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
    <!--全局的key序列化策略-->
    <property name="keySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <!--全局的value序列化策略-->
    <property name="valueSerializer">
        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    </property>
    <!--开启事务 -->
    <property name="enableTransactionSupport" value="false" />
</bean>

三、redis集群版

  • spring-redis.xml 集群配置,用 spring-data-redis 包做集成

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

相关推荐
小马爱打代码几秒前
Spring Boot :使用 Spring Cache 注解方式集成 Redis
spring boot·redis·spring
l1t2 分钟前
DeekSeek辅助总结PostgreSQL Mistakes and How to Avoid Them 的一个例子
数据库·postgresql
醉风塘14 分钟前
JDBC批量操作终极指南:PreparedStatement批处理与事务性能优化实战
数据库·性能优化
2401_8384725117 分钟前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python
invicinble37 分钟前
对于mysql层对sql层面的知识体系的理解和把握
数据库·sql·mysql
2301_7903009640 分钟前
用Matplotlib绘制专业图表:从基础到高级
jvm·数据库·python
有味道的男人42 分钟前
1688获得商品类目调取商品榜单
java·前端·spring
DFT计算杂谈1 小时前
VASP+PHONOPY+pypolymlpj计算不同温度下声子谱,附批处理脚本
java·前端·数据库·人工智能·python
树码小子1 小时前
SpringMCV(9)响应:返回静态页面 & 修改响应数据
spring·mvc