标签:memcached
memcached
==========================================================================
概述:
Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。
Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。
Memcached简洁而强大。它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存的很多问题。它的API兼容大部分流行的开发语言。
本质上,它是一个简洁的key-value存储系统。
一般的使用目的:通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。
Memcached 官网:http://memcached.org/。
==========================================================================
1.介绍
★缓存服务器特性:
缓存:cache,无持久存储功能;
bypass缓存
k/v cache,仅支持存储流式化数据;
★Memcached的功用
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度;
★memcached特性:
k/v cache:仅可序列化数据;存储项:k/v;
智能性一半依赖于客户端(调用memcached的API开发程序),一半依赖于服务端;
分布式缓存:互不通信的分布式集群;
分布式系统请求路由方法:取模法,一致性哈希算法;
算法复杂度:O(1)
清理过期缓存项:
缓存耗尽:LRU
缓存项过期:惰性清理机制
附图:
2.memcached的安装和配置
★安装及监听端口
CentOS 7 由base仓库直接提供:
监听的端口:11211/tcp, 11211/udp
★安装文件内容如下:
[root@centos7 ~]# rpm -ql memcached /etc/sysconfig/memcached # 环境配置文件定义启动memcached是的选项 /usr/bin/memcached # 主程序 /usr/bin/memcached-tool # 管理memcached应用的工具 /usr/lib/systemd/system/memcached.service # Unit File文件依赖于第一条的环境配置文件 /usr/share/doc/memcached-1.4.15 /usr/share/doc/memcached-1.4.15/AUTHORS /usr/share/doc/memcached-1.4.15/CONTRIBUTORS /usr/share/doc/memcached-1.4.15/COPYING /usr/share/doc/memcached-1.4.15/ChangeLog /usr/share/doc/memcached-1.4.15/NEWS /usr/share/doc/memcached-1.4.15/README.md /usr/share/doc/memcached-1.4.15/protocol.txt # 官方文档 /usr/share/doc/memcached-1.4.15/readme.txt /usr/share/doc/memcached-1.4.15/threads.txt /usr/share/man/man1/memcached-tool.1.gz /usr/share/man/man1/memcached.1.gz★主程序:
/usr/bin/memcached
★配置文件:
/etc/sysconfig/memcached
[root@centos7 ~]# cat /etc/sysconfig/memcached PORT="11211" # 监听端口 USER="memcached" # 指明运行的用户身份 MAXCONN="1024" # 最大并发连接数 CACHESIZE="64" # 使用多大的内存空间来缓存 OPTIONS=""
Unit File:memcached.service
[root@centos7 ~]# cat /usr/lib/systemd/system/memcached.service [Unit] Description=Memcached Before=httpd.service After=network.target [Service] Type=simple EnvironmentFile=-/etc/sysconfig/memcached ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS [Install] WantedBy=multi-user.target★协议格式:memcached协议
文本格式
二进制格式(编码效率更高)
★启动memcached并查看端口11211/tcp, 11211/udp
[root@centos7 ~]# systemctl start memcached.service [root@centos7 ~]# ss -tunl Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 *:68 *:* udp UNCONN 0 0 *:68 *:* udp UNCONN 0 0 *:27217 *:* udp UNCONN 0 0 127.0.0.1:323 *:* udp UNCONN 0 0 *:30538 *:* udp UNCONN 0 0 *:11211 *:* udp UNCONN 0 0 *:514 *:* tcp LISTEN 0 128 :::11211 :::*
2.memcached的命令
★memcached连接
我们可以通过 telnet 命令并指定主机ip和端口来连接 Memcached 服务。
语法:telnet HOST PORT
命令中的 HOST 和 PORT 为运行 Memcached 服务的 IP 和 端口
★命令分类:
统计类:stats, stats items, stats slabs, stats sizes
存储类:set, add, replace, append, prepend
检索类:get, delete, incr/decr
清空:flush_all
示例:
命令演示:
基于文本格式的协议连接:
[root@centos7 ~]# telnet localhost 11211 # 使用telnet连接 Trying ::1... Connected to localhost. Escape character is ‘^]‘. stats # 统计状态 STAT pid 4710 STAT uptime 1051 STAT time 1478173984 STAT version 1.4.15 STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 0.037827 STAT rusage_system 0.059442 STAT curr_connections 10 STAT total_connections 12 STAT connection_structures 11 STAT reserved_fds 20 STAT cmd_get 0 STAT cmd_set 0 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 0 STAT get_misses 0 STAT delete_misses 0 STAT delete_hits 0 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 43 STAT bytes_written 1068 STAT limit_maxbytes 67108864 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT bytes 0 STAT curr_items 0 STAT total_items 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 END stats items END stats slabs # 内存分配 STAT active_slabs 0 STAT total_malloced 0
[root@centos7 ~]# telnet localhost 11211 Trying ::1... Connected to localhost. Escape character is ‘^]‘. set mykey 1 300 11 # 设定key为mykey,flags为1,过期时长为300s,大小为11字节, hello world # 回车之后给定的值 STORED get mykey # 检索key VALUE mykey 1 11 hello world END append mykey 1 300 1 # 在后面追加一个字节 . STORED get mykey VALUE mykey 1 12 hello world. END prepend mykey 1 300 9 # 在前面预设9个字节 welcome, # 值为welcome STORED get mykey VALUE mykey 1 21 welcome, hello world. END delete mykey # 删除key DELETED get mykey END set counter 1 300 1 0 STORED incr counter 1 # 增加一个字节,值为1 1 incr counter 1 2 incr counter 10 12 decr counter 1 11 get counter VALUE counter 1 2 11 END
3.memcached的常用选项
-m <num>:
Use <num> MB memory max to use for object storage; the default is 64 megabytes. 缓存空间大小,默认为64M
-c <num>:
Use <num> max simultaneous connections; the default is 1024. 最大并发连接数,默认1024;
-u <username>:
以指定的用户身份来运行进程;
-l <ip_addr>:
监听的IP地址,默认为本机所有地址;
-p <num>:
监听的TCP端口, the default is port 11211.
-U <num>:
Listen on UDP port <num>, the default is port 11211, 0 is off.
-M:
内存耗尽时,不执行LRU清理缓存,而是拒绝存入新的缓存项,直到有多余的空间可用时为止;
-f <factor>:
增长因子;默认是1.25;
-t <threads>:
启动的用于响应用户请求的线程数;默认为4个
演示:
[root@centos7 ~]# memcached -u memcached -vv -f 1.11 # 指定增长因子 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 184 perslab 5698 slab class 7: chunk size 208 perslab 5041 slab class 8: chunk size 232 perslab 4519 slab class 9: chunk size 264 perslab 3971 slab class 10: chunk size 296 perslab 3542 slab class 11: chunk size 328 perslab 3196 slab class 12: chunk size 368 perslab 2849 slab class 13: chunk size 408 perslab 2570 slab class 14: chunk size 456 perslab 2299 slab class 15: chunk size 512 perslab 2048 slab class 16: chunk size 568 perslab 1846 slab class 17: chunk size 632 perslab 1659 slab class 18: chunk size 704 perslab 1489 slab class 19: chunk size 784 perslab 1337 slab class 20: chunk size 872 perslab 1202 slab class 21: chunk size 968 perslab 1083 slab class 22: chunk size 1080 perslab 970 slab class 23: chunk size 1200 perslab 873 slab class 24: chunk size 1336 perslab 784 slab class 25: chunk size 1488 perslab 704 slab class 26: chunk size 1656 perslab 633 slab class 27: chunk size 1840 perslab 569 slab class 28: chunk size 2048 perslab 512 slab class 29: chunk size 2280 perslab 459 slab class 30: chunk size 2536 perslab 413 slab class 31: chunk size 2816 perslab 372 slab class 32: chunk size 3128 perslab 335 slab class 33: chunk size 3472 perslab 302 slab class 34: chunk size 3856 perslab 271 slab class 35: chunk size 4280 perslab 244 slab class 36: chunk size 4752 perslab 220 slab class 37: chunk size 5280 perslab 198 slab class 38: chunk size 5864 perslab 178 slab class 39: chunk size 6512 perslab 161 slab class 40: chunk size 7232 perslab 144 slab class 41: chunk size 8032 perslab 130 slab class 42: chunk size 8920 perslab 117 slab class 43: chunk size 9904 perslab 105 slab class 44: chunk size 11000 perslab 95 slab class 45: chunk size 12216 perslab 85 slab class 46: chunk size 13560 perslab 77 slab class 47: chunk size 15056 perslab 69 slab class 48: chunk size 16712 perslab 62 slab class 49: chunk size 18552 perslab 56 slab class 50: chunk size 20592 perslab 50 slab class 51: chunk size 22864 perslab 45 slab class 52: chunk size 25384 perslab 41 slab class 53: chunk size 28176 perslab 37 slab class 54: chunk size 31280 perslab 33 slab class 55: chunk size 34720 perslab 30 slab class 56: chunk size 38544 perslab 27 slab class 57: chunk size 42784 perslab 24 slab class 58: chunk size 47496 perslab 22 slab class 59: chunk size 52720 perslab 19 slab class 60: chunk size 58520 perslab 17 slab class 61: chunk size 64960 perslab 16 slab class 62: chunk size 72112 perslab 14 slab class 63: chunk size 80048 perslab 13 slab class 64: chunk size 88856 perslab 11 slab class 65: chunk size 98632 perslab 10 slab class 66: chunk size 109488 perslab 9 slab class 67: chunk size 121536 perslab 8 slab class 68: chunk size 134904 perslab 7 slab class 69: chunk size 149744 perslab 7 slab class 70: chunk size 166216 perslab 6 slab class 71: chunk size 184504 perslab 5 slab class 72: chunk size 204800 perslab 5 slab class 73: chunk size 227328 perslab 4 slab class 74: chunk size 252336 perslab 4 slab class 75: chunk size 280096 perslab 3 slab class 76: chunk size 310912 perslab 3 slab class 77: chunk size 345112 perslab 3 slab class 78: chunk size 383080 perslab 2 slab class 79: chunk size 425224 perslab 2 slab class 80: chunk size 472000 perslab 2 slab class 81: chunk size 523920 perslab 2 slab class 82: chunk size 581552 perslab 1 slab class 83: chunk size 645528 perslab 1 slab class 84: chunk size 716536 perslab 1 slab class 85: chunk size 795360 perslab 1 slab class 86: chunk size 882856 perslab 1 slab class 87: chunk size 1048576 perslab 1 <26 server listening (auto-negotiate) <27 server listening (auto-negotiate) <28 send buffer was 212992, now 268435456 <29 send buffer was 212992, now 268435456 <28 server listening (udp) <28 server listening (udp) <29 server listening (udp) <28 server listening (udp) <28 server listening (udp) <29 server listening (udp) <29 server listening (udp) <29 server listening (udp)
3.memcached的驱动模块及命令行工具
★memcached默认没有认证机制,可借用于SASL进行认证;
SASL:Simple Authentication Secure Layer
★API:其他程序连接memcached的驱动模块
php-pecl-memcache
php-pecl-memcached
python-memcached
libmemcached
libmemcached-devel
★命令行工具:管理memcached服务
memcached-tool SERVER:PORT COMMAND
演示:
[root@centos7 ~]# memcached-tool Usage: memcached-tool <host[:port] | /path/to/socket> [mode] memcached-tool 10.0.0.5:11211 display # shows slabs memcached-tool 10.0.0.5:11211 # same. (default is display) memcached-tool 10.0.0.5:11211 stats # shows general stats memcached-tool 10.0.0.5:11211 dump # dumps keys and values [root@centos7 ~]# memcached-tool 127.0.0.1:11211 stats #127.0.0.1:11211 Field Value accepting_conns 1 auth_cmds 0 auth_errors 0 bytes 0 bytes_read 7 bytes_written 0 cas_badval 0 cas_hits 0 cas_misses 0 cmd_flush 0 cmd_get 0 cmd_set 0 cmd_touch 0 conn_yields 0 connection_structures 11 curr_connections 10 curr_items 0 decr_hits 0 decr_misses 0 delete_hits 0 delete_misses 0 evicted_unfetched 0 evictions 0 expired_unfetched 0 get_hits 0 get_misses 0 hash_bytes 524288 hash_is_expanding 0 hash_power_level 16 incr_hits 0 incr_misses 0 libevent 2.0.21-stable limit_maxbytes 67108864 listen_disabled_num 0 pid 5346 pointer_size 64 reclaimed 0 reserved_fds 20 rusage_system 0.006145 rusage_user 0.008193 threads 4 time 1478179802 total_connections 11 total_items 0 touch_hits 0 touch_misses 0 uptime 60 version 1.4.15
=====================================================================================
★memcached向详细用法可以参考如下文档
http://www.runoob.com/memcached/memcached-tutorial.html
标签:memcached
原文地址:http://1992tao.blog.51cto.com/11606804/1869146