Source

redis-dict /

Filename Size Date modified Message
6 B
1.5 KB
2.4 KB
540 B
2.8 KB

redis-dict

Really tiny wrapper around redis hash which serializes values (by default it uses pickle).

USAGE

KEYS

RedisDict accepts only str instances as keys - it is impossible relly on Python __hash__ function as its value can differ on different machines (for example hash(None) is derived from None object id).

UPDATING VALUES

RedisDict.execute_update

If you store mutable objects and want to update them you have to be careful:

  • you have to explicite overwrite value in RedisDict instance:

    >>> import redis_dict, redis
    >>> connection = redis.Redis()
    >>> rd = redis_dict.RedisDict('new-dict', connection)
    >>> rd['a'] = {'key': 'value'}
    >>> a = rd['a']
    >>> # this call doesn't affects redis state
    >>> a['key'] = 'new value'
    >>> print rd['a']['key']
    value
    >>> # you have to explicite override
    >>> rd['a'] = a
    >>> print rd['a']['key']
    new value
    
  • such an update is not atomic operation - prefered way to do it is to use execute_update method which uses redis transactions (WATCH and MULTI) to ensure atomicity of operation:

    >>> import redis_dict, redis
    >>> connection = redis.Redis()
    >>> rd = redis_dict.RedisDict('new-dict', connection)
    >>> rd['a'] = {'key': 'value'}
    >>> # updater receives current value and should return new value
    >>> rd.execute_update('a', lambda curr_value: dict(curr_value, key='new value'))
    >>> print rd['a']['key']
    new value