JosephScott / Marmerine
另一种Memcached实现
0.0.3
2022-05-21 17:37 UTC
Requires
- php: >=8.1.0
- workerman/workerman: 4.0.37
Requires (Dev)
- pestphp/pest: 1.21.3
- phpstan/phpstan: 1.6.7
Suggests
- ext-event: Better for PHP workerman performance
README
这是一个Memcached的替代实现。
git clone https://github.com/josephscott/marmerine.git
composer install
php server.php start
存储命令
set
- 支持: 是 ✅
- 格式:
set <key> <flags> <exptime> <length> [noreply]\r\n<data>\r\n
- 成功响应:
STORED\r\n
- 错误响应:
CLIENT_ERROR [error]\r\nERROR\r\n
示例
$ printf "set thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED
$ printf "set thing 0 300 3\r\nabc123XYZ\r\n" | nc localhost 11211 CLIENT_ERROR bad data chunk ERROR
描述
这将存储给定键的值,即使它已经存在。
add
- 支持: 是 ✅
- 格式:
add <key> <flags> <exptime> <length> [noreply]\r\n<data>\r\n
- 成功响应:
STORED\r\n
- 失败响应:
NOT_STORED\r\n
- 错误响应:
CLIENT_ERROR [error]\r\nERROR\r\n
示例
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED
$ printf "add thing 0 300 3\r\nabc123XYZ\r\n" | nc localhost 11211 CLIENT_ERROR bad data chunk ERROR
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 NOT_STORED
描述
如果给定的键不存在,则存储其值。
replace
- 支持: 是 ✅
- 格式:
replace <key> <flags> <exptime> <length> [noreply]\r\n<data>\r\n
- 成功响应:
STORED\r\n
- 错误响应:
NOT_STORED\r\n
示例
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "replace thing 0 300 12\r\nabc-REPLACED\r\n" | nc localhost 11211 STORED $ printf "get thing\r\n" | nc localhost 11211 VALUE thing 0 12 abc-REPLACED END
$ printf "replace thing 0 300 3\r\nabc\r\n" | nc localhost 11211 NOT_STORED
描述
这将替换给定键的值。
append
- 支持: 是 ✅
- 格式:
append <key> <flags> <exptime> <length> [noreply]\r\n<data>\r\n
- 成功响应:
STORED\r\n
- 错误响应:
NOT_STORED\r\n
示例
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "append thing 0 300 6\r\n-AFTER\r\n" | nc localhost 11211 STORED $ printf "get thing\r\n" | nc localhost 11211 VALUE thing 0 9 abc-AFTER END
$ printf "append thing 0 300 6\r\n-AFTER\r\n" | nc localhost 11211 NOT_STORED
描述 这将向现有键的值追加一个字符串。
prepend
- 支持: 是 ✅
- 格式:
prepend <key> <flags> <exptime> <length> [noreply]\r\n<data>\r\n
- 成功响应:
STORED\r\n
- 错误响应:
NOT_STORED\r\n
EXAMPLES
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "prepend thing 0 300 7\r\nBEFORE-\r\n" | nc localhost 11211 STORED $ printf "get thing\r\n" | nc localhost 11211 VALUE thing 0 10 BEFORE-abc END
描述 这将向现有键的值前缀一个字符串。
cas
- 支持: 是 ✅
- 格式:
cas <key> <flags> <expiry> <datalen> <cas> [noreply]\r\n<data>\r\n
- 成功响应:
STORED\r\n
- 错误响应:
NOT_STORED\r\n
EXAMPLES
$ printf "set thing 0 300 3\r\nabc\r\n" | nc localhost 11211
STORED
$ printf "gets thing\r\n" | nc localhost 11211
VALUE thing 0 3 1
abc
END
$ printf "cas thing 0 300 3 1\r\nXYZ\r\n"| nc localhost 11211
STORED
描述
如果cas令牌未更改,则设置键的新值。
touch
- 支持: 是 ✅
- 格式:
touch <key> <expiry> [noreply]\r\n
- 成功响应:
TOUCHED\r\n
- 错误响应:
NOT_FOUND\r\n
示例
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "touch thing 1800\r\n" | nc localhost 11211 TOUCHED
描述
更新现有键的过期时间。
检索命令
get
- 支持: 是 ✅
- 格式:
get <key> [key2 key3 ... keyn]\r\n
- 找到响应:
VALUE <key> <flags> <length>\r\n<data>\r\nEND\r\n
- 未找到响应:
END\r\n
示例
$ printf "set thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "get thing\r\n" | nc localhost 11211 VALUE thing 0 3 abc END
$ printf "get thing\r\n" | nc localhost 11211 END
$ printf "set thing1 0 300 4\r\n1abc\r\n" | nc localhost 11211 STORED $ printf "set thing2 0 300 4\r\n2abc\r\n" | nc localhost 11211 STORED $ printf "set thing3 0 300 4\r\n3abc\r\n" | nc localhost 11211 STORED $ printf "get thing thing1 thing2 thing3\r\n" | nc localhost 11211 VALUE thing1 0 4 1abc VALUE thing2 0 4 2abc VALUE thing3 0 4 3abc END
描述
获取给定键的值。当键不存在时,只返回 END\r\n
的响应。当提供多个键时,只返回存在的键。
gets
- 支持: 是 ✅
- 格式:
get <key> [key2 key3 ... keyn]\r\n
- 找到响应:
VALUE <key> <flags> <length> <cas>\r\n<data>\r\nEND\r\n
- 未找到响应:
END\r\n
示例
$ printf "set thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "gets thing\r\n" | nc localhost 11211 VALUE thing 0 3 1 abc END
$ printf "gets thing\r\n" | nc localhost 11211 END
描述
这是带有响应中cas令牌的get命令。
gat
- 支持: 否 ⛔
gats
- 支持: 否 ⛔
删除命令
delete
- 支持: 是 ✅
- 格式:
delete <key> [noreply]\r\n
- 成功响应:
DELETED\r\n
- 未找到响应:
NOT_FOUND\r\n
示例
$ printf "set thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "delete thing\r\n" | nc localhost 11211 DELETED
$ printf "delete thing\r\n" | nc localhost 11211 NOT_FOUND
描述
删除给定的键。如果键不存在,则返回 NOT_FOUND
。
flush_all
- 支持: 是 ✅
- 格式:
flush_all [delay] [noreply]\r\n
- 每个响应:
OK\r\n
示例
$ printf "flush_all\r\n" | nc localhost 11211 OK
$ printf "flush_all\r\n" | nc localhost 11211 OK $ printf "flush_all\r\n" | nc localhost 11211 OK
描述
删除所有存储的键。没有失败或错误条件,它总是返回 OK
。
算术命令
incr
- 支持: 是 ✅
- 格式:
incr <key> <value> [noreply]\r\n
- 成功响应:
<incremented value>\r\n
- 错误响应:
CLIENT_ERROR cannot increment or decrement non-numeric value
示例
$ printf "add thing 0 300 1\r\n1\r\n" | nc localhost 11211 STORED $ printf "incr thing 1\r\n" | nc localhost 11211 2 $ printf "incr thing 1\r\n" | nc localhost 11211 3
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "incr thing 1\r\n" | nc localhost 11211 CLIENT_ERROR cannot increment or decrement non-numeric value
描述
这仅适用于整数值。
decr
- 支持: 是 ✅
- 格式:
decr <key> <value> [noreply]\r\n
- 成功响应:
<decremented value>\r\n
- 错误响应:
CLIENT_ERROR cannot increment or decrement non-numeric value
示例
$ printf "add thing 0 300 1\r\n9\r\n" | nc localhost 11211 STORED $ printf "decr thing 1\r\n" | nc localhost 11211 8 $ printf "decr thing 1\r\n" | nc localhost 11211 7
$ printf "add thing 0 300 3\r\nabc\r\n" | nc localhost 11211 STORED $ printf "decr thing 1\r\n" | nc localhost 11211 CLIENT_ERROR cannot increment or decrement non-numeric value
描述
这仅适用于整数值。
其他命令
quit
- 支持: 是 ✅
- 格式:
quit\r\n
- 每个响应: ( None )
示例
$ printf "quit\r\n" | nc localhost 11211
描述
这会关闭与服务器的连接。它不会返回任何内容。
版本
- 支持: 是 ✅
- 格式:
version\r\n
- 每个响应:
VERSION <version>\r\n
示例
$ printf "version\r\n" | nc localhost 11211 VERSION 1.6.12
描述
从服务器获取版本号。
详细程度
- 支持: 否 ⛔
统计数据
- 支持: 是 ✅
- 格式:
stats\r\n
- 每个响应:
STAT <stat> <value>\r\nEND\r\n
示例
$ printf "stats\r\n" | nc localhost 11211 STAT pid 92458 STAT uptime 9 STAT time 1650595977 END
描述
当前支持的统计数据
- 进程ID
- 运行时间
- 时间
- 总连接数
- 当前项目数
- 命令_
- 命中次数
- 缺失次数
Memcached资源
- Memcached,原始版本。
- Memcached Protocol.txt,协议的通用描述。
- Twemproxy,Twitter提供的Memcached和Redis代理。
- Twemproxy Memcached Notes,Twemproxy支持的Memcached命令的有用列表。
- MySQL Memcached TCP Text Protocol,MySQL实现中概述的命令。
- Memcached Cheat Sheet,其他Memcached资源和示例的列表。
- libmemcached-awesome,原始libmemcached的更新版本。
- memc.rs,用Rust编写的Memcached克隆,与二进制协议兼容。
- memtier_benchmark,Memcached(和Redis)基准测试工具。
- 过期时间,对Memcached处理过期时间的良好描述。