How to save and retrieve string with accents in redis?

前端 未结 1 1585
青春惊慌失措
青春惊慌失措 2020-12-17 03:41

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 ?

         


        
相关标签:
1条回答
  • 2020-12-17 04:05

    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.

    0 讨论(0)
提交回复
热议问题