Memcached-Java-Client 使用

假设有三台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 );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值