memcached client: opening, closing and reusing connections

前端 未结 1 1746
感动是毒
感动是毒 2021-02-05 23:37

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

相关标签:
1条回答
  • 2021-02-06 00:18

    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.

    0 讨论(0)
提交回复
热议问题