5.1 搭建准备
1、 安装memcached需先安装libevent,故需要准备libevent linux安装包:libevent-1.4.14b-stable.tar.gz。
2、 准备安装memcached linux安装包:memcached-1.4.10.tar.gz
5.2 搭建
1、 用用户登录主机,创建一个临时目录:临时目录。
2、 将libevent安装包和memcached安装包上传到临时目录下。
3、 安装libevent
找到libevent安装包存放的目录,输入命令:cd 临时目录
将libevent安装包进行解压,输入命令:tar zvxf libevent-1.4.14b-stable.tar.gz
进入到libevent目录中,输入命令:cd libevent-1.4.14b-stable
设置安装路径,输入命令:./configure –prefix=/usr
进行安装,输入命令:make
进行初始安装,输入命令:make install
测试libevent是否安装成功,输入命令:ls -al /usr/lib | grep libevent,将会显示下面内容
lrwxrwxrwx 1 root root 21 2012-08-03 15:37 libevent-1.4.so.2 -> libevent-1.4.so.2.2.0
-rwxr-xr-x 1 root root 400816 2012-08-03 15:37 libevent-1.4.so.2.2.0
-rw-r--r-- 1 root root 640978 2012-08-03 15:37 libevent.a
lrwxrwxrwx 1 root root 26 2012-08-03 15:37 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.2.0
-rwxr-xr-x 1 root root 144397 2012-08-03 15:37 libevent_core-1.4.so.2.2.0
-rw-r--r-- 1 root root 238540 2012-08-03 15:37 libevent_core.a
-rwxr-xr-x 1 root root 988 2012-08-03 15:37 libevent_core.la
lrwxrwxrwx 1 root root 26 2012-08-03 15:37 libevent_core.so -> libevent_core-1.4.so.2.2.0
lrwxrwxrwx 1 root root 27 2012-08-03 15:37 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 316680 2012-08-03 15:37 libevent_extra-1.4.so.2.2.0
-rw-r--r-- 1 root root 496316 2012-08-03 15:37 libevent_extra.a
-rwxr-xr-x 1 root root 995 2012-08-03 15:37 libevent_extra.la
lrwxrwxrwx 1 root root 27 2012-08-03 15:37 libevent_extra.so -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 953 2012-08-03 15:37 libevent.la
lrwxrwxrwx 1 root root 21 2012-08-03 15:37 libevent.so -> libevent-1.4.so.2.2.0
4、 安装memcached
找到memcached安装包存放的目录,输入命令:cd 临时目录
将memcached安装包进行解压,输入命令:tar zvxf memcached-1.4.10.tar.gz
进入memcached目录,输入命令:cd memcached-1.4.10
指定libevent安装目录,输入命令:./configure –with-libevent=/usr
进行安装,输入命令:make
进行初始安装,输入命令:make install
5、 启动memcached
启动memcached命令为:# /usr/local/bin/memcached -d -m 10 -u root -l ip -p 端口号 -c 256 -P /目录/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
5.3 测试
1、 编写一个将一个值存入缓存中,再从缓存中获取,最后再将缓存中删除该值的程序,该程序需添加xmemcached-1.2.6.2.jar。
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
public class TestMain {
public static void main(String args[]) {
try {
MemcachedClient mc = null;
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("IP:端口号")); //获取build
mc = builder.build(); //获取缓存服务器
boolean flag = mc.add("helloworld", 3, "helloworld") ; //存入缓存中
System.out.println("value == " + mc.get("helloworld")); //从缓存中获取值
System.out.println("flag == " + flag);
flag = mc.add("helloworld", 3, "helloworld") ; //添加到缓存中会失败
System.out.println("flag == " + flag);
mc.delete("helloworld") ; //从缓存中删除该键+
} catch (Exception e) {
e.printStackTrace() ;
}
}
}
2、 启动该程序,如程序正常运行,且结果与预期结果一致,则缓存启动成功。
1、 安装memcached需先安装libevent,故需要准备libevent linux安装包:libevent-1.4.14b-stable.tar.gz。
2、 准备安装memcached linux安装包:memcached-1.4.10.tar.gz
5.2 搭建
1、 用用户登录主机,创建一个临时目录:临时目录。
2、 将libevent安装包和memcached安装包上传到临时目录下。
3、 安装libevent
找到libevent安装包存放的目录,输入命令:cd 临时目录
将libevent安装包进行解压,输入命令:tar zvxf libevent-1.4.14b-stable.tar.gz
进入到libevent目录中,输入命令:cd libevent-1.4.14b-stable
设置安装路径,输入命令:./configure –prefix=/usr
进行安装,输入命令:make
进行初始安装,输入命令:make install
测试libevent是否安装成功,输入命令:ls -al /usr/lib | grep libevent,将会显示下面内容
lrwxrwxrwx 1 root root 21 2012-08-03 15:37 libevent-1.4.so.2 -> libevent-1.4.so.2.2.0
-rwxr-xr-x 1 root root 400816 2012-08-03 15:37 libevent-1.4.so.2.2.0
-rw-r--r-- 1 root root 640978 2012-08-03 15:37 libevent.a
lrwxrwxrwx 1 root root 26 2012-08-03 15:37 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.2.0
-rwxr-xr-x 1 root root 144397 2012-08-03 15:37 libevent_core-1.4.so.2.2.0
-rw-r--r-- 1 root root 238540 2012-08-03 15:37 libevent_core.a
-rwxr-xr-x 1 root root 988 2012-08-03 15:37 libevent_core.la
lrwxrwxrwx 1 root root 26 2012-08-03 15:37 libevent_core.so -> libevent_core-1.4.so.2.2.0
lrwxrwxrwx 1 root root 27 2012-08-03 15:37 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 316680 2012-08-03 15:37 libevent_extra-1.4.so.2.2.0
-rw-r--r-- 1 root root 496316 2012-08-03 15:37 libevent_extra.a
-rwxr-xr-x 1 root root 995 2012-08-03 15:37 libevent_extra.la
lrwxrwxrwx 1 root root 27 2012-08-03 15:37 libevent_extra.so -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 953 2012-08-03 15:37 libevent.la
lrwxrwxrwx 1 root root 21 2012-08-03 15:37 libevent.so -> libevent-1.4.so.2.2.0
4、 安装memcached
找到memcached安装包存放的目录,输入命令:cd 临时目录
将memcached安装包进行解压,输入命令:tar zvxf memcached-1.4.10.tar.gz
进入memcached目录,输入命令:cd memcached-1.4.10
指定libevent安装目录,输入命令:./configure –with-libevent=/usr
进行安装,输入命令:make
进行初始安装,输入命令:make install
5、 启动memcached
启动memcached命令为:# /usr/local/bin/memcached -d -m 10 -u root -l ip -p 端口号 -c 256 -P /目录/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
5.3 测试
1、 编写一个将一个值存入缓存中,再从缓存中获取,最后再将缓存中删除该值的程序,该程序需添加xmemcached-1.2.6.2.jar。
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
public class TestMain {
public static void main(String args[]) {
try {
MemcachedClient mc = null;
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("IP:端口号")); //获取build
mc = builder.build(); //获取缓存服务器
boolean flag = mc.add("helloworld", 3, "helloworld") ; //存入缓存中
System.out.println("value == " + mc.get("helloworld")); //从缓存中获取值
System.out.println("flag == " + flag);
flag = mc.add("helloworld", 3, "helloworld") ; //添加到缓存中会失败
System.out.println("flag == " + flag);
mc.delete("helloworld") ; //从缓存中删除该键+
} catch (Exception e) {
e.printStackTrace() ;
}
}
}
2、 启动该程序,如程序正常运行,且结果与预期结果一致,则缓存启动成功。