<body>

kenshou

天空没有飞过的痕迹,但我已经努力飞过……

« Home | Next » | Next » | Next » | Next » | Next » | Next » | Next » | Next » | Next » | Next »

memcached的安装和简单测试

1:下载
memcached 下载地址 http://danga.com/memcached/download.bml
libevent http://www.monkey.org/~provos/libevent/
java客服端下载 http://www.whalin.com/memcached/
2:先安装libevent
tar zxvf libevent-1.4.3-stable
./configure -prefix=/usr
make;make install

安装memcached
tar zxvf memcached-1.2.5.tar.gz
#设置前面libevent的安装路径
./configure -with-libevent=/usr
make; make install

3启动memcached
memcached -d -m 1024 -p 11211 -u root
参数说明:
-d选项是启动一个守护进程
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址
-p是设置Memcache监听的端口,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定
-P是设置保存Memcache的pid文件

如果出现memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
说明没有找到文件:libevent-1.4.so.2

解决办法:
将libevent-1.4.so.2拷贝到任何一个列出的lib下或者软链接就可以了。
或者 修改.profile文件。加入:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:/opt/cuix/3rdParty/libevent-1.4.4/lib




4.编写java测试代码

package test.memcached;

import java.util.ArrayList;
import java.util.List;

import com.danga.MemCached.*;


public class MemcachedMain {

// 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 =
{
"192.168.1.9:11211",
};

Integer[] weights = {1};

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

// from here on down, you can call any of the client calls
public static void examples() {
final String FLAG="foo";
// mcc.delete(FLAG);
List listMem=(List) mcc.get(FLAG);
if(listMem!=null){
for(TBean tb:listMem){
System.out.println(tb.getName()+tb.getTb().getTest());
}
}else{
List list=new ArrayList();
for(int i=0;i<10;i++){
TBean tb=new TBean();
tb.setName("test"+i);
tb.getTb().setTest("asd"+i);
list.add(tb);
}
// System.out.println(tb.getName());
mcc.set( FLAG, list);
}

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

}
class Tb implements java.io.Serializable{
private String test;
public String getTest(){
return test;
}
public void setTest(String test){
this.test=test;
}
}
class TBean implements java.io.Serializable
{
private static final long serialVersionUID = 1945562032261336919L;
private String name;
Tb tb=new Tb();
public Tb getTb(){
return tb;
}
public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}

标签:

leave a response