首先解释一下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. 获取<input type="radio">被选中的内容

    背景: <input type="radio">,该标签表示的是单选按钮,这个类型相对于其他类型的获取,比较特殊,特此记录一下. 获取方式: 1. 使用选择器直接获取( ...

  2. JVM虚拟机 与 GC 垃圾回收

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.JVM体系结构概述 1.JVM 与系统.硬件 ​ JVM是运行在操作系统之上的,它与硬件没有直接的交 ...

  3. JS遍历对象修改属性名

    根据接口返回数据中number属性值,对数据进行截取,并改变属性名.直接上码: 下面是需要处理的数据 let data={"minValue":7400, "maxVal ...

  4. Qcom rampdump解析工具使用

    使用如下命令获取qcom工具: ljj@ljj-ThinkCentre-M83:~/git/qcom_tools/ramdump$ git clone git://codeaurora.org/qui ...

  5. Java实现 蓝桥杯VIP 算法训练 求完数

    问题描述 如果一个自然数的所有小于自身的因子之和等于该数,则称为完数.设计算法,打印1-9999之间的所有完数. 样例输出 与上面的样例输入对应的输出. 例: 数据规模和约定 1-9999 publi ...

  6. Java实现 LeetCode 42 接雨水

    42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这 ...

  7. java实现第六届蓝桥杯表格计算

    表格计算 某次无聊中, atm 发现了一个很老的程序.这个程序的功能类似于 Excel ,它对一个表格进行操作. 不妨设表格有 n 行,每行有 m 个格子. 每个格子的内容可以是一个正整数,也可以是一 ...

  8. 去摆摊吧,落魄的Java程序员

    真的,我也打算去摆摊,宣传语我都想好了.沉默王二,一枚有颜值却靠才华苟且的程序员,<Web 全栈开发进阶之路>作者,CSDN 明星博主,周排名第 4,总排名 40,这数据在众多互联网大咖面 ...

  9. MIT6.S081/6.828 实验1:Lab Unix Utilities

    Mit6.828/6.S081 fall 2019的Lab1是Unix utilities,主要内容为利用xv6的系统调用实现sleep.pingpong.primes.find和xargs等工具.本 ...

  10. <Android> Location Service 分析

    由于各种原因,老师希望我学习Android系统源码以应对可能参与的项目.我只好深入曹营,刺探军情了. 定位服务是手机上最常用的功能之一,据说也是相对比较简单的服务,所以从这里入手.其他系统服务的架构都 ...