memcached java client

yuezu1026 2009-09-05 08:23:28
我使用memcached java client,里面使用两台机器做mecached server,然后用两个客户端去访问缓存,有的时候可以获取到存放的数据,有的时候获取不到。不知道是为什么?
...全文
396 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
左大神在这 2010-01-04
  • 打赏
  • 举报
回复
问题解决了吗,什么原因?
cena_168 2009-09-25
  • 打赏
  • 举报
回复
解决了你就把答案发上来。。 不然你太不后到了。
mrshelly 2009-09-11
  • 打赏
  • 举报
回复
应该是你 socket pool 里的设计问题.

当你添加一个 key 到server 时,要把 pool 清掉. 其它 server 生效时,再添加到 pool 里来.
hzrui 2009-09-10
  • 打赏
  • 举报
回复
呵呵,qq上跟你聊过了赚点分数,^_^
yuezu1026 2009-09-06
  • 打赏
  • 举报
回复
也就是说,我在两台机器上启动上面的程序,但是有的时候,在一台机器上把key,是放进去了。但是另一台取不到这个key的数据。
yuezu1026 2009-09-06
  • 打赏
  • 举报
回复
package com.danga.MemCached.test;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;


public class Test {
protected static MemCachedClient mcc = new MemCachedClient();

static {
String[] servers ={"192.168.40.4:12000","192.168.40.5:12000"};

Integer[] weights = { 3 , 3};

//创建一个实例对象SockIOPool
SockIOPool pool = SockIOPool.getInstance();

// set the servers and the weights
//设置Memcached Server
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 );

// Tcp的规则就是在发送一个包之前,本地机器会等待远程主机
// 对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,
// 以至这个包准备好了就发;
pool.setNagle( false );
//连接建立后对超时的控制
pool.setSocketTO( 3000 );
//连接建立时对超时的控制
pool.setSocketConnectTO( 0 );

// initialize the connection pool
//初始化一些值并与MemcachedServer段建立连接
pool.initialize();


// lets set some compression on for the client
// compress anything larger than 64k
mcc.setCompressEnable( true );
mcc.setCompressThreshold( 64 * 1024 );
}

public static void bulidCache(){
//set(key,value,Date) ,Date是一个过期时间,如果想让这个过期时间生效的话,这里传递的new Date(long date) 中参数date,需要是个大于或等于1000的值。
//因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ,也就是说,如果 小于1000的值,除以1000以后都是0,即永不过期
mcc.set( "test", "This is a test String" ,new Date(10000)); //十秒后过期

}

public static void output() {
//从cache里取值
String value = (String) mcc.get( "test" );
System.out.println(value);
}

public static void main(String[] args){
bulidCache();
output();
}

}
mrshelly 2009-09-06
  • 打赏
  • 举报
回复
不明白你用的哪个 client class, 怎么添加server 的.

25,980

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧