memcached 常用命令及使用说明

来源:互联网 发布:分分彩计划软件 编辑:程序博客网 时间:2024/05/10 02:58

memcached 查看方法

格式: telnet ip port

例如 telnet localhost 11211
退出命令:quit


一.存储命令

存储命令格式:

[plain] view plaincopy
  1. <command name> <key> <flag> <expire> <bytes>  
  2. <data block>  

参数说明:

command name命令名称key查找关键字flag存储额外信息expire数据保存时间,0表示永远,单位秒bytes存储数据的字节数data block存储的数据

1.set  无论如何都存储,数据不存在时存储,数据存在时更新。

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. set mykey 0 0 3  
  6. 456  
  7. STORED  
2.add 当数据不存在时存储。

[plain] view plaincopy
  1. add mykey 0 0 3  
  2. 123  
  3. STORED  
  4. add mykey 0 0 3  
  5. 456  
  6. NOT_STORED  
3.replace 当数据存在时存储

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. replace mykey 0 0 3  
  6. 456  
  7. STORED  
  8.   
  9. delete mykey  
  10. DELETED  
  11.   
  12. replace mykey 0 0 3  
  13. 678  
  14. NOT_STORED  

二.读取命令


1.get
 key 可以一个或多个,用空格格开。

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. set mykey1 0 0 3  
  6. 456  
  7. STORED  
  8.   
  9. get mykey mykey1  
  10. VALUE mykey 0 3  
  11. 123  
  12. VALUE mykey1 0 3  
  13. 456  
  14. END  
2.gets 与 get 一样,但会返回多一个数字,这个数字用来检查数据是否被修改过,如修改过,这个数字回改变。

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. gets mykey  
  6. VALUE mykey 0 3 7  
  7. 123  
  8. END  
  9.   
  10. replace mykey 0 0 3  
  11. 888  
  12. STORED  
  13.   
  14. gets mykey  
  15. VALUE mykey 0 3 8  
  16. 888  
  17. END  

3.cas cas即checked and set ,当最后一个参数与gets返回的数字一致时才存储,否则返回EXISTS。

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. gets mykey  
  6. VALUE mykey 0 3 9  
  7. 123  
  8. END  
  9.   
  10. cas mykey 0 0 3 8  
  11. 456  
  12. EXISTS  
  13.   
  14. cas mykey 0 0 3 9  
  15. 456  
  16. STORED  

三.追加与清除命令


1.append
 将数据追加到当前缓存数据的之后,当缓存数据存在时才存储。

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. append mykey 0 0 3  
  6. 456  
  7. STORED  
  8.   
  9. get mykey  
  10. VALUE mykey 0 6  
  11. 123456  
  12. END  
  13.   
  14. append notexists 0 0 3  
  15. 456  
  16. NOT_STORED  

2.prepend 将数据追加到当前缓存数据的之前,当缓存数据存在时才存储。

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. prepend mykey 0 0 3  
  6. 456  
  7. STORED  
  8.   
  9. get mykey  
  10. VALUE mykey 0 6  
  11. 456123  
  12. END  
  13.   
  14. prepend notexists 0 0 3  
  15. 456  
  16. NOT_STORED  

3.delete 删除缓存数据,数据存在返回DELETED,数据不存在返回NOT_FOUND

[plain] view plaincopy
  1. set mykey 0 0 3  
  2. 123  
  3. STORED  
  4.   
  5. delete mykey  
  6. DELETED  
  7.   
  8. delete mykey  
  9. NOT_FOUND  
4.flush_all 将当前所有缓存数据设置为过期,但不会释放内存。
[plain] view plaincopy
  1. flush_all  
  2. OK  

四.状态命令

1.stats 查看memcached运行状态

[plain] view plaincopy
  1. pid                     Memcached 进程ID    
  2.     
  3. uptime                  Memcached 运行时间,单位:秒    
  4.     
  5. time                    Memcached 当前的UNIX时间    
  6.     
  7. version                 Memcached 的版本号    
  8.     
  9. rusage_user             该进程累计的用户时间,单位:秒    
  10.     
  11. rusage_system           该进程累计的系统时间,单位:秒    
  12.     
  13. curr_items              Memcached 当前存储的内容数量    
  14.     
  15. total_items             Memcached 启动以来存储过的内容总数    
  16.     
  17. bytes                   Memcached 当前存储内容所占用的字节数(*/1024/1024=mb)    
  18.     
  19. curr_connections        当前连接数量    
  20.     
  21. total_connections       Memcached 运行以来接受的连接总数    
  22.     
  23. connection_structures   Memcached 分配的连接结构的数量    
  24.     
  25. cmd_get                 查询请求总数    
  26.     
  27. cmd_set                 存储(添加/更新)请求总数    
  28.     
  29. get_hits                查询成功获取数据的总次数    
  30.     
  31. get_misses              查询成功未获取到数据的总次数    
  32.     
  33. bytes_read              Memcached 从网络读取到的总字节数    
  34.     
  35. bytes_written           Memcached 向网络发送的总字节数    
  36.     
  37. limit_maxbytes          Memcached 在存储时被允许使用的字节总数    

2.stats items
执行stats items,可以看到STAT items行,如果memcached存储内容很多,那么这里也会列出很多的STAT items行。


3.stats cachedump slabs_id limit_num

slabs_id:由stats items返回的结果(STAT items后面的数字)决定的
limit_num:返回的记录数,0表示返回所有记录
通过stats items、stats cachedump slab_id limit_num配合get命令可以遍历memcached的记录。

[plain] view plaincopy
  1. stats cachedump 1 0  
  2. ITEM mykey [3 b; 1362880145 s]  
  3. END  


4.stats slabs
 显示各个slab的信息,包括chunk的大小、数目、使用情况等


5.stats sizes
 输出所有item的大小和个数


6.stats reset
 清空统计数据