memcached.高性能,高并发的分布式内存对象缓存系统,纯内存的缓存系统,效率极高,memcached主要目的,通过自身内存缓存关系型数据库的查询结果,减少数据库自身被访问的次数,以提高动态web应用的速度,网站架构的并发能力和扩展性
memcached可缓存用户重复的请求的动态内容(如:文字。session等);数据调用,API调用或页面引用结果的直接数据,如字符串,对象等。http://www.memcached.org/
1,通常在一个memcached场景中,对于不会经常变动的数据放在memcached中,“数据预热”!也可以做成分类数据静态化文件,并且可以通过web缓存或者CND加速2,热点数据缓存,对于一些更新频繁的数据也会(人为或者预热)写入到memcached
memcached特性:1,memcached通过telnet/nc等命令可直接操作memcached数据,并且memcached协议基于文本格式,和二进制格式2,支持epoll/kqueue异步io模型,使用libevent作为事件处理通知机制3,key/value键值数据类型4,全内存缓存,速度高,速率快,无持久化存储,当冲击则丢失,如果希望数据保留则可以使用redis,或者memecachedb
Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:
键值:查找标准和对应值,键hash编码存放,和key的每一个字段的key做比较,比较过程在不出现码冲突,键冲突的情况下,查找时间无论键的hash个数,只要没出现冲突,则时间一样。
yum安装:yum install libevent libevent-devel nc telnet memcached -y
[root@NFS-WEB2 ~]# which memcached
/usr/bin/memcached
[root@NFS-WEB2 ~]# memcached -m 16m -p 11211 -d -uroot -c 8192
[root@NFS-WEB2 ~]# lsof -i :11211
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 3159 root 26u IPv4 25986 0t0 TCP *:memcache (LISTEN)
memcached 3159 root 27u IPv6 25987 0t0 TCP *:memcache (LISTEN)
memcached 3159 root 28u IPv4 25990 0t0 UDP *:memcache
memcached 3159 root 29u IPv6 25991 0t0 UDP *:memcache
[root@NFS-WEB2 ~]#
多实例启动
[root@NFS-WEB2 ~]# memcached -m 16m -p 11212 -d -uroot -c 8192
[root@NFS-WEB2 ~]# lsof -i :11212
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 3186 root 26u IPv4 27205 0t0 TCP *:11212 (LISTEN)
memcached 3186 root 27u IPv6 27206 0t0 TCP *:11212 (LISTEN)
memcached 3186 root 28u IPv4 27209 0t0 UDP *:11212
memcached 3186 root 29u IPv6 27210 0t0 UDP *:11212
[root@NFS-WEB2 ~]#
memcached -h相关参数:存储类命令:set(设置缓存), add(新增), replace(替换), append(在一个缓存后附加新的内容), prepend(在缓存内容之前)获取数据类命令:get(查看), delete(删除), incr/decr统计类命令:stats, stats items, stats slabs, stats sizes清理命令: flush_all
进程与连接的设置:-d: 守护进程方式允许-u: 指定用户-l: 指定进程监控服务器iP-p: 监控tcp端口号默认11211-P(大写):设置保存的pid文件内存相关:-m:指定memcached服务科缓存的最大内存,默认64M-M: memcached服务内存不够时禁用LRU,如果内存满了会报错-n:为key+value_flags分配的最小内存空间,默认48字节-f :设定Slab Allocator定义预先分配内存空间大小固定的块时使用的增长因子;-L:启用打内存页,可以降低内存消耗, 改进性能并发连接:-c:最大并发连接,默认1024-t: 线程数,默认4,由于memcached采用NIO,线程太多用处不大-R:每个event最大请求数,默认20-C:禁用CAS,可以禁止版本计数,减少开销-S: 启用sasl进行用户认证;调试参数:-v: 打印较少的errors/warnings-vv: 打印非常多调试信息和错误输出到控制台,也打印客户端命令和响应-vvv: 打印极多的调试信息和错误输出,打印内部状态转变
[root@NFS-WEB2 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
插入
set user01 0 0 7
linuxea
STORED
查看
get user01
VALUE user01 0 7
linuxea
END
删除
delete user01
DELETED
get user01
END
当有多实例时在使用:
memcached -m 16m -p 11211 -d -uroot -c 8192 -P /var/run/11211.pid
kill
cat /var/run/11211.pid
即可
查看
[root@NFS-WEB2 memcache-2.2.7]# memcached -u root -vv
slab class 1: chunk size 96 perslab 10922
slab class 2: chunk size 120 perslab 8738
slab class 3: chunk size 152 perslab 6898
slab class 4: chunk size 192 perslab 5461
slab class 5: chunk size 240 perslab 4369
slab class 6: chunk size 304 perslab 3449
slab class 7: chunk size 384 perslab 2730
slab class 8: chunk size 480 perslab 2184
slab class 9: chunk size 600 perslab 1747
slab class 10: chunk size 752 perslab 1394
slab class 11: chunk size 944 perslab 1110
slab class 12: chunk size 1184 perslab 885
slab class 13: chunk size 1480 perslab 708
slab class 14: chunk size 1856 perslab 564
slab class 15: chunk size 2320 perslab 451
slab class 16: chunk size 2904 perslab 361
slab class 17: chunk size 3632 perslab 288
slab class 18: chunk size 4544 perslab 230
slab class 19: chunk size 5680 perslab 184
slab class 20: chunk size 7104 perslab 147
slab class 21: chunk size 8880 perslab 118
slab class 22: chunk size 11104 perslab 94
slab class 23: chunk size 13880 perslab 75
slab class 24: chunk size 17352 perslab 60
slab class 25: chunk size 21696 perslab 48
slab class 26: chunk size 27120 perslab 38
slab class 27: chunk size 33904 perslab 30
slab class 28: chunk size 42384 perslab 24
slab class 29: chunk size 52984 perslab 19
slab class 30: chunk size 66232 perslab 15
slab class 31: chunk size 82792 perslab 12
slab class 32: chunk size 103496 perslab 10
slab class 33: chunk size 129376 perslab 8
slab class 34: chunk size 161720 perslab 6
slab class 35: chunk size 202152 perslab 5
slab class 36: chunk size 252696 perslab 4
slab class 37: chunk size 315872 perslab 3
slab class 38: chunk size 394840 perslab 2
slab class 39: chunk size 493552 perslab 2
slab class 40: chunk size 616944 perslab 1
slab class 41: chunk size 771184 perslab 1
slab class 42: chunk size 1048576 perslab 1
<26 server listening (auto-negotiate)
<27 server listening (auto-negotiate)
<28 send buffer was 124928, now 268435456
<29 send buffer was 124928, now 268435456
<28 server listening (udp)
<29 server listening (udp)
<28 server listening (udp)
<29 server listening (udp)
<28 server listening (udp)
<29 server listening (udp)
<28 server listening (udp)
<29 server listening (udp)
指定1.1的增长因子
[root@NFS-WEB2 memcache-2.2.7]# memcached -u root -f 1.1 -vv
slab class 1: chunk size 96 perslab 10922
slab class 2: chunk size 112 perslab 9362
slab class 3: chunk size 128 perslab 8192
slab class 4: chunk size 144 perslab 7281
slab class 5: chunk size 160 perslab 6553
slab class 6: chunk size 176 perslab 5957
slab class 7: chunk size 200 perslab 5242
slab class 8: chunk size 224 perslab 4681
php插件
http://pecl.php.net/package/memcache
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xf memcache-2.2.7.tgz
cd memcached-2.2.7
[root@NFS-WEB2 memcache-2.2.7]# /usr/local/php5.3/bin/phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@NFS-WEB2 memcache-2.2.7]# . /configure --enable-memcache --with-php-config=/usr/local/php5.3/bin/php-config
[root@NFS-WEB2 memcache-2.2.7]# make
[root@NFS-WEB2 memcache-2.2.7]# make install
Installing shared extensions: /usr/local/php5.3/lib/php/extensions/no-debug-non-zts-20090626/
编辑/usr/local/php/lib/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:
[root@NFS-WEB2 memcache-2.2.7]# vim /etc/php.ini
extension = /usr/local/php5.3/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
[root@NFS-WEB2 php5.3]# /usr/local/php5.3/sbin/php-fpm
而后对memcached功能进行测试,在网站目录中建立测试页面test.php,添加如下内容:<?php$mem = new Memcache;$mem->connect("127.0.0.1", 11211) or die("Could not connect");
$version = $mem->getVersion();echo "Server's version: ".$version."<br/>n";
$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");echo "Store data in the cache (data will expire in 600 seconds)<br/>n";
$get_result = $mem->get('hellokey');echo "$get_result is from memcached server."; ?>
phpMemcachedAdmin
[root@NFS-WEB2 share]# yum -y install phpMemcachedAdmin
[root@NFS-WEB2 share]# cp -R phpMemcachedAdmin/ /www/www/pma
状态参数:curr_items 当前缓存条个数total_items 总的缓存条目total_connections 总的连接数curr_connections 当前的连接数connection_struouures 总的连接个数cmd_get 总的get次数get_hits get命中次数get_misses get未命中次数incr_hits 自动加1命中次数incr_misses 自动加1未命中次数bytes_read 用户发来的字节数bytes_written 总发送客户端响应的字节数limit_maxbytes memcache内存的总的字节大小threads 线程数 evictions 未获取空闲内存删除的items数
memcache-master[root@NFS-WEB2 www]# unzip memadmin-master.zip
memcachephp工具下载地址:点我下载