Python-redis keys() returns list of bytes objects instead of strings

前端 未结 4 1614
孤独总比滥情好
孤独总比滥情好 2021-02-01 14:37

I\'m using the regular redis package in order to connect my Python code to my Redis server.

As part of my code I check if a string object is existed in my R

4条回答
  •  梦毁少年i
    2021-02-01 14:59

    You would be better off using the EXISTS command and restructuring your code like:

    string = 'abcde'
    if redis.exists(string):
        do something..
    

    The KEYS operation returns every key in your Redis database and will cause serious performance degradation in production. As a side effect you avoid having to deal with the binary to string conversion.

    You can configure the Redis client to automatically convert responses from bytes to strings using the decode_responses argument to the StrictRedis constructor:

    r = redis.StrictRedis('localhost', 6379, charset="utf-8", decode_responses=True)
    

    Make sure you are consistent with the charset option between clients.

提交回复
热议问题