从官方文档翻译并整理的,有地方可能表述不准确
文档地址:https://github.com/lettuce-io/lettuce-core/wiki/About-lettuce
lettuce是一个线程安全的redis客户端。提供同步,异步和reactive(?)的 APIs.。如果可以避开阻塞和事务型的操作比如BLPOP
和MULTI
/EXEC
,多个线程可以分享同一个连接。多个连接被NIO框架netty有效的管理。
并且支持哨兵模式,集群模式和数据模式。
他的大部分方法对正好对应redis的命令。
RedisURI是redis连接的一些标准信息,比如需要提供数据库名称,密码,url,超时时间等。有三种方式可以创建:
redis :// [: password@] host [: port] [/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [&_database=database_]]
rediss :// [: password@] host [: port] [/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [&_database=database_]]
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_]]
RedisClient client = RedisClient.create(RedisURI.create("localhost", 6379));
client.setDefaultTimeout(20, TimeUnit.SECONDS);
// …
client.shutdown();
RedisURI redisUri = RedisURI.Builder.redis("localhost")
.withPassword("authentication")
.withDatabase(2)
.build();
RedisClient client = RedisClient.create(redisUri);
RedisURI redisUri = RedisURI.Builder.redis("localhost")
.withSsl(true)
.withPassword("authentication")
.withDatabase(2)
.build();
RedisClient client = RedisClient.create(redisUri);
RedisURI redisUri = RedisURI.create("redis://authentication@localhost/2");
RedisClient client = RedisClient.create(redisUri);
// …
client.shutdown();
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.close();
client.shutdown();
懵逼ing...
发布订阅的使用
事务的使用
自定义命令
主从模式的使用
哨兵模式的使用
集群模式的使用
lettuce是线程安全的,可以被多个线程同时使用,所以线程池不是必须的。lettuce提供了一般的连接池支持。
lettuce的连接池依赖common-pool2
org.apache.commons
commons-pool2
2.4.3
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();
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();