网友的相关建议:
一、什么是memcache
memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等
二、libevent介绍
libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名的用于apache的php缓存库memcached据说也是libevent based,而且libevent在使用上可以做到跨平台
三、准备工作
下载:memcache:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz
下载:http://www.monkey.org/~provos/libevent-1.3.tar.gz
四、安装过程
1、卸载低版本的libevent
#ls -al /usr/lib |grep libevent
lrwxrwxrwx 1 root root libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
-rwxr-xr-x 1 root root libevent-1.1a.so.1.0.2
查看当前libevent版本,如果版本低于1.3,建议先卸载
#rpm -e libevent --nodeps
卸载libevent,
#ls -al /usr/lib |grep libevent
再次查看,卸载成功
2、安装libevent
#tar zxvf libevent-1.3.tar.gz
解压libevent
#cd libevent-1.3
#./configure --prefix=/usr
#make
#make install
配置安装libevent到/usr目录下
#ls -al /usr/lib |grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
再此查看,安装libevent1.3版本成功
3、安装memcached,同时需要安装中指定libevent的安装位置
#tar zxvf memcached-1.2.6.tar.gz
#cd memcached-1.2.6
解压进入mamcache目录
#./configure --with-libevent=/usr/
#make
#make install
安装完成后会把memcached放到 /usr/local/bin/memcached
#ls -al /usr/local/bin/memcached
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
查看memcache安装成功
五、memcached的基本设置
#/usr/local/bin/memcached -d -m 2000 -u root -p 12000 -c 256 -P ./memcached.pid
1.启动Memcache的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
也可以启动多个守护进程,不过端口不能重复。
六:客户端测试
1、下载java_memcached-release_2.5.1.zip
2、创建一个java project,将java_memcached-release_2.5.1.jar包引用。
3、在main函数中,创建2个类,如下
package com.danga.MemCached;
import java.io.Serializable;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
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();
}
}
package com.danga.MemCached;
import java.io.Serializable;
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 ={"192.168.0.226:12000"};
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("ID : "+obj.getId()+"
"+"Name : "+obj.getName());
}
public static void main(String[] args)
{
bulidCache();
output();
}
}
4、运行结果
This is a test String
ID : 1
Name : test
memcache配置成功~~~~~~~~~~~~~~~~~~~~