redis的java客户端lettuce的使用

redis的java客户端lettuce的使用从官方文档翻译并整理的 有地方可能表述不准确文档地址 https github com lettuce io lettuce core wiki About lettuce1 lettuce 的介绍 lettuce 是一个线程安全的 redis 客户端 提供同步 异步和 reactive 的 APIs 如果可以避开阻塞和事务型的操作比如 BLPOP 和 MULTI EXEC 多个线程可以分

从官方文档翻译并整理的,有地方可能表述不准确

文档地址:About Lettuce · lettuce-io/lettuce-core Wiki · GitHub

1. lettuce的介绍

lettuce是一个线程安全的redis客户端。提供同步,异步和reactive(?)的 APIs.。如果可以避开阻塞和事务型的操作比如BLPOP 和MULTI/EXEC,多个线程可以分享同一个连接。多个连接被NIO框架netty有效的管理。

并且支持哨兵模式,集群模式和数据模式。

他的大部分方法对正好对应redis的命令。

2. RedisURI

1. 创建

RedisURI是redis连接的一些标准信息,比如需要提供数据库名称,密码,url,超时时间等。有三种方式可以创建:

  • RedisURI.create(“redis://localhost/”);
  • RedisURI.Builder.redis(“localhost”, 6379).auth(“password”).database(1).build();
  • new RedisURI(“localhost”, 6379, 60, TimeUnit.SECONDS);

2. uri的语法

  • 单独的redis:

redis :// [: password@] host [: port] [/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [&_database=database_]]

  • 单独的redis ssl:

rediss :// [: password@] host [: port] [/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [&_database=database_]]

  • 单独的redis unix的socket

redis-socket :// path [?[timeout=timeout[d|h|m|s|ms|us|ns]][&_database=database_]]

  • redis哨兵模式

redis-sentinel :// [: password@] host1[: port1] [, host2[: port2]] [, hostN[: portN]] [/ database][?[timeout=timeout[d|h|m|s|ms|us|ns]] [&_sentinelMasterId=sentinelMasterId_] [&_database=database_]]

3. 时间单位

  • d 天
  • h小时
  • m分钟
  • s秒
  • ms 毫秒
  • us 微秒
  • ns 纳秒

4. 示例

  • url和port
RedisClient client = RedisClient.create(RedisURI.create("localhost", 6379)); client.setDefaultTimeout(20, TimeUnit.SECONDS); // … client.shutdown();
  •  builder
RedisURI redisUri = RedisURI.Builder.redis("localhost") .withPassword("authentication") .withDatabase(2) .build(); RedisClient client = RedisClient.create(redisUri);
  • ssl builder
RedisURI redisUri = RedisURI.Builder.redis("localhost") .withSsl(true) .withPassword("authentication") .withDatabase(2) .build(); RedisClient client = RedisClient.create(redisUri);
  •  String RedisURI
RedisURI redisUri = RedisURI.create("redis://authentication@localhost/2"); RedisClient client = RedisClient.create(redisUri); // … client.shutdown();

3. 基本使用

  • maven
 
   
   
     biz.paluch.redis 
    
   
     lettuce 
    
   
     5.0.0.Beta1 
    
  
  • 建立连接
RedisClient client = RedisClient.create("redis://localhost"); StatefulRedisConnection 
  
    connect = client.connect(); 
  
  • 同步方式
RedisCommands 
  
    commands = connect.sync(); String value = commands.get("foo"); 
  
  • 异步方式
RedisAsyncCommands 
  
    redisAsync = connect.async(); RedisFuture 
   
     redisFuture = redisAsync.get("a"); try { String a = redisFuture.get(); System.out.println(a); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } 
    
  

RedisFuture的get方法是阻塞方法,会一直阻塞到返回结果,可以添加超时时间 

  • 批量操作
connection.setAutoFlushCommands(false); //批量操作 connection.flushCommands();

需要注意的是,setAutoFlushCommands方法会导致连接被阻塞,会影响其他代码的执行。

这里就需要用到连接池。其他用到连接池的地方如事物等。

  • 关闭连接
connection.close(); client.shutdown(); 

4. Reactive API

懵逼ing…

5. Pub Sub

发布订阅的使用

6. Transactions

事务的使用

7. dynamic Redis Command Interfaces

自定义命令

8. Master Slave

主从模式的使用

9. Redis Sentinel

哨兵模式的使用

10. Redis Cluster

集群模式的使用

11. 连接池的使用

lettuce是线程安全的,可以被多个线程同时使用,所以线程池不是必须的。lettuce提供了一般的连接池支持。

lettuce的连接池依赖common-pool2

 
   
   
     org.apache.commons 
    
   
     commons-pool2 
    
   
     2.4.3 
    
  

1. 连接池的归还两种方式

  • StatefulConnection.close()
  • GenericObjectPool.returnObject(…)

2. 基本使用

RedisClient client = RedisClient.create(RedisURI.create(host, port)); GenericObjectPool 
  
    > pool = ConnectionPoolSupport .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig()); // executing work try (StatefulRedisConnection 
   
     connection = pool.borrowObject()) { RedisCommands 
    
      commands = connection.sync(); commands.multi(); commands.set("key", "value"); commands.set("key2", "value2"); commands.exec(); } // terminating pool.close(); client.shutdown(); 
     
    
  

3. 集群使用

RedisClusterClient clusterClient = RedisClusterClient.create(RedisURI.create(host, port)); GenericObjectPool 
  
    > pool = ConnectionPoolSupport .createGenericObjectPool(() -> clusterClient.connect(), new GenericObjectPoolConfig()); // execute work try (StatefulRedisClusterConnection 
   
     connection = pool.borrowObject()) { connection.sync().set("key", "value"); connection.sync().blpop(10, "list"); } // terminating pool.close(); clusterClient.shutdown(); 
    
  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215398.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午2:01
下一篇 2026年3月18日 下午2:01


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号