这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
分享:技术:memcached:memcached的介绍 [2015/07/22 13:52] gxx |
分享:技术:memcached:memcached的介绍 [2015/07/22 14:22] (当前版本) gxx |
||
---|---|---|---|
行 34: | 行 34: | ||
</code> | </code> | ||
====== memcached的常用命令 ====== | ====== memcached的常用命令 ====== | ||
- | * 存储命令 | + | ===== 存储命令/读取命令 ===== |
存储命令的格式: | 存储命令的格式: | ||
<command name> <key> <flags> <exptime> <bytes> | <command name> <key> <flags> <exptime> <bytes> | ||
行 45: | 行 45: | ||
^ <bytes> |存储字节数| | ^ <bytes> |存储字节数| | ||
^ <data block> |存储的数据块(可直接理解为key-value结构中的value)| | ^ <data block> |存储的数据块(可直接理解为key-value结构中的value)| | ||
+ | <code> | ||
+ | gxx@iZ23goxo66aZ:~$ telnet localhost 11211 | ||
+ | Trying 127.0.0.1... | ||
+ | Connected to localhost. | ||
+ | Escape character is '^]'. | ||
+ | set name 0 0 3 #set设置name,不管是否存在,长度为3字节 | ||
+ | gxx | ||
+ | STORED | ||
+ | get name #get获取name | ||
+ | VALUE name 0 3 | ||
+ | gxx | ||
+ | END | ||
+ | add name 0 0 3 #add添加name,已存在,添加失败 | ||
+ | lqy | ||
+ | NOT_STORED | ||
+ | add pwd 0 0 6 #add添加pwd,不存在,添加成功 | ||
+ | 123456 | ||
+ | STORED | ||
+ | replace notexist 0 0 3 #replace替换notexist,不存在,替换失败 | ||
+ | 123 | ||
+ | NOT_STORED | ||
+ | replace pwd 0 0 3 #replace替换pwd,已存在,替换成功 | ||
+ | 123 | ||
+ | STORED | ||
+ | get pwd #get获取pwd | ||
+ | VALUE pwd 0 3 | ||
+ | 123 | ||
+ | END | ||
+ | delete pwd #delete删除pwd,删除成功 | ||
+ | DELETED | ||
+ | get pwd #get获取pwd,不存在,直接返回END | ||
+ | END | ||
+ | get name #get获取name | ||
+ | VALUE name 0 3 | ||
+ | gxx | ||
+ | END | ||
+ | gets name #gets获取name,后面多返回一个类似版本号的数字,当name值改变时,这个多返回的数字也会改变 | ||
+ | VALUE name 0 3 12 | ||
+ | gxx | ||
+ | END | ||
+ | </code> | ||
+ | ===== 状态命令 ===== | ||
+ | <code> | ||
+ | stats #查看状态 | ||
+ | STAT pid 9167 | ||
+ | STAT uptime 105193 | ||
+ | STAT time 1437545041 | ||
+ | STAT version 1.4.14 (Ubuntu) | ||
+ | STAT libevent 2.0.21-stable | ||
+ | STAT pointer_size 64 | ||
+ | STAT rusage_user 2.010588 | ||
+ | STAT rusage_system 0.816230 | ||
+ | STAT curr_connections 5 | ||
+ | STAT total_connections 13 | ||
+ | STAT connection_structures 6 | ||
+ | STAT reserved_fds 20 | ||
+ | STAT cmd_get 20 | ||
+ | STAT cmd_set 16 | ||
+ | STAT cmd_flush 1 | ||
+ | STAT cmd_touch 0 | ||
+ | STAT get_hits 15 | ||
+ | STAT get_misses 5 | ||
+ | STAT delete_misses 0 | ||
+ | STAT delete_hits 2 | ||
+ | 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 1084 | ||
+ | STAT bytes_written 7545 | ||
+ | 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 expired_unfetched 0 | ||
+ | STAT evicted_unfetched 0 | ||
+ | STAT bytes 365 | ||
+ | STAT curr_items 5 | ||
+ | STAT total_items 14 | ||
+ | STAT evictions 0 | ||
+ | STAT reclaimed 0 | ||
+ | END | ||
+ | stats items #查看项状态 | ||
+ | STAT items:1:number 5 | ||
+ | STAT items:1:age 75765 | ||
+ | STAT items:1:evicted 0 | ||
+ | STAT items:1:evicted_nonzero 0 | ||
+ | STAT items:1:evicted_time 0 | ||
+ | STAT items:1:outofmemory 0 | ||
+ | STAT items:1:tailrepairs 0 | ||
+ | STAT items:1:reclaimed 0 | ||
+ | STAT items:1:expired_unfetched 0 | ||
+ | STAT items:1:evicted_unfetched 0 | ||
+ | END | ||
+ | </code> | ||
+ | ===== 字符拼接命令 ===== | ||
+ | <code> | ||
+ | set name 0 0 3 #set设置name | ||
+ | gxx | ||
+ | STORED | ||
+ | get name #get获得name | ||
+ | VALUE name 0 3 | ||
+ | gxx | ||
+ | END | ||
+ | append name 0 0 2 #append在name后面拼接2个字节 | ||
+ | 12 | ||
+ | STORED | ||
+ | get name #get获得name | ||
+ | VALUE name 0 5 | ||
+ | gxx12 | ||
+ | END | ||
+ | prepend name 0 0 3 #prepend在name前面拼接3个字节 | ||
+ | 123 | ||
+ | STORED | ||
+ | get name #get获得name | ||
+ | VALUE name 0 8 | ||
+ | 123gxx12 | ||
+ | END | ||
+ | flush_all #flush_all清空所有数据 | ||
+ | OK | ||
+ | get name #get获得name,返回空 | ||
+ | END | ||
+ | </code> | ||
+ | ===== 加减计数命令 ===== | ||
+ | <code> | ||
+ | set count 0 0 1 #set设置count为1 | ||
+ | 1 | ||
+ | STORED | ||
+ | incr count 1 #incr给count加1为2 | ||
+ | 2 | ||
+ | incr count 5 #incr给count加5为7 | ||
+ | 7 | ||
+ | get count #get获得count | ||
+ | VALUE count 0 1 | ||
+ | 7 | ||
+ | END | ||
+ | decr count 2 #decr给count减2为5 | ||
+ | 5 | ||
+ | get count #get获得count | ||
+ | VALUE count 0 1 | ||
+ | 5 | ||
+ | END | ||
+ | </code> | ||
+ | ====== memcached的业务场景 ====== | ||
+ | ===== 适用的场景 ===== | ||
+ | - 如果网站包含了访问量很大的动态网页,因而数据库的负载将会很高。由于大部分数据库请求都是读操作,那么memcached可以显著地减小数据库负载。 | ||
+ | - 如果数据库服务器的负载比较低但CPU使用率很高,这时可以缓存计算好的结果( computed objects )和渲染后的网页模板(enderred templates)。 | ||
+ | - 利用memcached可以缓存 session数据 、临时数据以减少对他们的数据库写操作。 | ||
+ | - 缓存一些很小但是被频繁访问的文件。 | ||
+ | - 缓存Web 'services'(非IBM宣扬的Web Services,译者注)或RSS feeds的结果.。 | ||
+ | ===== 不适用的场景 ===== | ||
+ | - 缓存对象的大小大于1MB,Memcached本身就不是为了处理庞大的多媒体(large media)和巨大的二进制块(streaming huge blobs)而设计的。 | ||
+ | - key的长度大于250字符 | ||
+ | - 虚拟主机不让运行memcached服务,如果应用本身托管在低端的虚拟私有服务器上,像vmware, xen这类虚拟化技术并不适合运行memcached。Memcached需要接管和控制大块的内存,如果memcached管理的内存被OS或 hypervisor交换出去,memcached的性能将大打折扣。 | ||
+ | - 应用运行在不安全的环境中,Memcached为提供任何安全策略,仅仅通过telnet就可以访问到memcached。如果应用运行在共享的系统上,需要着重考虑安全问题。 | ||
+ | - 业务本身需要的是持久化数据或者说需要的应该是database |