java - 每个 servlet 一个 Lettuce Redis 连接?

标签 java multithreading servlets redis lettuce

在 servlet 中,应该在 init() 中创建 Lettuce Redis 连接并在 destroy() 中关闭,还是应该为每个请求创建一个连接(在 >doPost 还是 doGet?)

我正在使用同步RedisCommands (connection.sync())

Lettuce states :

Lettuce is thread-safe by design which is sufficient for most cases. All Redis user operations are executed single-threaded. Using multiple connections does not impact the performance of an application in a positive way. The use of blocking operations usually goes hand in hand with worker threads that get their dedicated connection. The use of Redis Transactions is the typical use case for dynamic connection pooling as the number of threads requiring a dedicated connection tends to be dynamic. That said, the requirement for dynamic connection pooling is limited. Connection pooling always comes with a cost of complexity and maintenance.

最佳答案

这取决于您正在追逐的场景。 如果您打算使用 Redis 事务,那么您必须确保正在进行的事务连接不被共享。 如果没有,那么在大多数情况下,一个连接就足够了,因为 redis 是单线程的,并且所有命令都将在命令队列中等待。

关于java - 每个 servlet 一个 Lettuce Redis 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61799175/

相关文章:

c# - SlimDX - 终止线程

java - Servlet 过滤器重定向 : How to redirect after chain. doFilter(request,resp);

Java如何保存表达式?

java - BigDecimal 不计算除法

java - 使用 KeyBIndings 响应按钮

Python并行编程模型

java - 如何在java中从毫秒X开始播放声音

c - 调用 SIGINT 时终止线程 - C

java - 如何将byte[]图像渲染到JSP?

java - 使用Tomcat和servlet时如何删除txt文件?