java Redisson vs Jedis for redis
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42250951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redisson vs Jedis for redis
提问by Shubham Chaurasia
Now I have to use a java client for redis. I have come across Jedisand Redisson.
现在我必须为 redis 使用 java 客户端。我遇到了Jedis和Redisson。
EDIT: Reframing as the question was kind of opinion based.
编辑:重新定义问题是基于意见的。
Which is more efficient in terms of speed? Any benchmarks?
哪个在速度方面更有效?任何基准?
Which of them is able to provide the following?
他们中的哪一个能够提供以下内容?
Distributed locks(and update some keys in a map)
Auto key expiry notification but I want this to be received by only one particular subscriber from among a group of subscribers(Similar to consumer group concept in Apache Kafka). How this can be achieved?
分布式锁(并更新地图中的一些键)
自动密钥到期通知,但我希望只有一组订阅者中的一个特定订阅者收到此通知(类似于 Apache Kafka 中的消费者组概念)。如何做到这一点?
PS: Please don't mark it as duplicate of this.
PS:请不要将其标记为与此重复。
回答by mp911de
That question is opinion-based but lets get some objective points into it:
这个问题是基于意见的,但让我们得到一些客观的观点:
TL; DR:
TL; 博士:
The driver choice depends on multiple things:
驱动程序的选择取决于多种因素:
- Additional dependencies
- Programming model
- Scalability
- Being opinionated regarding the implementation of high-level features
- Prospect of your project, the direction in which you want to evolve
- 额外的依赖
- 编程模型
- 可扩展性
- 对高级功能的实现有意见
- 你的项目前景,你想要发展的方向
Explanation
解释
Additional dependencies
额外的依赖
Some projects are opinionated regarding additional dependencies and transient dependencies when adding a library.
一些项目在添加库时对附加依赖项和瞬态依赖项有自己的看法。
Jedis is almost dependency-free, it requires Apache Commons Pool 2 for connection-pooling.
Jedis 几乎是无依赖的,它需要 Apache Commons Pool 2 来进行连接池。
Redisson requires Netty, the JCache API and Project Reactor as basic dependencies. It's extensible because it integrates with a lot of other libraries (Tomcat Session store).
Redisson 需要 Netty、JCache API 和 Project Reactor 作为基本依赖项。它是可扩展的,因为它与许多其他库(Tomcat 会话存储)集成。
Programming model
编程模型
That's how you interact with your Redis client. It also defines the abstraction level.
这就是您与 Redis 客户端交互的方式。它还定义了抽象级别。
Jedis is a low-level driver exposing Redis API as Java method calls:
Jedis 是一个将 Redis API 公开为 Java 方法调用的低级驱动程序:
Jedis jedis = …;
jedis.set("key", "value");
List<String> values = jedis.mget("key", "key2", "key3");
Redisson is a high-level client that exposes its functionality through various API objects:
Redisson 是一个高级客户端,它通过各种 API 对象公开其功能:
Redisson redisson = …
RMap map = redisson.getMap("my-map"); // implement java.util.Map
map.put("key", "value");
map.containsKey("key");
map.get("key");
Each call invokes one or more Redis calls, some of them are implemented with Lua (Redis "Scripting").
每个调用都会调用一个或多个 Redis 调用,其中一些是用 Lua(Redis“脚本”)实现的。
Scalability
可扩展性
There are multiple drivers available for Java that come with various properties that might fit your project. Scalability plays into that as well. Looking at drivers it boils down how drivers, work with their resources and which programming models they support.
有多个可用于 Java 的驱动程序,它们带有可能适合您的项目的各种属性。可扩展性也发挥了作用。查看驱动程序可以归结为驱动程序如何使用其资源以及它们支持哪些编程模型。
Jedis uses blocking I/O and method calls are synchronous. Your program flow is required to wait until I/O is handled by the sockets. There's no asynchronous (Future
, CompletableFuture
) or reactive support (RxJava Observable
or Reactive Streams Publisher
).
Jedis 使用阻塞 I/O 并且方法调用是同步的。您的程序流需要等待,直到套接字处理 I/O。没有异步 ( Future
, CompletableFuture
) 或反应式支持(RxJavaObservable
或 Reactive Streams Publisher
)。
Jedis client instances are not thread-safe hence they require connection-pooling (Jedis-instance per calling thread).
Jedis 客户端实例不是线程安全的,因此它们需要连接池(每个调用线程的 Jedis 实例)。
Redisson uses non-blocking I/O and an event-driven communication layer with netty. Method calls are synchronous, asynchronous or reactive (via Project Reactor 2.0 or 3.1). Connections are pooled, but the API itself is thread-safe and requires fewer resources. I'm not entirely sure, but maybe you can even operate on a single connection. That's the most efficient way when working with Redis.
Redisson 使用非阻塞 I/O 和带有 netty 的事件驱动通信层。方法调用是同步的、异步的或反应式的(通过 Project Reactor 2.0 或 3.1)。连接是池化的,但 API 本身是线程安全的,需要的资源更少。我不完全确定,但也许您甚至可以对单个连接进行操作。这是使用 Redis 时最有效的方式。
Opinion about the client implementation
关于客户端实现的意见
These paragraphs deal with how the clients are implemented.
这些段落涉及如何实现客户端。
Both clients have an excellent feature coverage, and you can fulfill your requirements with both libraries.
两个客户端都具有出色的功能覆盖范围,您可以使用这两个库满足您的要求。
Jedis is a straightforward implementation that just writes commands to an OutputStream
and parses the responses. No more than that.
Jedis 是一个简单的实现,它只向 an 写入命令OutputStream
并解析响应。仅此而已。
If you want high-level features, then you need to implement these by using the Redis API. It gives you full control over the commands you invoke and the resulting behavior. Implementing your features might require additional efforts here.
如果您想要高级功能,则需要使用 Redis API 来实现这些功能。它使您可以完全控制您调用的命令和结果行为。在这里实现您的功能可能需要额外的努力。
Redisson is a high-level client that provides features through its abstractions. While you can use these objects without the need of knowing they are backed by Redis (Map
, List
, Set
, …), each API call translates to one or more Redis calls, some to Lua script execution.
Redisson 是一个高级客户端,通过其抽象提供功能。虽然您可以使用这些对象而无需知道它们由 Redis ( Map
, List
, Set
, ...) 支持,但每个 API 调用都会转换为一个或多个 Redis 调用,其中一些会转换为 Lua 脚本执行。
You might like or dislike the way Redisson behaves and how it implements the features, but in the end, there's not much you can do about it. Using Redissons high-level features might reduce your implementation efforts.
您可能喜欢或不喜欢 Redisson 的行为方式以及它如何实现功能,但最终,您对此无能为力。使用 Redissons 高级功能可能会减少您的实施工作。
Outlook
外表
That section entirely depends on where you're heading to. Jedis supports all Redis API commands, Redis Standalone, Redis Sentinel and Redis Cluster. There are no slave reads in master-slave setups, but I assume that's just a matter of time until jedis will provide these features.
该部分完全取决于您要去哪里。Jedis 支持所有 Redis API 命令、Redis Standalone、Redis Sentinel 和 Redis Cluster。主从设置中没有从读取,但我认为 jedis 提供这些功能只是时间问题。
With jedis, you can't go async and using advanced features of AWS ElastiCache or slave reads requires your own implementation.
使用 jedis,您不能异步,使用 AWS ElastiCache 或从读取的高级功能需要您自己的实现。
Redisson has a broad coverage of various setups. It supports all the things Jedis supports and provides read strategies for Master/Slave setups, has improved support for AWS ElastiCache.
Redisson 广泛涵盖了各种设置。它支持 Jedis 支持的所有内容,并为主/从设置提供读取策略,改进了对 AWS ElastiCache 的支持。