springboot整合redis-Lettuce
·
springboot整合redis-Lettuce
文档
- redis单机安装
- redis常用的五种数据类型
- redis数据类型-位图bitmap
- redis数据类型-基数统计HyperLogLog
- redis数据类型-地理空间GEO
- redis数据类型-流Stream
- redis数据类型-位域bitfield
- redis持久化-RDB
- redis持久化-AOF
- redis持久化-RDB+AOF混合模式
- redis持久化-纯缓存模式
- redis事务
- redis管道 -redis pipeline -redis pipelining
- redis发布订阅
- redis主从复制
- redis哨兵模式
- redis集群模式 -分区算法
- redis集群模式 -集群搭建
- redis集群模式 -节点故障 -节点主从调整
- redis集群模式 -扩缩容
- redis集群模式 -批量操作
- springboot整合redis-Jedis
官方文档
说明
- redis版本:7.0.0
- springboot版本:3.2.0
springboot整合redis -Lettuce
-
以单机版redis为例,安装redis参考文档:redis单机安装
-
引入依赖
<dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </dependency>- 版本号:
6.3.0.RELEASE,springboot3.2.0中默认的lettuce-core版本
- 版本号:
-
示例,使用
Lettuce连接单机版redispublic class LettuceDemo { public static void main(String[] args) { // 1. 指定redis服务的ip和端口,账号和密码 RedisURI uri = RedisURI.builder().withHost("192.168.145.132").withPort(6379).withAuthentication("default", "123456").build(); // 2. 创建连接客户端 RedisClient redisClient = RedisClient.create(uri); StatefulRedisConnection<String, String> connect = redisClient.connect(); // 3. 通过connect创建操作的command RedisCommands<String, String> commands = connect.sync(); // keys List<String> keys = commands.keys("*"); System.out.println(keys); // string String status = commands.set("k5", "hello-lettuce"); System.out.println(status); // OK String k5 = commands.get("k5"); System.out.println(k5); Boolean aBoolean = commands.expire("k5", 20L); System.out.println(aBoolean); // true // list commands.del("myList1"); Long count1 = commands.lpush("myList1", "v1", "v2", "v3"); System.out.println(count1); // 列表大小 List<String> list = commands.lrange("myList1", 0, -1); System.out.println(list); // [v3, v2, v1] // hash commands.del("myHash1"); commands.hset("myHash1", "name", "zhangsan"); Map<String, String> map = new HashMap<>(); map.put("id", "001"); map.put("age", "20"); commands.hset("myHash1", map); String name = commands.hget("myHash1", "name"); System.out.println(name); List<KeyValue<String, String>> hashList = commands.hmget("myHash1", "id", "name"); System.out.println(hashList); // [KeyValue[id, 001], KeyValue[name, zhangsan]] // set commands.del("mySet1"); long count2 = commands.sadd("mySet1", "s1", "s2", "s3", "s3", "s2"); System.out.println(count2); // 新增数 Set<String> set = commands.smembers("mySet1"); System.out.println(set); // [s2, s1, s3] // zset commands.del("myZset1"); commands.zadd("myZset1", 60d, "v1"); commands.zadd("myZset1", 70d, "v2"); commands.zadd("myZset1", 80d, "v3"); commands.zadd("myZset1", 80d, "v4"); commands.zadd("myZset1", 90d, "v5"); List<String> zsetList = commands.zrangebyscore("myZset1", 80d, 90d); System.out.println(zsetList); // [v3, v4, v5] // 4. 各种关闭释放资源 connect.close(); redisClient.shutdown(); } }
更多推荐

所有评论(0)