Lettuce小试牛刀
Lettuce 支持使用完整的 Redis API 进行同步和异步通信,包括其数据结构、发布/订阅消息传递和高可用性服务器连接。
安装Redis
为了方便开发测试,直接利用windwos自带的Ubuntu。
sudo apt-get update
sudo apt-get install redis
windows自带的Ubuntu安装后报了一些warning,类似找不到linux的版本之类的,其实可以忽略。
启动redis
sudo service redis-server start
测试redis
redis-cli
127.0.0.1:6379> ping
PONG
在spring boot项目里添加Lettuce的依赖:
implementation 'io.lettuce:lettuce-core:6.2.2.RELEASE'
测试代码:
RedisClient redisClient = RedisClient .create("redis://password@localhost:6379/"); StatefulRedisConnection<String, String> connection = redisClient.connect();RedisCommands<String, String> syncCommands = connection.sync(); syncCommands.set("key", "Hello, Redis!"); String value = syncommands.get(“key”);
类型介绍
RedisClient 是一个可扩展且线程安全的 Redis 客户端,支持同步、异步和响应式执行模型。
如果多个线程避免阻塞和事务性操作(例如 BLPOP 和 MULTI/EXEC),则它们可以共享一个连接。
StatefulRedisConnection 是一个线程安全的连接(到redis server)。多个线程可以共享一个 StatefulRedisConnection。 ConnectionWatchdog 监视每个连接并自动重新连接,直到调用 StatefulConnection.close()。所有挂起的命令将在成功重新连接后(重新)发送。