memcached client: opening, closing and reusing connections

大城市里の小女人 提交于 2019-12-03 02:54:08

Spymemcached doesn't provide a connection pool, so every time I create a MemcachedClient instance I am creating a new connection right?

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.

Then when should I close the connection?

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.

Should I provide the same instance to all the threads in my application or create a new one every time?

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.

xmemcached does have a connection pool. In this case should I close connections I get from the pool?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!