java使用memcached1--安装与基本使用
环境
CentOs6.4 libevent-2.0.22-stable memcached-1.4.24
一、memcached安装
# cd /usr/local
1、编译安装libevent
# wget http://nchc.dl.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz # tar -xzvf libevent-2.0.22-stable.tar.gz # cd libevent-2.0.22-stable # ./configure --prefix=/usr # make && make install
2、编译安装memcached
# wget http://memcached.org/latest # tar -xzvf memcached-1.4.24.tar.gz # cd memcached-1.4.24 # mkdir /usr/local/memcached # ./configure --prefix=/usr/local/memcached --with-libevent=/usr # make && make install
3、启动Memcached
/usr/local/memcached/bin/memcached -u root -d -m 2048 -l 127.0.0.1 -p 11211 -P /tmp/memcached.pid
4、telnet登录
# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. set foo 0 900 5 isfoo STORED get foo VALUE foo 0 5 isfoo END quit Connection closed by foreign host.
Memcached启动参数说明:
-p 监听tcp协议的监听端口 -T 监听UDP协议id监听端口默认都是11211 -s 如果只在本地通信那么可以将其监听在某个套接字上,比如mysql.scok 能够利用共享内存方式进行通信的 -c 最大并发连接数 -l 监听的地址,如果服务器有多块网卡,那么用-l来指定监听的某个网卡上 -d 以一个服务方式运行 -r 设定最大内核大小限制 -u 以某个用户身份运行 -m 以兆为单位指定memcached最大内存可用空间 -t 用于处理入站请求最大的线程数,仅在memcached编译时开启了支持线程才有效,而linux对线程支持是非常有限的,所以不用关心了 -f 设定slab定义预先分配内存空间大小固定的块时使用的增长因子 -n 最小的存储单位是多大,默认是48字节,单位是字节 -P 指定pid文件 -L 视图使用最多的内存空间 -S 启用SSL认证功能
二、java使用memcached
package memcached; import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.CASResponse; import net.spy.memcached.CASValue; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { MemcachedJava memcached = new MemcachedJava(); // memcached.set(); // memcached.add(); // memcached.replace(); // memcached.prepend(); // memcached.cas(); // memcached.get(); // memcached.gets(); // memcached.delete(); memcached.incr(); } private void set() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 存储数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 查看存储状态 System.out.println("set status:" + fo.get()); // 输出值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } @SuppressWarnings("rawtypes") private void add() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 打印状态 System.out.println("set status:" + fo.get()); // 输出 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 添加 fo = mcc.add("runoob", 900, "memcached"); // 打印状态 System.out.println("add status:" + fo.get()); // 添加新key fo = mcc.add("codingground", 900, "All Free Compilers"); // 打印状态 System.out.println("add status:" + fo.get()); // 输出 System.out.println("codingground value in cache - " + mcc.get("codingground")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } @SuppressWarnings("rawtypes") private void replace() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加第一个 key=》value 对 Future fo = mcc.set("runoob", 900, "Free Education"); // 输出执行 add 方法后的状态 System.out.println("add status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 添加新的 key fo = mcc.replace("runoob", 900, "Largest Tutorials' Library"); // 输出执行 set 方法后的状态 System.out.println("replace status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } @SuppressWarnings("rawtypes") private void append() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 对存在的key进行数据添加操作 fo = mcc.append("runoob", " for All"); // 输出执行 set 方法后的状态 System.out.println("append status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("codingground")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private void prepend() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Education for All"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 对存在的key进行数据添加操作 fo = mcc.prepend("runoob", "Free "); // 输出执行 set 方法后的状态 System.out.println("prepend status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private void cas() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 使用 get 方法获取数据 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 通过 gets 方法获取 CAS token(令牌) CASValue casValue = mcc.gets("runoob"); // 输出 CAS token(令牌) 值 System.out.println("CAS token - " + casValue); // 尝试使用cas方法来更新数据 CASResponse casresp = mcc.cas("runoob", casValue.getCas(), 900, "Largest Tutorials-Library"); // 输出 CAS 响应信息 System.out.println("CAS Response - " + casresp); // 输出值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private void get() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 使用 get 方法获取数据 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private void gets() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 从缓存中获取键为 runoob 的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 通过 gets 方法获取 CAS token(令牌) CASValue casValue = mcc.gets("runoob"); // 输出 CAS token(令牌) 值 System.out.println("CAS value in cache - " + casValue); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private void delete() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "World's largest online tutorials library"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 对存在的key进行数据添加操作 fo = mcc.delete("runoob"); // 输出执行 delete 方法后的状态 System.out.println("delete status:" + fo.get()); // 获取键对应的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private void incr() { try { // 连接本地的 Memcached 服务 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.77.135", 11211)); System.out.println("Connection to server sucessful."); // 添加数字值 Future fo = mcc.set("number", 900, "1000"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("value in cache - " + mcc.get("number")); // 自增并输出 System.out.println("value in cache after increment - " + mcc.incr("number", 111)); // 自减并输出 System.out.println("value in cache after decrement - " + mcc.decr("number", 112)); // 关闭连接 mcc.shutdown(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }