memcached 客户端:打开、关闭和重用连接
我一直在测试spymemcached 和xmemcached 客户端。我一直在尝试在项目文档中寻找答案,但效果很差。
我的问题是关于打开、关闭和重用连接。我在一份文档中找到了这一点:
客户端可以在不再需要连接时随时关闭连接。笔记, 然而,鼓励客户端缓存他们的连接而不是 而不是每次需要存储或检索数据时重新打开它们。缓存连接将消除与建立 TCP 连接相关的开销”。
不提供连接池,因此每次创建 MemcachedClient
实例时,我都会创建一个新连接,对吗?那么我什么时候应该关闭连接?我应该为应用程序中的所有线程提供相同的实例还是每次都创建一个新实例?
在这种情况下,我应该关闭从池中获得的连接吗?
I have been testing spymemcached and xmemcached clients. I have been trying to find answers in the projects documentation but it is very poor.
My questions are regarding opening, closing and reusing the connections. I found this in one document:
A client may just close the connection at any moment it no longer needs it. Note,
however, that clients are encouraged to cache their connections rather
than reopen them every time they need to store or retrieve data. Caching connections will eliminate the overhead associated with establishing a TCP connection".
Spymemcached doesn't provide a connection pool, so every time I create a MemcachedClient
instance I am creating a new connection right? Then when should I close the connection? Should I provide the same instance to all the threads in my application or create a new one every time?
xmemcached does have a connection pool. In this case should I close connections I get from the pool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,每次创建新的 MemcachedClient 对象时,都会创建一个新连接。每个连接对于应用程序来说都是异步的,因此即使只有一个连接也可能足以满足您的应用程序的需要。然而,有些人确实构建了 MemcachedClients 连接池。
一旦不再需要与 memcached 通信,就可以关闭连接。如果您的应用程序寿命较短,则需要关闭连接以使 jvm 停止,因为默认情况下 MemcachedClient 连接是守护程序连接。
对多个线程使用相同的连接。由于创建 TCP 连接的开销,为每个调用创建一个新连接将导致性能显着下降。
我不熟悉 xmemcached,但我想您只想创建几个(可能是 16 个)线程并与应用程序线程共享它们以获得最佳性能。
Yes, every time you create a new MemcachedClient object you create a new connection. Each connection appear asynchronous to the application so even having one connection will probably be enough for your application. Some people do however build a connection pool of MemcachedClients though.
You shut down connections as soon as you no longer need to communicate with memcached. If you application is short lived you need to shutdown the connection in order to get the jvm to stop since MemcachedClient connections are daemon connections by default.
Use the same connection with multiple threads. Creating a new connection for each call will cause a significant performance drop because of the overhead of creating a TCP connection.
I'm not familiar with xmemcached, but I would imagine you would only want to create a few (16 maybe) threads and share them with your application threads for the best performance.