Luc
Luc

Reputation: 17082

How to save and retrieve string with accents in redis?

I do not manage to set and retrieve string with accents in my redis db.
Chars with accents are encoded, how can I retrieve them back as they where set ?

redis> set test téléphone
OK
redis> get test
"t\xc3\xa9l\xc3\xa9phone"

I know this has already been asked (http://stackoverflow.com/questions/6731450/redis-problem-with-accents-utf-8-encoding) but there is no detailed answer.

Upvotes: 7

Views: 8014

Answers (1)

Didier Spezia
Didier Spezia

Reputation: 73306

The Redis server itself stores all data as a binary objects, so it is not dependent on the encoding. The server will just store what is sent by the client (including UTF-8 chars).

Here are a few experiments:

$ echo téléphone | hexdump -C
00000000  74 c3 a9 6c c3 a9 70 68  6f 6e 65 0a              |t..l..phone.|

c3a9 is the representation of the 'é' char.

$ redis-cli
> set t téléphone
OK
> get t
"t\xc3\xa9l\xc3\xa9phone"

Actually the data is correctly stored in the Redis server. However, when it is launched in a terminal, the Redis client interprets the output and applies the sdscatrepr function to transform non printable chars (whose definition is locale dependent, and may be broken for multibyte chars anyway).

A simple workaround is to launch redis-cli with the 'raw' option:

$ redis-cli --raw
> get t
téléphone

Your own application will probably use one of the client libraries rather than redis-cli, so it should not be a problem in practice.

Upvotes: 24

Related Questions