假设有三台server,Server 1 和 server 2有3GB内存,server 3有2GB 内存空间用于缓存,
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 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();
}
// 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" ).toString();
}
如果想支持多种客户端 (i.e. Java, PHP, Perl, etc.)
需要如下设置
// use a compatible hashing algorithm
pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );