# Memcache Commands {{#include ../../banners/hacktricks-training.md}} ## Commands Cheat-Sheet **来自** [**https://lzone.de/cheat-sheet/memcached**](https://lzone.de/cheat-sheet/memcached) 支持的命令(官方和一些非官方的)在 [doc/protocol.txt](https://github.com/memcached/memcached/blob/master/doc/protocol.txt) 文档中有记录。 遗憾的是,语法描述并不清晰,简单的帮助命令列出现有命令会更好。以下是您可以在 [source](https://github.com/memcached/memcached) 中找到的命令概述(截至 2016 年 8 月 19 日): | Command | Description | Example | | -------------------- | --------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | | get | 读取一个值 | `get mykey` | | set | 无条件设置一个键 |

set mykey

确保在使用 Unix CLI 工具时使用 \r\n 作为换行符。例如

printf "set mykey 0 60 4\r\ndata\r\n" | nc localhost 11211

| | add | 添加一个新键 | `add newkey 0 60 5` | | replace | 覆盖现有键 | `replace key 0 60 5` | | append | 将数据附加到现有键 | `append key 0 60 15` | | prepend | 将数据前置到现有键 | `prepend key 0 60 15` | | incr | 将数值键的值增加给定的数字 | `incr mykey 2` | | decr | 将数值键的值减少给定的数字 | `decr mykey 5` | | delete | 删除现有键 | `delete mykey` | | flush_all | 立即使所有项目失效 | `flush_all` | | flush_all | 在 n 秒内使所有项目失效 | `flush_all 900` | | stats | 打印一般统计信息 | `stats` | | | 打印内存统计信息 | `stats slabs` | | | 打印更高层次的分配统计信息 | `stats malloc` | | | 打印项目的信息 | `stats items` | | | | `stats detail` | | | | `stats sizes` | | | 重置统计计数器 | `stats reset` | | lru_crawler metadump | 转储缓存中(所有)项目的大部分元数据 | `lru_crawler metadump all` | | version | 打印服务器版本。 | `version` | | verbosity | 增加日志级别 | `verbosity` | | quit | 终止会话 | `quit` | #### Traffic Statistics 您可以使用命令查询当前的流量统计信息 ``` stats ``` 您将获得一个列表,其中提供连接数、进出字节以及更多信息。 示例输出: ``` STAT pid 14868 STAT uptime 175931 STAT time 1220540125 STAT version 1.2.2 STAT pointer_size 32 STAT rusage_user 620.299700 STAT rusage_system 1545.703017 STAT curr_items 228 STAT total_items 779 STAT bytes 15525 STAT curr_connections 92 STAT total_connections 1740 STAT connection_structures 165 STAT cmd_get 7411 STAT cmd_set 28445156 STAT get_hits 5183 STAT get_misses 2228 STAT evictions 0 STAT bytes_read 2112768087 STAT bytes_written 1000038245 STAT limit_maxbytes 52428800 STAT threads 1 END ``` #### Memory Statistics 您可以使用查询当前的内存统计信息。 ``` stats slabs ``` 请提供需要翻译的具体内容。 ``` STAT 1:chunk_size 80 STAT 1:chunks_per_page 13107 STAT 1:total_pages 1 STAT 1:total_chunks 13107 STAT 1:used_chunks 13106 STAT 1:free_chunks 1 STAT 1:free_chunks_end 12886 STAT 2:chunk_size 100 STAT 2:chunks_per_page 10485 STAT 2:total_pages 1 STAT 2:total_chunks 10485 STAT 2:used_chunks 10484 STAT 2:free_chunks 1 STAT 2:free_chunks_end 10477 [...] STAT active_slabs 3 STAT total_malloced 3145436 END ``` 如果您不确定您的 memcached 实例是否有足够的内存,请始终关注“stats”命令给出的“evictions”计数器。如果实例有足够的内存,“evictions”计数器应该为 0 或至少不增加。 #### 哪些键被使用? 没有内置函数可以直接确定当前的键集。但是您可以使用 ``` stats items ``` 确定存在多少个键的命令。 ``` stats items STAT items:1:number 220 STAT items:1:age 83095 STAT items:2:number 7 STAT items:2:age 1405 [...] END ``` 这至少有助于查看是否使用了任何密钥。要从已经进行 memcache 访问的 PHP 脚本中转储密钥名称,可以使用来自 [100days.de](http://100days.de/serendipity/archives/55-Dumping-MemcacheD-Content-Keys-with-PHP.html) 的 PHP 代码。 {{#include ../../banners/hacktricks-training.md}}