Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and retrieve string with accents in redis?

Tags:

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.

like image 847
Luc Avatar asked Jan 22 '12 18:01

Luc


People also ask

Can UTF 8 data be stored and retrieved with the string data type without any loss or corruption of the data Redis?

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.)

How strings are stored in Redis?

Keys that hold strings can only hold one value; you cannot store more than one string in a single key. However, strings in Redis are binary-safe, meaning a Redis string can hold any kind of data, from alphanumeric characters to JPEG images. The only limit is that strings must be 512 MB long or less.

Can Redis only store strings?

In Redis, we have the advantage of storing both strings and collections of strings as values. A string value cannot exceed 512 MB of text or binary data. However, it can store any type of data, like text, integers, floats, videos, images, and audio files.

Does Redis allow duplicate keys?

You can use the DUMP and RESTORE commands to duplicate the key: use the DUMP command to serialize the value of a key. use the RESTORE command to restore the serialized value to another key.


1 Answers

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.

like image 112
Didier Spezia Avatar answered Jan 03 '23 23:01

Didier Spezia