PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] How to connection pool memcached in Java (spymemcached)

 November 12, 2022     java, memcached, spymemcached     No comments   

Issue

The API I'm using, spymemcached, basically gives

MemcachedClient memc = new MemCachedClient(new InetSocketAddress("host", port));

It doesn't really give me a connect() or isConnected() function. The API offers a DefaultConnectionFactory but at first look at the code it doesn't look like it manages a connection pool. Does anyone know how to do this in spymemcached or in another Java memcached library?

More generally - what's the most "moral" way to make my application tolerant against loss of connection?


Solution

When you call the MemcachedClient constructor it automatically connects to your memcached server. There is no connect() or isConnected() method. If you lose the connection with Spymemcached it will try to reconnect for you. Also, the DefaultConnectionFactory is meant to be used to specify special connection attributes (e.g. hashing method and failure mode). If you want to use a connection factory then you need to use the MemcachedClient constructor that takes a ConnectionFactory and a List<InetSocketAddress>.

Spymemcached uses a single IO thread, but acts like a multithreaded client. With one thread for example, you can do up to 50k ops per second. If you want to create a thread pool then you will have to do it in your user application.

More generally - what's the most "moral" way to make my application tolerant against loss of connection?

Like I mentioned above, Spymemcached will attempt to reconnect if it loses connection to the server. This process will usually take around 17ms. Most people that use the client do however create a thread pool in their application code.



Answered By - mikewied
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing