欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring boot集成redis lettuce代码实例

时间:2020-12-28 11:08:40|栏目:JAVA代码|点击:

spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端

引入依赖

<!-- spring boot redis 缓存引入 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <version>2.0.4.RELEASE</version>
    </dependency>

<!-- redis依赖commons-pool 这个依赖一定要添加 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>

配置文件

#Redis 配置
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=123456
#Redis数据库索引(默认为0)
spring.redis.database=0
##连接超时时间
spring.redis.timeout=60s

# 以下连接池已在SpringBoot2.0不推荐使用
##连接池最大连接数(使用负值表示没有限制)
#spring.redis.jedis.pool.max-active=10
##连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.jedis.pool.max-wait=-1ms
##连接池中的最大空闲连接
#spring.redis.jedis.pool.max-idle=8
##连接池中的最小空闲连接
#spring.redis.jedis.pool.min-idle=0

# Lettuce
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=10000
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
# 关闭超时时间
spring.redis.lettuce.shutdown-timeout=100

配置config

@Configuration
@AutoConfigureAfter(RedisConfig.class)
public class RedisConfig {
 
//  @Bean
//  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//    redisTemplate.setKeySerializer(new StringRedisSerializer());
//    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//    redisTemplate.setHashValueSerializer(new StringRedisSerializer());
//    redisTemplate.setValueSerializer(new StringRedisSerializer());
//    redisTemplate.setConnectionFactory(factory);
//    return redisTemplate;
//  }
 
  @Bean
  public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory factory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(factory);
    return template;
  }
 
  @Bean
  public HashOperations<String, String, String> hashOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForHash();
  }
 
  @Bean
  public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForValue();
  }
 
  @Bean
  public SetOperations<String, String> setOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForSet();
  }
 
  @Bean
  public ListOperations<String, String> listOperations(RedisTemplate<String, String> redisTemplate) {
    return redisTemplate.opsForList();
  }
}

上一篇:Java 生成二维码的工具资料整理

栏    目:JAVA代码

下一篇:Spring-data-redis操作redis知识总结

本文标题:Spring boot集成redis lettuce代码实例

本文地址:http://www.codeinn.net/misctech/37414.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有