1.memcached是什么?
       memcached is a high-performance,
distributed memory object caching system, generic in nature, but
intended for use in speeding up dynamic web applications by alleviating
database load.
2.下载地址
     
3.安装
     跟一般的linux软件安装一样 ./configure 然后 make,make install
     需要一个库 libevent
    下载地址:
     http://www.monkey.org/~provos/libevent/
     安装库后创建个链接
     ln -s /usr/local/lib/libevent-1.3b.so.1 /lib/libevent-1.3b.so.1
4.启动
     memcached -u nobody -d -m 50 -l 192.168.0.8 -p 10001
    -u 指定用户
    -m 指定缓存使用内存大小,只是测试,就用了50m
    -l 指定ip地址
    -p 指定端口
5.测试
   memcached是一个数据源,所以与应用是独立的,理论上支持任何语言访问。现在已有的client库可在下面的地址得到:
   http://www.danga.com/memcached/apis.bml
   我们用java测试,jdk为1.6

java 代码

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

 public class MemcacheTest {

 // 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 ={"8:10001"};

Integer[] weights = { 3 };

// 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 );
}

public static void bulidCache(){
mcc.set( "foo", "This is a test String" );
TestObj obj = new TestObj();
obj.setId(new Long(1));
obj.setName("test");
mcc.set("testObj", obj);
}

// from here on down, you can call any of the client calls
public static void output() {
//
String bar = (String) mcc.get( "foo" );
System.out.println(bar);
TestObj obj = (TestObj)mcc.get("testObj");
System.out.println(obj);
}

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

}

 

import java.io.Serializable;

 public class TestObj implements Serializable {

 private static final long serialVersionUID = 1L;
 private String name;
private Long id;
/**
 *
 */
public TestObj() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String toString(){
return "id:" this.getId() ";name:" this.getName();
}
}

 从faq上找了段代码,稍做改动。

运行,成功输出:

This is a test String
com.danga.MemCached.MemCachedClient Sat Jul 14 18:59:46 CST 2007 – deserializing class net.teamhot.memecache.TestObj
id:1;name:test

中间两行是日志。
把buildCache方法注释了,再运行,成功。

如果是两种语言共享cache,要注意对象的持久化方式。

6.可能遇到的问题
   如果连接不成功请用telent测试服务器端口是否能连接通。连不通请更改服务器的防火墙设置,或者干脆把防火墙关闭了。

   /etc/init.d/iptables stop