Lettuce是一个简单易用的Redis客户端库,我们这里简单了解下其用法。
官方网站:https://lettuce.io/
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.1.8.RELEASE</version>
</dependency>
如果单独使用Lettuce,只需要引入lettuce-core
即可。
Lettuce的使用非常简单,而且API设计也十分优雅,我们直接看一个例子。
package com.gacfox.demolettuce;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
public class Main {
public static void main(String[] args) {
// 建立Redis连接
RedisURI redisURI = RedisURI.builder().withHost("127.0.0.1").withPort(6379).build();
RedisClient client = RedisClient.create(redisURI);
StatefulRedisConnection<String, String> connection = client.connect();
// 执行操作
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("username", "Alice");
// 关闭连接
connection.close();
client.shutdown();
}
}
上面代码中,我们先建立了一个Redis连接,然后执行了一个SET命令,向Redis数据库中写入了一条数据,最后关闭了连接。这里注意Lettuce的连接对象StatefulRedisConnection
是线程安全的,可以直接在多线程环境下使用。
我们执行操作时,使用了connection.sync()
方法,获取了一个命令操作接口对象,该方法操作Redis是同步阻塞式的。实际上,Lettuce也支持异步和Reactor式操作模式。
Lettuce能够很简单是实现Redis发布订阅模式,下面是一个例子。
package com.gacfox.demolettuce;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
public class Main {
public static void main(String[] args) {
// 建立Redis连接
RedisURI redisURI = RedisURI.builder().withHost("127.0.0.1").withPort(6379).build();
RedisClient client = RedisClient.create(redisURI);
StatefulRedisConnection<String, String> connection = client.connect();
// 执行操作
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.publish("channel", "Alice");
// 关闭连接
connection.close();
client.shutdown();
}
}
package com.gacfox.demolettuce;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.pubsub.RedisPubSubListener;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
public class Main {
public static void main(String[] args) {
// 建立Redis连接
RedisURI redisURI = RedisURI.builder().withHost("127.0.0.1").withPort(6379).build();
RedisClient client = RedisClient.create(redisURI);
StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
// 注册监听器
connection.addListener(new RedisPubSubListener<String, String>() {
@Override
public void unsubscribed(String channel, long count) {
System.out.println("unsubscribed: channel[" + channel + "] count[" + count + "]");
}
@Override
public void subscribed(String channel, long count) {
System.out.println("subscribed: channel[" + channel + "] count[" + count + "]");
}
@Override
public void punsubscribed(String pattern, long count) {
System.out.println("punsubscribed: pattern[" + pattern + "] count[" + count + "]");
}
@Override
public void psubscribed(String pattern, long count) {
System.out.println("psubscribed: pattern[" + pattern + "] count[" + count + "]");
}
@Override
public void message(String pattern, String channel, String message) {
System.out.println("message: pattern[" + pattern + "] channel[" + channel + "] message[" + message + "]");
}
@Override
public void message(String channel, String message) {
System.out.println("message: channel[" + channel + "] message[" + message + "]");
}
});
// 订阅
RedisPubSubCommands<String, String> syncCommands = connection.sync();
syncCommands.subscribe("channel");
// 出于演示目的,防止主线程退出
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
上面代码分别是发布端程序和订阅端程序。从代码中我们可以得知,Lettuce的PubSub模式采用了监听器模式来实现,RedisPubSubListener
会在单独的线程中被回调。当收到订阅的消息时,message()
方法会被回调。
运行结果如下。
subscribed: channel[channel] count[1]
message: channel[channel] message[Alice]
message: channel[channel] message[Alice]