springboot整合redis的两种连接方式

 

jedis和lettuce比较:

jedis:多线程下,非线程安全,所以使用连接池(不支持异步操作),适用springboot1.x

lettuce:多线程下,线程安全,基于Netty支持异步操作,适用springboot2.x

redis五大类型操作如下:

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

redis的所有Serializer如下:

1、GenericToStringSerializer: 可以将任何对象泛化为字符串并序列化

2、StringRedisSerializer: 简单的字符串序列化 

3、Jackson2JsonRedisSerializer: 序列化object对象为json字符串
如:"{\"userName\":\"user\",\"age\":10}"

4、JdkSerializationRedisSerializer: 序列化java对象(被序列化的对象必须实现Serializable接口),无法转义成对象。
如:\x01\x03\x02\x11t\x00

5、GenericJackson2JsonRedisSerializer:类似第三条,但使用时构造函数不用特定的类参考以上序列化,自定义序列化类。(推荐)

 

一、使用lettuce

1、引入pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.2.4.RELEASE</version>
</dependency>

<!--redis连接池-->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.8.0</version>
</dependency>

2、application配置文件添加

#redis配置
#数据库索引,默认0
spring.redis.database=0
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
#连接超时时间毫秒(类型为Duration,添加秒)
spring.redis.timeout=10s

# Lettuce
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=1
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=3
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
spring.redis.lettuce.pool.max-wait=5s
#空闲链接检测线程检测周期毫秒(负值表示不检测)(类型为Duration,添加秒)
spring.redis.lettuce.pool.time-between-eviction-runs=60s
#关闭超时时间
spring.redis.lettuce.shutdown-timeout=100

3、如果需要修改Serializer、打开事务等,在启动类添加(选填)

	@Bean
	public RedisTemplate redisTemplate(LettuceConnectionFactory factory){
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		//配置序列化方式
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

		//设置redis支持数据库的事务
		redisTemplate.setEnableTransactionSupport(true);

		return redisTemplate;
	}

4、注入RedisTemplate,使用redis操作即可,如:opsForValue(),expire()(设置过期时间)

 

二、使用jedis

1、引入pom(spring-boot-starter-data-redis 默认是lettuce 需要先排除)

		<!--spring-boot-starter-data-redis 默认是lettuce 需要先排除-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<version>2.2.4.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--redis连接池-->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.8.0</version>
		</dependency>

		<!--jedis-->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.2.0</version>
		</dependency>

2、application配置文件添加

#redis配置
#数据库索引,默认0
spring.redis.database=0
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
#连接超时时间毫秒(类型为Duration,添加秒)
spring.redis.timeout=10s
#连接池最大连接数
spring.redis.jedis.pool.max-active=5
#连接池最小空闲连接
spring.redis.jedis.pool.min-idle=1
#连接池最大空闲连接
spring.redis.jedis.pool.max-idle=3
#连接池最大阻塞等待时间(负值表示没有限制)(类型为Duration,添加秒)
spring.redis.jedis.pool.max-wait=5s
#空闲链接检测线程检测周期毫秒(负值表示不检测)(类型为Duration,添加秒)
spring.redis.jedis.pool.time-between-eviction-runs=60s

3、如果需要修改Serializer、打开事务等,在启动类添加(选填)

	@Bean
	public RedisTemplate redisTemplate(JedisConnectionFactory factory){
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		//配置序列化方式
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

		//设置redis支持数据库的事务
		redisTemplate.setEnableTransactionSupport(true);

		return redisTemplate;
	}

4、注入RedisTemplate,使用redis操作即可,如:opsForValue(),expire()(设置过期时间)

 

构造CacheManager时springboot版本比较: 

springboot1.x时用:CacheManager cacheManager=new RedisCacheManager(redisTemplate)

springboot2.x时用:RedisCacheManager redisCacheManager = RedisCacheManager.create(redisConnectionFactory)

(实现类是JedisConnectionFactory或LettuceConnectionFactory)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值