Redis Lettuce 及 Spring 客户端 使用方法

编程入门 行业动态 更新时间:2024-10-27 22:22:34

Redis Lettuce 及 Spring 客户端 <a href=https://www.elefans.com/category/jswz/34/1769874.html style=使用方法"/>

Redis Lettuce 及 Spring 客户端 使用方法

大佬文章链接(44条消息) Redis高级客户端Lettuce详解_huayang183的博客-CSDN博客_lettuce

目录

lettuce客户端命令集

两种获取方式

五种数据类型使用

排序

分布式锁

TTL

持久化

配置

快照

AOF

主从(复制)

非事务型流水线(pipline)

降低内存占用

配置

分片

哨兵

集群

SpringBoot使用redis

基本配置

获取指令集方式

1.使用封装的api

2.使用原生指令集(轻度封装,但指令和格式没发现变化)


lettuce客户端命令集

两种获取方式

1.直接获取

    public static RedisClient client;public static StatefulRedisConnection<String, String> connect;public static RedisAsyncCommands<String, String> commands;static{client= RedisClient.create("redis://localhost:6379");connect = client.connect();commands = connect.async();
//异步模式下是否自动刷新命令,相当于piplinecommands.setAutoFlushCommands(true);}

2.使用redisuri

    RedisURI redisUri = RedisURI.builder()                    // <1> 建立单机链接的链接信息.withHost("localhost").withPort(6379).withTimeout(Duration.of(10, ChronoUnit.SECONDS)).build();RedisClient redisClient = RedisClient.create(redisUri);   // <2> 建立客户端StatefulRedisConnection<String, String> connection = redisClient.connect();     // <3> 建立线程安全的链接RedisCommands<String, String> redisCommands = connection.sync();                // <4> 建立同步命令//异步模式下是否自动刷新命令,相当于piplinecommands.setAutoFlushCommands(true);

五种数据类型使用

和redis命令行使用的命令和格式基本相同,忘了看下命令集源码,略过。

排序

(44条消息) Redis研究(十七)—SORT排序_小地盘的博客-CSDN博客_sort排序

事务及乐观锁

   RedisAsyncCommands<String, String> commands = Connectmands;@Testpublic void test(){commands.watch("516741");//加乐观锁commands.multi();//开启事务commands.set("516741","test8765657865746878687");commands.unwatch();//取消乐观锁commands.discard();//取消事务commands.exec();//执行事务}

分布式锁

基本命令,详细加锁过程见  redis实战 中完整加锁过程。

        commands.setnx("lock_key","value");commands.msetnx(new HashMap<>());commands.hsetnx("hash","key","value");

TTL

    RedisAsyncCommands<String, String> commands = Connectmands;@Testpublic void test(){commands.ttl("key");//查看key距离过期还有几秒commands.expire("key",122);//key在指定秒后过期commands.persist("key");//移除键的过期时间commands.expireat("key",1222222);//将键的过期时间设置为给定的unix时间戳commands.pexpire("key",122);//让键在指定的毫秒数后过期commands.pexpireat("key",132);//将一个毫秒级精度的unix时间戳设置为键的过期时间commands.pttl("key");//查看key距离过期时间还有多少毫秒}

持久化

Redis 中的数据持久化策略(RDB) - Single_Yam - 博客园 (cnblogs)

Redis详解(七)------ AOF 持久化 - YSOcean - 博客园 (cnblogs)

配置

#快照持久化配置#从最近一次创建快照后开始计算,如60秒内有1000次写入,则使用BGSAVE更新快照,配置多个save选项时,同时生效。
save 60 1000 #是否在创建快照失败后停止写入
stop-writes-on-bgsave-error no# rdbcompression 配置为 yes,那么即代表 redis 进行 RDB 文件生成中,如果遇到字符串对象并且其中的字符串值占用超过 20 个字节,那么就会对字符串进行 LZF 算法进行压缩。
rdbcompression yes#文件名
dbfilename filename.rdb#rdbchecksum 配置 redis 是否使用 CRC64 校验算法校验 RDB 文件是否发生损坏,默认开启状态,如果你需要提升性能,可以选择性关闭。rdbchecksum yes
#################################################################AOF配置#是否使用AOF持久化appendonly no#同步频率,有三个选项,always,no,everysec,对应每次,操作系统决定,每秒
appendfsync everysec#aof文件名,默认是"appendonly.aofappenddilename aof文件名#为yes表示rewrite期间对新写操作不fsync,暂时存在内存中,等rewrite完成后再写入,默认为no,建议yes。
no-appendfsync-on-rewrite no#aof自动重写配置,当目前aof文件大小超过上一次重写的aof文件大小的百分之多少进行重写
auto-aof-rewrite-percentage 100#设置允许重写的最小aof文件大小,避免了达到约定百分比但尺寸仍然很小的情况还要重写。
auto-aof-rewrite-min-size 64mb#如果选择的是yes,当截断的aof文件被导入的时候,会自动发布一个log给客户端然后load。如果是no,用户必须手动redis-check-aof修复AOF文件才可以。默认值为 yes。aof-load-truncated yes#aof和rdb文件目录dir ./

快照

        commands.bgsave();//新fork一个子进程,创建快照commands.save();//阻塞当前redis进程,创建快照,完成前不响应其他命令。

AOF

     commands.configSet("appendonly","yes");//配置文件没有改变,但奇怪的是原本没有的aof文件出现了commands.bgrewriteaof();//异步重写AOF文件,在执行后aof修改时间更新了,应该有效。

主从(复制)

    commands.slaveof("127.0.0.1",6379);//设置master,并复制mastercommands.slaveofNoOne();//Promote server as master.恢复为master。

非事务型流水线(pipline)

Lettuce中没找到pipline,键开篇那位大佬文章提到,Lettuce async命令类似于Jedis 中 pipline

void flushCommands()
Flush pending commands. This commands forces a flush on the channel and can be used to buffer ("pipeline") commands to achieve batching. No-op if channel is not connected

       //异步模式下是否自动刷新命令,相当于piplinecommands.setAutoFlushCommands(true);//执行缓存的命令,手动刷新//Flush pending commands. This commands forces a flush on the channel and can be used to buffer ("pipeline") commands to achieve batching. No-op if channel is not connectedcommands.flushCommands();

降低内存占用

短结构,配合数据分片使用来降低内存占用,可见 redis实战。

配置

#每个列表最大元素数量list-max-ziplist-entries 512#每个列表所有节点最大体积list-max-ziplist-value 64hash-max-ziplist-entires 512hash-max-ziplist-value 64zset-max-ziplist-entries 128zset-max-ziplist-value 64

分片

见redis实战,仅供参考,思路就是利用hash函数计算应该将数据写入哪个key中,缺点是聚合计算困难。

哨兵

哨兵构建(Windios环境下)及 创建链接(见大佬文章的高可用和分片,没试过)

(44条消息) redis哨兵_qq_52390606的博客-CSDN博客

集群

集群搭建(window环境下)及 创建链接(可能没有,当时没找到)

(44条消息) Redis cluster_qq_52390606的博客-CSDN博客

SpringBoot使用redis

Spring Boot 2.X(六):Spring Boot 集成 Redis-阿里云开发者社区 (aliyun)

基本配置

Spring Boot 2 Redis配置项总结 - 简书 (jianshu)

server.port=80
server.servlet.context-path=/#reids相关配置
#redis服务器地址
spring.redis.host=localhost
#雷迪森服务器端口
spring.redis.port=6379
#redis密码,默认为空
spring.redis.password=
#redis数据库索引(默认为0)
spring.redis.database=0
#连接池对打阻塞等待时间(负表示没有限制)
spring.redis.jedis.pool.max-wait=10000
#连接池最大连接数(负表示没有限制)
spring.redis.jedis.pool.max-active=100
#连接池中的最大空闲链接
spring.redis.jedis.pool.max-idle=20
#连接池中的最小空闲链接
spring.redis.jedis.pool.min-idle=0
#链接超时时间
spring.redis.timeout=3000

获取指令集方式

1.使用封装的api

redisTemplate.opsForValue();//使用被封装的各种指令集

2.使用原生指令集(轻度封装,但指令和格式没发现变化)

RedisConnectionFactory redisConnectionFactory=redisTemplate.getRequiredConnectionFactory();
RedisConnection redisConnection = redisConnectionFactory.getConnection();
//释放连接
RedisConnectionUtils.releaseConnection(redisConnection,redisConnectionFactory);  

使用原生指令集,其余操作可参考Lettuce操作(上边的),使用封装的api没试过。

要自己实现序列化(json格式)。

更多推荐

Redis Lettuce 及 Spring 客户端 使用方法

本文发布于:2024-02-08 14:49:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1674200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用方法   客户端   Redis   Lettuce   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!