### Configure Memcached Java Client Connection Pool Source: https://github.com/gwhalin/memcached-java-client/blob/master/doc/HOWTO.txt This snippet demonstrates how to initialize and configure the `SockIOPool` for the Memcached Java client, including setting server addresses, weights, connection limits, idle times, maintenance sleep, and TCP settings. It also shows how to enable compression for the `MemCachedClient`. ```Java import com.danga.MemCached.*; public class MyClass { // create a static client as most installs only need // a single instance protected static MemCachedClient mcc = new MemCachedClient(); // set up connection pool once at class load static { // server list and weights String[] servers = { "server1.mydomain.com:1624", "server2.mydomain.com:1624", "server3.mydomain.com:1624" }; Integer[] weights = { 3, 3, 2 }; // grab an instance of our connection pool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights pool.setServers( servers ); pool.setWeights( weights ); // set some basic pool settings // 5 initial, 5 min, and 250 max conns // and set the max idle time for a conn // to 6 hours pool.setInitConn( 5 ); pool.setMinConn( 5 ); pool.setMaxConn( 250 ); pool.setMaxIdle( 1000 * 60 * 60 * 6 ); // set the sleep for the maint thread // it will wake up every x seconds and // maintain the pool size pool.setMaintSleep( 30 ); // set some TCP settings // disable nagle // set the read timeout to 3 secs // and don't set a connect timeout pool.setNagle( false ); pool.setSocketTO( 3000 ); pool.setSocketConnectTO( 0 ); // initialize the connection pool pool.initialize(); // lets set some compression on for the client // compress anything larger than 64k mcc.setCompressEnable( true ); mcc.setCompressThreshold( 64 * 1024 ); } // from here on down, you can call any of the client calls public static void examples() { mcc.set( "foo", "This is a test String" ); String bar = mcc.get( "foo" ); } } ``` -------------------------------- ### Disable Memcached Java Client Failover and Failback Source: https://github.com/gwhalin/memcached-java-client/blob/master/doc/HOWTO.txt This snippet illustrates how to disable the automatic failover and failback mechanisms of the Memcached Java client. This can be useful in environments with unstable servers to prevent frequent server switching. ```Java pool.setFailover( false ); pool.setFailback( false ); ``` -------------------------------- ### Configure Memcached Java Client for Multi-Client Compatibility Source: https://github.com/gwhalin/memcached-java-client/blob/master/doc/HOWTO.txt This snippet shows how to adjust `SockIOPool` and `MemCachedClient` settings to ensure compatibility with other Memcached clients (e.g., PHP, Perl). It covers using a compatible hashing algorithm, storing primitives as strings, and disabling key sanitization. ```Java // use a compatible hashing algorithm pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH ); // store primitives as strings // the java client serializes primitives // // note: this will not help you when it comes to // storing non primitives mcc.setPrimitiveAsString( true ); // don't url encode keys // by default the java client url encodes keys // to sanitize them so they will always work on the server // however, other clients do not do this mcc.setSanitizeKeys( false ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.