1下载libevent和memcached
http://libevent.org/
http://memcached.org/downloads
将libevent-2.0.21-stable.tar.gz 和 memcached-1.4.15.tar.gz上传到服务器/home
2首先安装libevent
Memcache用到了libevent这个库用于Socket的处理.
解压,安装:
# tar -zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# ./configure –prefix=/usr
# make
# make install
3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 Jul 23 18:14 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x 1 root root 1069593 Jul 23 18:14 libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 2063298 Jul 23 18:14 libevent.a
lrwxrwxrwx 1 root root 26 Jul 23 18:14 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 645672 Jul 23 18:14 libevent_core-2.0.so.5.1.9
-rw-r--r-- 1 root root 1293914 Jul 23 18:14 libevent_core.a
-rwxr-xr-x 1 root root 979 Jul 23 18:14 libevent_core.la
还不错,都安装上了。
4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /home
# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
# ./configure –with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,
5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 377904 Jul 23 18:27 /usr/local/bin/memcached
启动memcached
/usr/local/bin/memcached -d -m 10 -u root -p 12000 -c 256 -P /home/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文件,我这里是保存在 /home/memcached.pid
查看memcached是否启动
ps -ef | grep mem
结束
kill 9 ID
在linux配置好memcached,java代码:
javabean序列化后可以放入缓存
implements Serializable
http://libevent.org/
http://memcached.org/downloads
将libevent-2.0.21-stable.tar.gz 和 memcached-1.4.15.tar.gz上传到服务器/home
2首先安装libevent
Memcache用到了libevent这个库用于Socket的处理.
解压,安装:
# tar -zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# ./configure –prefix=/usr
# make
# make install
3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 Jul 23 18:14 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x 1 root root 1069593 Jul 23 18:14 libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 2063298 Jul 23 18:14 libevent.a
lrwxrwxrwx 1 root root 26 Jul 23 18:14 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 645672 Jul 23 18:14 libevent_core-2.0.so.5.1.9
-rw-r--r-- 1 root root 1293914 Jul 23 18:14 libevent_core.a
-rwxr-xr-x 1 root root 979 Jul 23 18:14 libevent_core.la
还不错,都安装上了。
4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /home
# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
# ./configure –with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,
5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 377904 Jul 23 18:27 /usr/local/bin/memcached
启动memcached
/usr/local/bin/memcached -d -m 10 -u root -p 12000 -c 256 -P /home/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文件,我这里是保存在 /home/memcached.pid
查看memcached是否启动
ps -ef | grep mem
结束
kill 9 ID
在linux配置好memcached,java代码:
public static void main(String[] args) {
MemCachedClient mcc = new MemCachedClient();
SockIOPool sUpool = null;
String[] servers = { "127.0.0.1:12000" };//装好缓存的ip地址以及端口
Integer[] weights = { 3, 3, 2 };
sUpool = SockIOPool.getInstance();
sUpool.setServers(servers);
sUpool.setWeights(weights);
sUpool.setMaintSleep(30);
sUpool.setNagle(false);
sUpool.setSocketTO(3000);
sUpool.setSocketConnectTO(0);
sUpool.initialize();
// List list=new ArrayList();
// for(int i=0;i<5;i++){
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("name", "tom");
// map.put("sex", "男");
// map.put("age", i);
// list.add(map);
// }
//
mcc.delete("a");//清空缓存
// mcc.add("a", list);
System.out.println( mcc.get("a"));
}
javabean序列化后可以放入缓存
implements Serializable