redis. how set key name with empty string (not empty value) - redis

I'm making Redis client with swift.
How can i make key name with empty string. (not empty value)
in redis-cli.
I can make key name with empty string.
127.0.0.1:6379> set "" "stringValue"
OK
but, I am using socket to connect Redis server.
and send command, I got error.
telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set "" "stringValue"
-ERR wrong number of arguments for 'set' command
please help me.

You used the inline command to send command to Redis, which does not work with binary data, or data which has 0 length or contains whitespace.
Instead, you should use the more complicated form: *3\r\n$3\r\nset\r\n$0\r\n\r\n$3\r\nval\r\n
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
*3
$3
set
$0
$3
val
+OK

Related

How to set to redis value if the value contain multiline?

I am not able to set multiline value to redis? is there any better way to do?
I found the answer.
Just do like
printf "*3\r\n\$3\r\nset\r\n\$3\r\nfoo\r\n\$15\r\nMyGroup\r\nGroupA\r\n" | nc localhost 6379
* Specify the rows
\$ count the character

How many white spaces is the best for ssh config

For Ruby, using 2 spaces is the best.
For Python, using 4 spaces is the best.
But for ssh config file, how many spaces is the best?
I found the originally accepted answer a bit confusing so I thought I'd contribute some additional information.
To the original question, ssh config files allow, but do not require, indentation with whitespace (either tabs or spaces). Blank lines and lines beginning with a hash # are ignored.
The config file consists of stanzas, each beginning with the reserved word Host or Match followed by a list of options until the stanza ends at the next Host, Match or end of file.
The options can be specified as name value or name=value. Looking at the OpenSSH release notes, it appears the developers use the name=value format. Leading whitespace is ignored. Unquoted in-line whitespace is also ignored
The following (mixing with and without equals and whitespace) are equivalent
Host test1
Hostname = 192.168.0.100
Host test1
Hostname 192.168.0.100
Host=test1
Hostname 192.168.0.100
Note that the equal sign is significant when parsing options. Values with embedded equals signs need to be quoted. This contrived example demonstrates what happens without quotes.
Host test1
Hostname = 192.168.0.100
UserKnownHostsFile /tmp/name_with=equals /tmp/name2
Will look for known host in /tmp/name_with and in /tmp/name2 but not in /tmp/name_with=equals.
The configuration files (for ssh or other programs) do not need indentation.
They contain lines of type name=value.
Some programs allow spaces around the equal sign, others are more strict and do not accept them.
ssh accepts spaces around the equal sign but they are ignored. Use how many of them you like but don't abuse them and let the file be readable.
A small fragment from the documentation:
The file contains keyword-argument pairs, one per line. Lines starting with # and empty lines are interpreted as comments. Arguments may optionally be enclosed in double quotes (") in order to represent arguments containing spaces. Configuration options may be separated by whitespace or optional whitespace and exactly one =; the latter format is useful to avoid the need to quote whitespace when specifying configuration options using the ssh, scp, and sftp -o option.

Redis command line SET value containing double quotes

I want to use redis command line (using redis-cli) to store json values. This is what I do
redis 127.0.0.1:6379> set test '{"a":"b"}'
This command fails with message :
Invalid argument(s)
I don't have problem with setting values that don't contain double quotes. What is the correct way to escape double quotes?
Add slashes to quotes
set test "{\"a\":\"b\"}"
We can use single quotes for storing the JSON value:
set name '{"a":"b"}'
Run get query like: get name
output : {"a":"b"}
later redis has fixed this problem. single quote works fine.

Why does Redis not work with requirepass directive?

I want to set a password to connect to a Redis server.
The appropriate way to do that is using the requirepass directive in the configuration file.
http://redis.io/commands/auth
However, after setting the value, I get this upon restarting Redis:
Stopping redis-server: redis-server.
Starting redis-server: Segmentation fault (core dumped)
failed
Why is that?
The password length is limited to 512 characters.
In redis.h:
#define REDIS_AUTHPASS_MAX_LEN 512
In config.c:
} else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
if (strlen(argv[1]) > REDIS_AUTHPASS_MAX_LEN) {
err = "Password is longer than REDIS_AUTHPASS_MAX_LEN";
goto loaderr;
}
server.requirepass = zstrdup(argv[1]);
}
Now, the parsing mechanism of the configuration file is quite basic. All the lines are split using the sdssplitargs function of the sds (string management) library. This function interprets specific sequence of characters such as:
single and double quotes
\x hex digits
special characters such as \n, \r, \t, \b, \a
Here the problem is your password contains a single double quote character. The parsing fails because there is no matching double quote at the end of the string. In that case, the sdssplitargs function returns a NULL pointer. The core dump occurs because this pointer is not properly checked in the config.c code:
/* Split into arguments */
argv = sdssplitargs(lines[i],&argc);
sdstolower(argv[0]);
This is a bug that should be filed IMO.
A simple workaround would be to replace the double quote character or any other interpreted characters by an hexadecimal sequence (ie. \x22 for the double quote).
Although not documented, it seems there are limitations to the password value, particularly with the characters included, not the length.
I tried with 160 characters (just digits) and it works fine.
This
9hhNiP8MSHZjQjJAWE6PmvSpgVbifQKCNXckkA4XMCPKW6j9YA9kcKiFT6mE
too. But this
#hEpj6kNkAeYC3}#:M(:$Y,GYFxNebdH<]8dC~NLf)dv!84Z=Tua>>"A(=A<
does not.
So, Redis does not support some or all of the "special characters".
Just nailed this one with:
php: urlencode('crazy&char's^pa$$wor|]');
-or-
js: encodeURIComponent('crazy&char's^pa$$wor|]');
Then it can be used anywhere sent to the redis server via (usually) tcp

Accents stored in Redis not being readable

Working with Redis 2.10 using redis-cli on Linux, I am faced with a problem regarding accents...
If I execute the command
set "string" "à"
=> I get "\xc3\xa0"
It seems each converted accent begin with "\xc3"
How do I get my original string back?
Try using
redis-cli --raw
It solved problem for me.
"\xc3\xa0" is just Unicode "à" in UTF-8 encoding. Just decode the string and you're done...
"you string".encode("utf-8")
when you need get the string
"you string".decode("utf-8")
You need to spec the version of Redis and more importantly the client you are using.
If you are using a telnet client, the problem may be your client. Redis supports arbitrary bytes for values and UTF-8 is not a problem at all (if your client is properly converting the entered glyphs to the associated byte sequence.)