How to store and retrieve a dictionary with redis

前端 未结 11 1307
遇见更好的自我
遇见更好的自我 2020-12-07 10:20
# I have the dictionary my_dict
my_dict = {
    \'var1\' : 5
    \'var2\' : 9
}
r = redis.StrictRedis()

How would I store my_dict and retrieve it w

11条回答
  •  难免孤独
    2020-12-07 10:48

    Another way: you can use RedisWorks library.

    pip install redisworks

    >>> from redisworks import Root
    >>> root = Root()
    >>> root.something = {1:"a", "b": {2: 2}}  # saves it as Hash type in Redis
    ...
    >>> print(root.something)  # loads it from Redis
    {'b': {2: 2}, 1: 'a'}
    >>> root.something['b'][2]
    2
    

    It converts python types to Redis types and vice-versa.

    >>> root.sides = [10, [1, 2]]  # saves it as list in Redis.
    >>> print(root.sides)  # loads it from Redis
    [10, [1, 2]]
    >>> type(root.sides[1])
    
    

    Disclaimer: I wrote the library. Here is the code: https://github.com/seperman/redisworks

提交回复
热议问题