springboot整合redis-Lettuce

文档

  1. redis单机安装
  2. redis常用的五种数据类型
  3. redis数据类型-位图bitmap
  4. redis数据类型-基数统计HyperLogLog
  5. redis数据类型-地理空间GEO
  6. redis数据类型-流Stream
  7. redis数据类型-位域bitfield
  8. redis持久化-RDB
  9. redis持久化-AOF
  10. redis持久化-RDB+AOF混合模式
  11. redis持久化-纯缓存模式
  12. redis事务
  13. redis管道 -redis pipeline -redis pipelining
  14. redis发布订阅
  15. redis主从复制
  16. redis哨兵模式
  17. redis集群模式 -分区算法
  18. redis集群模式 -集群搭建
  19. redis集群模式 -节点故障 -节点主从调整
  20. redis集群模式 -扩缩容
  21. redis集群模式 -批量操作
  22. springboot整合redis-Jedis

官方文档

  1. 官网操作命令指南页面:https://redis.io/docs/latest/commands/?name=get&group=string
  2. Redis cluster specification

说明

  1. redis版本:7.0.0
  2. springboot版本:3.2.0

springboot整合redis -Lettuce

  1. 以单机版redis为例,安装redis参考文档:redis单机安装

  2. 引入依赖

    <dependency>
        <groupId>io.lettuce</groupId>
        <artifactId>lettuce-core</artifactId>
    </dependency>
    
    • 版本号:6.3.0.RELEASE,springboot3.2.0中默认的lettuce-core版本
  3. 示例,使用Lettuce连接单机版redis

    public 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();
        }
    }
    

更多推荐