如何使用包含空格的键从Redis获取值?

use*_*353 6 key redis

使用telnet我输入这样的命令行命令

get field with spaces
get "field with spaces"
get 'field with spaces'
Run Code Online (Sandbox Code Playgroud)

而这三个都返回相同的错误.

-ERR wrong number of arguments for 'get' command
Run Code Online (Sandbox Code Playgroud)

Col*_*lum 10

你用的是什么版本的redis?它使用双引号在2.2.2上工作正常

root@this:~# redis-cli
redis> set "test space" hello
OK
redis> get "test space"
"hello"
redis> get 'test space'
(error) ERR wrong number of arguments for 'get' command
redis> 
Run Code Online (Sandbox Code Playgroud)


小智 5

获取“字段\带\空格”

这对我有用。


Rod*_*ira 5

如果您只有telnet(而不是'redis-cli'),那么您需要使用Redis二进制安全统一协议来使用密钥名称中的空格,例如:

telnet localhost 6379
*2
$3
GET
$17
field with spaces
hello (this is Redis answer if "field with spaces" contains value "hello")

Explanation:
*2 = Number of arguments (first arg is "GET" and second is "field with spaces")
$3 = length of first argument ("GET" contains 3 bytes)
$17 = length of second argument ("field with spaces" contains 17 bytes)
Run Code Online (Sandbox Code Playgroud)

有关Redis二进制安全协议的更多信息:http://redis.io/topics/protocol