SpringBoot2.0之Redis-lettuce连接

拼搏现实的明天。 2022-05-13 15:56 304阅读 0赞

什么是lettuce
LettuceJedis 的都是连接Redis Server的客户端程序。Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

导入依赖和添加配置

  1. <!-- lettuce pool 缓存连接池 -->
  2. <dependency>
  3. <groupId>org.apache.commons</groupId>
  4. <artifactId>commons-pool2</artifactId>
  5. </dependency>
  6. <!-- spring boot redis 缓存引入 -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-data-redis</artifactId>
  10. </dependency>
  11. # 添加配置
  12. spring:
  13. redis:
  14. # reids的连接ip
  15. host: 114.67.224.231
  16. # Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
  17. database: 0
  18. # 连接超时时间(毫秒)
  19. timeout: 10000ms
  20. # springboot2.0后默认使用lettuce连接redis,底层使用的是netty框架做支撑
  21. lettuce:
  22. pool:
  23. # 连接池中的最小空闲连接 默认 0
  24. min-idle: 0
  25. # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
  26. max-wait: -1ms
  27. # 连接池最大连接数(使用负值表示没有限制) 默认 8
  28. max-active: 8
  29. # 连接池中的最大空闲连接 默认 8
  30. max-idle: 8

自定义Template

  1. package com.ocean.springcloud.oceanspringboot2.test.configuration;
  2. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
  3. import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. import java.io.Serializable;
  11. /**
  12. * @author 季超
  13. * @create 2018-10-25 10:46
  14. * @desc
  15. **/
  16. @Configuration
  17. @AutoConfigureAfter(RedisAutoConfiguration.class)
  18. public class RedisCacheAutoConfiguration {
  19. @Bean
  20. public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
  21. RedisTemplate<String, Serializable> template = new RedisTemplate<>();
  22. template.setKeySerializer(new StringRedisSerializer());
  23. template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  24. template.setConnectionFactory(redisConnectionFactory);
  25. return template;
  26. }
  27. }

测试

  1. package com.ocean.springcloud.oceanspringboot2.test;
  2. import com.ocean.springcloud.oceanspringboot2.test.entity.User;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.core.StringRedisTemplate;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. import javax.annotation.Resource;
  11. import java.io.Serializable;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. import java.util.stream.IntStream;
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest
  17. @Slf4j
  18. public class ApplicationTests {
  19. @Resource
  20. private StringRedisTemplate stringRedisTemplate;
  21. @Resource
  22. private RedisTemplate<String, Serializable> redisCacheTemplate;
  23. @Test
  24. public void redisTest(){
  25. // TODO 测试线程安全
  26. ExecutorService executorService = Executors.newFixedThreadPool(1000);
  27. IntStream.range(0, 1000).forEach(i ->
  28. executorService.execute(() -> stringRedisTemplate.opsForValue().increment("kk", 1))
  29. );
  30. stringRedisTemplate.opsForValue().set("k1", "v1");
  31. final String k1 = stringRedisTemplate.opsForValue().get("k1");
  32. log.info("[字符缓存结果] - [{}]", k1);
  33. // TODO 以下只演示整合,具体Redis命令可以参考官方文档,Spring Data Redis 只是改了个名字而已,Redis支持的命令它都支持
  34. String key = "battcn:user:1";
  35. redisCacheTemplate.opsForValue().set(key, new User(1L, "u1", "pa"));
  36. // TODO 对应 String(字符串)
  37. final User user = (User) redisCacheTemplate.opsForValue().get(key);
  38. log.info("[对象缓存结果] - [{}]", user);
  39. }
  40. }

是Redis其它类型所对应的操作方式

  • opsForValue: 对应 String(字符串)
  • opsForZSet: 对应 ZSet(有序集合)
  • opsForHash: 对应 Hash(哈希)
  • opsForList: 对应 List(列表)
  • opsForSet: 对应 Set(集合)
  • opsForGeo: 对应 GEO(地理位置

发表评论

表情:
评论列表 (有 0 条评论,304人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Springboot连接Redis

    前言 随着数据量的日益增长,很多时候如果直接用SELECT \ FROM XXX这些SQL语句来对上千万的数据进行查询,那耗费的时间可想而知。 作为用户一方,肯定希望系