java版的memcached client及使用文档

来源:互联网 发布:淘宝打假团队 编辑:程序博客网 时间:2024/05/17 02:39
 官方网站 http://www.whalin.com/memc...

假设我们有3台memcached 服务器,server1 和server2 有3GB 的内存空间,server3 有2GB 的内存空间.
下面程序说明怎么来创建客户端.
import com.danga.MemCached.*;

public class MyClass {

// 创建一个 memcached 客户端对象

protected static MemCachedClient mcc = new MemCachedClient();

// 创建  memcached连接池

static

{  // 指定memcached服务地址 String[] servers =
{ "server1.mydomain.com:1121","server2.mydomain.com:1121",
"server3.mydomain.com:1121" };
// 指定memcached服务器负载量
Integer[]  weights    ={ 3, 3, 2 };
// 从连接池获取一个连接实例

SockIOPool pool = SockIOPool.getInstance();

// 设置服务器和服务器负载量

pool.setServers( servers );

pool.setWeights( weights );

// 设置一些基本的参数

//设置初始连接数5   最小连接数 5   最大连接数 250

//设置一个连接最大空闲时间6小时

pool.setInitConn( 5 );

pool.setMinConn( 5 );

pool.setMaxConn( 250 );

pool.setMaxIdle( 1000 * 60 * 60 * 6 );

// 设置主线程睡眠时间

// 每隔30秒醒来  然后

// 开始维护 连接数大小

pool.setMaintSleep( 30 );

// 设置tcp 相关的树形

// 关闭nagle算法

// 设置 读取 超时3秒钟  set the read timeout to 3 secs

//  不设置连接超时

pool.setNagle( false );

pool.setSocketTO( 3000 );

pool.setSocketConnectTO( 0 );

// 开始初始化 连接池

pool.initialize();

// 设置压缩模式

//如果超过64k压缩数据

mcc.setCompressEnable( true );

mcc.setCompressThreshold( 64 * 1024 );

}

public static void examples() {

mcc.set( "foo", "This is a test String" );
原创粉丝点击