首先解释一下Lettuce客户端:

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

1、添加依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

2、添加redis配置

spring:
redis:
host: ****
password:****
port: 6379
# 连接超时时间(毫秒)
timeout: 1000
# Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
database: 0
# 连接池配置
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制) 默认 8
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-wait: -1
# 连接池中的最大空闲连接 默认 8
max-idle: 8
# 连接池中的最小空闲连接 默认 0
min-idle: 0

3、实现逻辑

@Autowired
private StringRedisTemplate stringRedisTemplate; @Override
public String testRedis(){ ExecutorService executorService = Executors.newFixedThreadPool(1000);
IntStream.range(0, 1000).forEach(i -> executorService.execute(() -> stringRedisTemplate.opsForValue().increment("lcl",1))); System.out.println("lcl1=============" + stringRedisTemplate.opsForValue().get("lcl")); stringRedisTemplate.opsForValue().set("lcl1","val1"); String val1 = stringRedisTemplate.opsForValue().get("lcl1"); System.out.println("lcl1=============" + val1); String key = "redis:test:demo1"; User user = new User();
user.setId(100L);
user.setUsername("u2");
user.setPassword("p2");
stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(user)); String valUser = stringRedisTemplate.opsForValue().get(key);
System.out.println("redis:test:demo1=============" + valUser); User getUser = JSON.parseObject(valUser, User.class); System.out.println("redis:test:demo1=============" + getUser.getUsername()+ "========" + getUser.getPassword()); return null;
}

测试结果:

由于redis有String、list、set、zset、hash、geo等类型,因此使用时不止使用opsForValue()方法,具体的对应方法如下:

    opsForValue: 对应 String(字符串)

    opsForZSet: 对应 ZSet(有序集合)

    opsForHash: 对应 Hash(哈希)

    opsForList: 对应 List(列表)

    opsForSet: 对应 Set(集合)

    opsForGeo: 对应 GEO(地理位置)

SpringBoot--整合Lettuce redis的更多相关文章

  1. springboot整合mybatis,redis,代码(二)

    一 说明: springboot整合mybatis,redis,代码(一) 这个开发代码的复制粘贴,可以让一些初学者直接拿过去使用,且没有什么bug 二 对上篇的说明 可以查看上图中文件: 整个工程包 ...

  2. springboot整合mybatis,redis,代码(一)

    一 搭建项目,代码工程结构 使用idea或者sts构建springboot项目 二  数据库sql语句 SQLyog Ultimate v12.08 (64 bit) MySQL - 5.7.14-l ...

  3. SpringBoot整合集成redis

    Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...

  4. 整合Lettuce Redis

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...

  5. springboot整合mybatis,redis,代码(四)

    一 说明 这是spring整合redis注解开发的系类: 二 正文 在注解开发时候,会有这几个注解需要注意: 具体含义: 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时 ...

  6. springboot整合mybatis,redis,代码(五)

    redis注解开发过程中包含许多注解 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时表示该方法是支持缓存的,当标记在类上时则表示该类所有的方法都是支持缓存的.应用到读取 ...

  7. springboot整合mybatis,redis,代码(三)

    一 说明 接着上篇讲述redis缓存配置的用法: 二 正文 首先要使用缓存就必须要开开启缓存,第二步是需要开redis-server 下载redis包之后,点击图中两个都可以开启redis 怎么看是否 ...

  8. 三:Springboot整合Redis

    一:springboot整合redis redis版本:3.0.0 运行环境:linux 1.安装redis 1.1安装gcc yum install gcc-c++ 1.2解压redis.3.0.0 ...

  9. 【SpringBoot | Redis】SpringBoot整合Redis

    SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...

  10. SpringBoot(十一):SpringBoot整合Redis

    详解springboot整合redis:https://blog.csdn.net/qq_36781505/article/details/86612988 一.环境准备 Redis-x64-3.2. ...

随机推荐

  1. Chisel3 - util - Valid

    https://mp.weixin.qq.com/s/L5eAwv--WzZdr-CfW2-XNA   Chisel提供的Valid接口.如果valid为置1,则表明输出的bits有效:反之,则输出无 ...

  2. git可视化打包更新文件

    每次当我们修改了项目代码的时候,总需要理出来一个更新包发给测试部门或者给客户更新.当我们一次的修改的代码多了之后,我们就很难按照文件夹一个一个的去提交出来哪些的更新的,哪些是未修改的.于是乎就在度娘能 ...

  3. Java实现 LeetCode 187 重复的DNA序列

    187. 重复的DNA序列 所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对 ...

  4. JQuery实现对html结点的操作(创建,添加,删除)

    效果图: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  5. Java实现判断单联通(强连通缩点+拓扑排序)Going from u to v or from v to u

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  6. java实现最大镜像子串

    ** 最大镜像子串** [代码填空](满分12分) 串"abcba"以字母"c"为中心左右对称:串"abba" 是另一种模式的左右对称.这两 ...

  7. java实现第七届蓝桥杯凑算式

    凑算式 凑算式 B DEF A + - + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中AI代表19的数字,不同的字母代表不同的数字. 比如: 6+8 ...

  8. CORS跨域漏洞学习

    简介 网站如果存CORS跨域漏洞就会有用户敏感数据被窃取的风险. 跨域资源共享(CORS)是一种浏览器机制,可实现对位于给定域外部的资源的受控访问.它扩展了同源策略(SOP)并增加了灵活性.但是,如果 ...

  9. centos7上安装memcached以及PHP安装memcached扩展(一)

    安装memecached 第一步:安装libevent # tar zvxf libevent-2.1.8-stable.tar.gz # cd libevent-2.1.8-stable # ./c ...

  10. ucore系统 eclipse-cdt实验环境准备

    官网下载eclipse eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz省略安装jdk 等配置解压安装即可导入实验的项目 点击完成即可完成导入 开始配置qemu ...