软件的版本:memcached是1.4.25,spymemcached是2.8.4,jdk1.6.0_45(64位)。
下载memcached-1.4.25.tar.gz放在/opt目录下面,从解压开始:
# 解压文件
[root@localhost opt]# tar -zxvf memcached-1.4.251.tar.gz
memcached-1.4.25/
memcached-1.4.25/aclocal.m4
memcached-1.4.25/assoc.c
# (中间省略)
[root@localhost opt]# cd memcached-1.4.25/
# 进行配置,检查当前的环境是否满足要安装软件的依赖关系
[root@localhost memcached-1.4.25]# ./configure
checking build system type… x86_64-unknown-linux-gnu
checking host system type… x86_64-unknown-linux-gnu
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for a thread-safe mkdir -p… /bin/mkdir -p
checking for gawk… gawk
checking whether make sets $(MAKE)… yes
checking whether make supports nested variables… yes
checking for gcc… no
checking for cc… no
checking for cl.exe… no
configure: error: in `/opt/memcached-1.4.25′:
configure: error: no acceptable C compiler found in $PATH
See `config.log’ for more details
# 提示说缺少gcc,马上装
[root@localhost memcached-1.4.25]#yum -y install gcc
# 下面是按照官网的步骤执行
[root@localhost memcached-1.4.25]# ./configure
[root@localhost memcached-1.4.25]# make
[root@localhost memcached-1.4.25]# make test
[root@localhost memcached-1.4.25]# sudo make install
# 启动memcached
# -m:设置内存大小(默认是m;-l:设置监听Ip地址;-p:监听端口;-u:以root用户执行
[root@localhost ~]# memcached -m 64 -l 192.168.1.100 -p 11211-u root
public class SpyMemcachedServer {
private String ip;
private int port;
public void setIp(String ip) {
this.ip = ip;
}
public String getIp() {
return ip;
}
public void setPort(int port) {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Port number must be between 0 to 65535");
}
this.port = port;
}
public int getPort() {
return port;
}
public String toString() {
return getIp() + ":" + getPort();
}
}
import java.util.concurrent.TimeUnit;
public interface SpyMemcachedConstants {
public static int DEFAULT_TIMEOUT = 5;
public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;
}
import java.io.IOException;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import net.spy.memcached.ConnectionObserver;
import net.spy.memcached.transcoders.Transcoder;
public class SpyMemcachedTest extends TestCase {
private SpyMemcachedManager manager;
protected void setUp() throws Exception {
super.setUp();
String[][] servs = new String[][] { { "192.168.1.100", "11211" },
// {"localhost", "11212"}
};
List<SpyMemcachedServer> servers = new ArrayList<SpyMemcachedServer>();
for (int i = 0; i < servs.length; i++) {
SpyMemcachedServer server = new SpyMemcachedServer();
server.setIp(servs[i][0]);
server.setPort(Integer.parseInt(servs[i][1]));
servers.add(server);
}
manager = new SpyMemcachedManager(servers);
manager.connect();
addObserver();
}
protected void tearDown() throws Exception {
super.tearDown();
manager.disConnect();
}
public void testSet() {
System.out.println("====TestSet====");
for (int i = 0; i < 10; i++) {
String key = "key" + i;
String value = "value" + i;
manager.set(key, value, 20);
}
}
public void testGet() {
System.out.println("====TestGet====");
for (int i = 0; i < 10; i++) {
String key = "key" + i;
Object value = manager.get(key);
if (value != null) {
System.out.println("From memcached");
System.out.println("key=" + key + ";value=" + value);
} else {
System.out.println("Not found");
System.out.println("key=" + key + ";value=" + value);
}
}
}
public void testAdd() {
System.out.println("====TestAdd====");
boolean flag = manager.add("key1", "value1-added", 20); // exist
assertEquals(false, flag);
flag = manager.add("key100", "value100", 20); // don't exist
assertEquals(true, flag);
testGet();
}
public void testReplace() {
System.out.println("====TestReplace====");
boolean flag = manager.replace("key2", "value2-replaced", 20);
assertEquals(true, flag);
flag = manager.replace("key1000", "value1000", 20);
assertEquals(false, flag);
testGet();
}
public void testDelete() {
System.out.println("====TestDelete====");
boolean flag = manager.delete("key3");
assertEquals(true, flag);
flag = manager.delete("key1000");
assertEquals(false, flag);
testGet();
}
public void testAsyncGet() {
System.out.println("====TestAsyncGet====");
Object value = manager.asyncGet("key4");
if (value != null) {
assertEquals("value4", (String) value);
}
System.out.println("value=" + value);
}
public void testGetMulti() {
System.out.println("====TestGetMulti====");
List<String> keys = new ArrayList<String>();
String[] strKeys = new String[5];
for (int i = 0; i < 5; i++) {
keys.add("key" + i);
strKeys[i] = "key" + i;
}
Map<String, Object> cache = manager.getMulti(keys);
printMap(cache);
cache = manager.getMulti(keys);
printMap(cache);
}
public void testAsyncGetMulti() {
System.out.println("====TestAsyncGetMulti====");
List<String> keys = new ArrayList<String>();
String[] strKeys = new String[5];
for (int i = 0; i < 5; i++) {
keys.add("key" + i);
strKeys[i] = "key" + i;
}
Map<String, Object> cache = manager.asyncGetMulti(keys);
printMap(cache);
cache = manager.asyncGetMulti(keys);
printMap(cache);
}
public void testIncrAndDecr() {
System.out.println("====TestIncrAndDecr====");
long l = -1;
l = manager.increment("incr", 2, 100, 60);
assertEquals(100, l);
l = manager.increment("incr", 4);
assertEquals(104, l);
l = manager.decrement("decr", 4, 100, 60);
assertEquals(100, l);
l = manager.decrement("decr", 3);
assertEquals(97, l);
System.out.println("incr=" + manager.get("incr").toString());
System.out.println("decr=" + manager.get("decr").toString());
}
public void testAsyncIncrAndDecr() {
System.out.println("====TestAsyncIncrAndDecr====");
long l = -1;
l = manager.asyncIncrement("incr", 2);
assertEquals(106, l);
l = manager.asyncDecrement("decr", 4);
assertEquals(93, l);
System.out.println("aincr=" + manager.get("incr").toString());
System.out.println("adecr=" + manager.get("decr").toString());
}
public void testPrintStat() throws IOException {
System.out.println("====TestPrintStat====");
manager.printStats();
}
private void addObserver() {
// System.out.println("====AddObserver====");
ConnectionObserver obs = new ConnectionObserver() {
public void connectionEstablished(SocketAddress sa,
int reconnectCount) {
System.out.println("Established " + sa.toString());
}
public void connectionLost(SocketAddress sa) {
System.out.println("Lost " + sa.toString());
}
};
manager.addObserver(obs);
}
public void testGetTranscoder() {
System.out.println("====TestGetTranscoder====");
Transcoder tran = manager.getTranscoder();
System.out.println(tran.getClass().toString());
}
private void printMap(Map map) {
StringBuffer temp = new StringBuffer();
Set set = map.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
Object value = map.get(key);
temp.append("key=" + key + ";value=" + value + "\r\n");
}
System.out.println(temp.toString());
}
}
#测试前,先停止防火墙
[root@localhost ~]# service iptables stop
# 执行测试例子(截取部分的信息)
2016-02-05 20:03:42.477 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.100:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
====TestSet====
2016-02-05 20:03:42.481 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@44bd928a
Established /192.168.1.100:11211
2016-02-05 20:03:42.498 INFO net.spy.memcached.MemcachedConnection: Shut down memcached client
2016-02-05 20:03:42.504 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.1.100:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
====TestGet====
Established /192.168.1.100:11211
2016-02-05 20:03:42.507 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@1d5a0305
From memcached
key=key0;value=value0
From memcached
key=key1;value=value1
From memcached
key=key2;value=value2
From memcached
key=key3;value=value3
From memcached
key=key4;value=value4
From memcached
# (省略)
#因为查阅memcached的运行状态需要用到telnet,而我没有安装telnet,所以先安装telnet-server端和telnet-client端。
[root@localhost ~]# yum install telnet-server
[root@localhost ~]# yum install telnet
# 查阅memcached运行状态:
[root@localhost ~]# telnet 192.168.1.100 11211
Trying 192.168.1.100…
Connected to 192.168.1.100.
Escape character is ‘^]’.
stats
STAT pid 4828 // memcached服务器的进程Id
STAT uptime 543 // 服务器已经运行的秒数
STAT time 1454703156 // 服务器当前的unix时间戳
STAT version 1.4.25 // //memcached版本
STAT libevent 1.4.13-stable // libevent版本
STAT pointer_size 64 // 当前操作系统的指针大小(32位系统一般是32bit,64就是64位操作系统)
STAT rusage_user 0.004999 // 进程的累计用户时间
STAT rusage_system 0.083987 // 进程的累计系统时间
STAT curr_connections 5 // 服务器当前存储的items数量
STAT total_connections 18 // 从服务器启动以后存储的items总数量
STAT connection_structures 6 // 服务器分配的连接构造数
STAT reserved_fds 20
STAT cmd_get 65 // get命令(获取)总请求次数
STAT cmd_set 16 // set命令(保存)总请求次数
STAT cmd_flush 0 // flush命令请求次数
STAT cmd_touch 0 // touch命令请求次数
STAT get_hits 60 // 总命中次数
STAT get_misses 5 // 总未命中次数
STAT delete_misses 1 // delete命令未命中次数
STAT delete_hits 1 // delete命令命中次数
STAT incr_misses 1 // incr命令未命中次数
STAT incr_hits 2 // incr命令命中次数
STAT decr_misses 1 // decr命令未命中次数
STAT decr_hits 2 // decr命令命中次数
STAT cas_misses 0 // cas命令未命中次数
STAT cas_hits 0 // cas命令命中次数
STAT cas_badval 0 // 使用擦拭次数
STAT touch_hits 0 // touch命令未命中次数
STAT touch_misses 0 // touch命令命中次数
STAT auth_cmds 0 // 认证命令处理的次数
STAT auth_errors 0 // 认证失败数目
STAT bytes_read 1120 // 总读取字节数(请求字节数)
STAT bytes_written 3100 // 总发送字节数(结果字节数)
STAT limit_maxbytes 67108864 // 分配给memcached的内存大小(字节b,启动memcached服务器配置64m,换算:67108864 b = 65536 kb = 64 m)
STAT accepting_conns 1 // 服务器是否达到过最大连接(0/1)
STAT listen_disabled_num 0 // 失效的监听数
STAT time_in_listen_disabled_us 0
STAT threads 4 // 当前线程数
STAT conn_yields 0 // 连接操作主动放弃数目
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 908 // 当前存储占用的字节数
STAT curr_items 12 // 当前存储的数据总数
STAT total_items 14 // 启动以来存储的数据总数
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0 // 为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)
STAT reclaimed 0 // 已过期的数据条目来存储新数据的数目
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
END
–完–
参考:
官方教程:http://www.memcached.org/downloads
memcached 的配置及 spymemcached 客户端简单使用:http://www.cnblogs.com/atio/p/3198230.html
spymemcached源码深入分析:http://my.oschina.net/astute/blog/93492
JAVA MemCache 史无前例的详细讲解!看完包精通MEMCACHE!:http://blog.csdn.net/hjm4702192/article/details/7894080
centos使用yum安装gcc:http://www.oschina.net/code/snippet_54100_10351
memcached之java客户端:spymemcached使用:http://blog.csdn.net/fhd001/article/details/6764038
linux查看memcached状态:http://www.2cto.com/os/201303/193264.html
linux查看memcached状态:http://blog.sina.com.cn/s/blog_6a1fb8420102uz90.html
linux下如何关闭防火墙?如何查看防火墙当前的状态:http://blog.csdn.net/bslzl/article/details/7937899
linux下查看进程和端口:http://blog.chinaunix.net/uid-25510439-id-2981118.html
memcached client – spymemcached使用:http://blog.csdn.net/gtuu0123/article/details/4849905
window的memcached的下载地址: https://commaster.net/content/installing-memcached-windows