memcache是一款流行的缓存产品,它分为两个部分:一个是运行在服务器端的memcached进程,一个是在客户端进行调用获取缓存中数据客户端,例如比较常用的PHP客户端。这里,记录一下安装服务器端的memcached的过程,比较容易。
准备软件包
- libevent-2.0.16-stable.tar.gz
- memcached-1.4.10.tar.gz
安装配置
安装libevent-2.0.16
安装libevent-2.0.16的过程如下所示(注意,安装需要root账户权限):
- [email protected] www.linuxidc.com:/home/shirdrn# tar -xvzf libevent-2.0.16-stable.tar.gz
- [email protected] www.linuxidc.com:/home/shirdrn# cd libevent-2.0.16-stable/
- [email protected] www.linuxidc.com:/home/shirdrn/libevent-2.0.16-stable# ./configure
- [email protected] www.linuxidc.com:/home/shirdrn/libevent-2.0.16-stable# make
- [email protected] www.linuxidc.com:/home/shirdrn/libevent-2.0.16-stable# make install
安装完成以后,通过下面的方式来验证是否安装成功:
- [email protected] www.linuxidc.com:/usr/local# ls -al /usr/local/lib | grep libevent
- lrwxrwxrwx 1 root root 21 2011-12-15 14:52 libevent-2.0.so.5 -> libevent-2.0.so.5.1.4
- -rwxr-xr-x 1 root root 1072325 2011-12-15 14:52 libevent-2.0.so.5.1.4
- -rw-r--r-- 1 root root 1683924 2011-12-15 14:52 libevent.a
- lrwxrwxrwx 1 root root 26 2011-12-15 14:52 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.4
- -rwxr-xr-x 1 root root 646606 2011-12-15 14:52 libevent_core-2.0.so.5.1.4
- -rw-r--r-- 1 root root 1045954 2011-12-15 14:52 libevent_core.a
- -rwxr-xr-x 1 root root 974 2011-12-15 14:52 libevent_core.la
- lrwxrwxrwx 1 root root 26 2011-12-15 14:52 libevent_core.so -> libevent_core-2.0.so.5.1.4
- lrwxrwxrwx 1 root root 27 2011-12-15 14:52 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.4
- -rwxr-xr-x 1 root root 450238 2011-12-15 14:52 libevent_extra-2.0.so.5.1.4
- -rw-r--r-- 1 root root 638042 2011-12-15 14:52 libevent_extra.a
- -rwxr-xr-x 1 root root 981 2011-12-15 14:52 libevent_extra.la
- lrwxrwxrwx 1 root root 27 2011-12-15 14:52 libevent_extra.so -> libevent_extra-2.0.so.5.1.4
- -rwxr-xr-x 1 root root 939 2011-12-15 14:52 libevent.la
- lrwxrwxrwx 1 root root 29 2011-12-15 14:52 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.4
- -rwxr-xr-x 1 root root 97847 2011-12-15 14:52 libevent_openssl-2.0.so.5.1.4
- -rw-r--r-- 1 root root 131388 2011-12-15 14:52 libevent_openssl.a
- -rwxr-xr-x 1 root root 1010 2011-12-15 14:52 libevent_openssl.la
- lrwxrwxrwx 1 root root 29 2011-12-15 14:52 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.4
- lrwxrwxrwx 1 root root 30 2011-12-15 14:52 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.4
- -rwxr-xr-x 1 root root 22414 2011-12-15 14:52 libevent_pthreads-2.0.so.5.1.4
- -rw-r--r-- 1 root root 19526 2011-12-15 14:52 libevent_pthreads.a
- -rwxr-xr-x 1 root root 1002 2011-12-15 14:52 libevent_pthreads.la
- lrwxrwxrwx 1 root root 30 2011-12-15 14:52 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.4
- lrwxrwxrwx 1 root root 21 2011-12-15 14:52 libevent.so -> libevent-2.0.so.5.1.4
安装memcached-1.4.10
1、安装memcached-1.4.10的过程,如下所示:
- [email protected] www.linuxidc.com:/home/shirdrn# tar -xvzf memcached-1.4.10.tar.gz
- [email protected] www.linuxidc.com:/home/shirdrn# cd memcached-1.4.10/
- [email protected] www.linuxidc.com:/home/shirdrn/memcached-1.4.10# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local
- [email protected] www.linuxidc.com:/home/shirdrn/memcached-1.4.10# make
- [email protected] www.linuxidc.com:/home/shirdrn/memcached-1.4.10# make install
2、测试memcached-1.4.10安装是否成功:
- [email protected] www.linuxidc.com:/usr/local# ls -al /usr/local/memcached/bin/mem*
- -rwxr-xr-x 1 root root 365926 2011-12-15 14:54 /usr/local/memcached/bin/memcached
启动memcached服务:
- /usr/local/memcached/bin/memcached -d -m 256 -u root -l host -p 8311 -c 5000 -P /tmp/memcached.pid
执行上面的命令行,将启动memcached服务进程,并且进程号存储在/tmp/memcached.pid文件中。
上述命令行中选项的含义简介如下:
- -d 指定memcached进程作为一个守护进程启动
- -m 指定分配给memcached使用的内存,单位是MB
- -u 指定运行memcached的用户
- -l 指定监听的服务器IP地址,如果有多个地址的话,使用逗号分隔,格式可以为“IP地址:端口号”,例如:-l 192.168.0.184:19830,192.168.0.195:13542;端口号也可以通过-p选项指定
- -p 指定memcached监听的端口,要保证该端口号未被占用
- -c 指定是最大运行的并发连接数,默认是1024
- -P 指定保存memcached进程的pid文件
Java 测试代码:
import net.spy.memcached.MemcachedClient; import java.net.InetSocketAddress; public static void main(String[] args) throws IOException { MemcachedClient client = new MemcachedClient(new InetSocketAddress("cq01-sdcrd-cbstat03.cq01.baidu.com", 8311)); client.set("testUserName", 1000, "shine"); Object ret = client.get("testUserName"); System.out.println(ret); }
时间: 2024-11-05 20:47:58