安装memcached及其使用
1.什么是memcached
Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
2.memcached的特点
Memcached作为高速运行的分布式缓存服务器具有以下特点:
(1)简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;
(2)功能的实现一半依赖于客户端,一半基于服务器端:客户端负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;
(3)各服务器间彼此无视:不在服务器间进行数据同步,memcached尽管是“分布式”缓存服务器,但服务器端并没有分布式功能。各个memcached不会互相通信以共享信息。他的分布式主要是通过客户端实现的。
(4)O(1)的执行效率
(5)清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长;
3.memcached是如何让工作的?
Memcached的神奇来自两阶段哈希(two-stage hash)。Memcached就像一个巨大的、存储了很多对的哈希表。通过key,可以存储或查询任意的数据。
客户端可以把数据存储在多台memcached上。当查询数据时,客户端首先参考节点列表计算出key的哈希值(阶段一哈希),进而选中一个节点;客户端将请求发送给选中的节点,然后memcached节点通过一个内部的哈希算法(阶段二哈希),查找真正的数据(item)。
关于memcached的详细介绍(建议先看完这些东西):
http://kb.cnblogs.com/page/42731/
http://kb.cnblogs.com/page/69074/
4.编译安装memcached
4.1 首先安装libevent
memcached依赖于libevent API,因此要事先安装。下载地址:
[root@localhost ~]# tar xf libevent-2.0.22-stable.tar.gz
[root@localhost ~]# cd libevent-2.0.22-stable
[root@localhost libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.0.22-stable]# make && make install
[root@localhost libevent]# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
[root@localhost libevent]# ldconfig
4.2安装配置memcached
memcached下载地址:
[root@localhost ~]# tar xf memcached-1.4.25.tar.gz
[root@localhost ~]# cd memcached-1.4.25
[root@localhost memcached-1.4.25]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localhost memcached-1.4.25]# make && make install
4.3 memcached参数说明
[root@localhost ~]# /usr/local/memcached/bin/memcached -h
参数说明:
-p 监听的TCP端口(默认: 11211)
-U 监听的UDP端口(默认: 11211, 0表示不监听)
-s 用于监听的UNIX套接字路径(禁用网络支持)
-A enable ascii "shutdown" command
-a UNIX套接字访问掩码,八进制数字(默认:0700)
-l 监听的IP地址(默认:INADDR_ANY,所有地址)
-d 作为守护进程来运行
-r 最大核心文件限制
-u 设定进程所属用户(只有root用户可以使用这个参数)
-m 单个数据项的最大可用内存,以MB为单位(默认:64MB)
-M 内存用光时报错(而不是删除数据)
-c 最大并发连接数(默认:1024)
-k 锁定所有内存页。注意你可以锁定的内存上限.
试图分配更多内存会失败的,所以留意启动守护进程时所用的用户可分配的内存上限
(不是前面的-u 参数;在sh下,使用命令"ulimit -S -l NUM_KB"来设置)
-v 提示信息(在事件循环中打印错误/警告信息)
-vv 详细信息(也打印客户端命令/响应)
-vvv 超详细信息(还打印内部状态的变化)
-h 打印这个帮助信息并退出
-i 打印memcached和libevent的许可
-V 显示版本号并退出
-P 保存进程ID到该指定文件,只有在使用-d选项的时候才有意义
-f 块大小增长因子(默认:1.25)
-n 分配给key+value+flags的最小空间(默认:48)
-L 尝试使用大内存页(如果可用的话)提高内存页尺寸可以减少"页表缓冲(TLB)"丢失次数,
提高运行效率为了从操作系统获得大内存页,memcached会把全部数据项分配到一个大区块。
-D 使用作为前缀和ID的分隔符。这个用于按前缀获得状态报告。默认是:(冒号)
如果指定了这个参数,则状态收集会自动开启;如果没指定,则需要用命令"stats
detail on"来开启。
-t 使用的线程数(默认:4)
-R 每个连接可处理的最大请求数。对于一个给定的连接,
它限制进程请求的数量来防止进程互斥等待(默认:20)
-C 禁用CAS
-b 设置后台日志队列的长度(默认:1024)
-B 绑定协议,可能值:ascii,binary,auto(默认)
-I 重写每个数据页大小。调整数据项最大大小值(默认: 1mb, 最小值: 1k, 最大值: 128m)
-F 禁用flush_all命令
-o Comma separated list of extended or experimental options
- (EXPERIMENTAL) maxconns_fast: immediately close new
connections if over maxconns limit
- hashpower: An integer multiplier for how large the hash
table should be. Can be grown at runtime if not big enough.
Set this based on "STAT hash_power_level" before a
restart.
- tail_repair_time: Time in seconds that indicates how long to wait before
forcefully taking over the LRU tail item whose refcount has leaked.
Disabled by default; dangerous option.
- hash_algorithm: The hash table algorithm
default is jenkins hash. options: jenkins, murmur3
- lru_crawler: Enable LRU Crawler background thread
- lru_crawler_sleep: Microseconds to sleep between items
default is 100.
- lru_crawler_tocrawl: Max items to crawl per slab per run
default is 0 (unlimited)
- lru_maintainer: Enable new LRU system + background thread
- hot_lru_pct: Pct of slab memory to reserve for hot lru.
(requires lru_maintainer)
- warm_lru_pct: Pct of slab memory to reserve for warm lru.
(requires lru_maintainer)
- expirezero_does_not_evict: Items set to not expire, will not evict.
(requires lru_maintainer)
5.测试memcached的使用
5.1 启动memcached(加上-d选项即以deamon方式运行)
[root@localhost ~]# /usr/local/memcached/bin/memcached -m 64 -n 56 -f 1.25 -u nginx -vv
slab class 1: chunk size 88 perslab 11915
slab class 2: chunk size 112 perslab 9362
slab class 3: chunk size 144 perslab 7281
slab class 4: chunk size 184 perslab 5698
slab class 5: chunk size 232 perslab 4519
slab class 6: chunk size 296 perslab 3542
slab class 7: chunk size 376 perslab 2788
slab class 8: chunk size 472 perslab 2221
slab class 9: chunk size 592 perslab 1771
slab class 10: chunk size 744 perslab 1409
slab class 11: chunk size 936 perslab 1120
slab class 12: chunk size 1176 perslab 891
slab class 13: chunk size 1472 perslab 712
slab class 14: chunk size 1840 perslab 569
slab class 15: chunk size 2304 perslab 455
slab class 16: chunk size 2880 perslab 364
slab class 17: chunk size 3600 perslab 291
slab class 18: chunk size 4504 perslab 232
slab class 19: chunk size 5632 perslab 186
slab class 20: chunk size 7040 perslab 148
slab class 21: chunk size 8800 perslab 119
slab class 22: chunk size 11000 perslab 95
slab class 23: chunk size 13752 perslab 76
slab class 24: chunk size 17192 perslab 60
slab class 25: chunk size 21496 perslab 48
slab class 26: chunk size 26872 perslab 39
slab class 27: chunk size 33592 perslab 31
slab class 28: chunk size 41992 perslab 24
slab class 29: chunk size 52496 perslab 19
slab class 30: chunk size 65624 perslab 15
slab class 31: chunk size 82032 perslab 12
slab class 32: chunk size 102544 perslab 10
slab class 33: chunk size 128184 perslab 8
slab class 34: chunk size 160232 perslab 6
slab class 35: chunk size 200296 perslab 5
slab class 36: chunk size 250376 perslab 4
slab class 37: chunk size 312976 perslab 3
slab class 38: chunk size 391224 perslab 2
slab class 39: chunk size 489032 perslab 2
slab class 40: chunk size 611296 perslab 1
slab class 41: chunk size 764120 perslab 1
slab class 42: chunk size 1048576 perslab 1
<26 server listening (auto-negotiate)
<27 server listening (auto-negotiate)
<28 send buffer was 112640, now 268435456
<28 server listening (udp)
<29 server listening (udp)
<30 server listening (udp)
<31 server listening (udp)
<32 send buffer was 112640, now 268435456
<32 server listening (udp)
<33 server listening (udp)
<34 server listening (udp)
<35 server listening (udp)
5.2查看监听的端口
[root@localhost ~]# netstat -untlp | grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 10807/memcached
tcp 0 0 :::11211 :::* LISTEN 10807/memcached
udp 0 0 0.0.0.0:11211 0.0.0.0:* 10807/memcached
udp 0 0 :::11211 :::* 10807/memcached
5.3 使用telnet命令可对memcached进行测试
[root@localhost ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
5.4 memcached命令说明
Command Description Example
get reads a value get mykey
set set a key unconditionally set mykey 0 60 5
add add a new key add newkey 0 60 5
replace overwrite existing key replace key 0 60 5
append append data to existing key append key 0 60 5
prepend prepend data to existing key prepend key 0 60 5
incr increments numerical key value by given number incr mykey 2
decr decrements numerical key value by given number decr mykey 2
delete deletes an existing key delete mykey
invalidate specfic items immediately flush_all
flash_all
invalidate all items in m seconds flush_all 900
prints general statistics stats
prints memory statistics stats slabs
prints memory statistics stats malloc
stats print higher level allocation statistics stats items
stats detail
stats sizes
Resets statistics stats reset
version Prints server version version
verbosity Increases log level verbosity
quit Terminate telnet session quit
如:
add命令:
add keyname flag timeout datasize
add mykey 0 10 12
Hello world!
get命令:
get keyname
VALUE mykey 0 12
Hello world!
END
5.5 stats查看的信息关键字说明(忽略值选项)
5.6示例
[root@localhost ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
add mykey 0 60 12 #添加一个key
Hello World!
STORED
get mykey #查看该key的value
VALUE mykey 0 12
Hello World!
END
get key #等待60秒后再次查看
END
quit
Connection closed by foreign host.
注意:只要有足够的空间,设定的key就不会被清除,只不过因为超时而不显示了;