API Reference

This documentation is generated from the docstrings found in the source.

Main Interface

class redicts.Proxy(path, lock_acquire_timeout=10, lock_expire_timeout=30, db_name=None)[source]

Create a new Proxy.

Parameters:
  • str_or_iterable (path) – The path where this value is stored. Can be a string (a dotted path) or an iterable of strings.
  • redis.Redis (rconn) – Optional; the redis connection to use.
Returns Proxy:

The ready to use Proxy.

acquire()

Acquire a lock on this value and all of it’s children.

add(count)

Convinience function to add a count to this value. If the value did not exist yet, it will be set to count. Will raise an ValueError if the key exists and does not support the add operator.

Parameters:count – (int) The count to increment.
Returns:The new total count.
clear()

Clear this level of the value tree including all children

delete(key)

Delete an existing key.

Parameters:key – (str) A dotted path.
exists()

Return true if this value actually exists

expire(seconds)

Expire (i.e. delete) the key after a certain number of seconds. After this time .val() will return None and .exists() will return False.

Parameters:seconds – (int) seconds after this value will no longer accessible.
get(key)

Return a lazy value for this key.

Parameters:key – (str) A dotted path or simple
Returns:A child Proxy.
is_locked()

Check if the node or any of its parents are locked

iter_children()

Iterate over all children including and below this node.

Returns:A generator object, yielding ValueProxies.
key()

Return the key of this value in redis

release()

Release a previously acquired lock.

Note that this does not clear the locks of the children, if you locked those explicitly you have to release them.

set(key, value, expire=None)

Set a new value to this key.

Parameters:
  • key – (str) A dotted path.
  • value – (object) Any value that can be passed to json.dumps.
  • expire – (int) Time in seconds when to expire this key or None.
time_to_live()

Return the amount of seconds, this value will be accessible.

Returns int:the amount to live in seconds.
val(default=None)

Get the actual value of this proxy

redicts.root(*args, **kwargs)[source]

Return the root Proxy

redicts.section(name, *args, **kwargs)[source]

Convience method for getting a Proxy for a first-level section. Try to to use a unique name, otherwise you might overwrite foreign keys. A good idiom is to use something like this to get a descriptive, but unique name for your module:

section(__name__)
Parameters:str (name) – The section name. May not contain dots.
Returns:A Proxy for the section.
class redicts.Pool(cfg=None)[source]

Pool of redis connections

get_connection(db_name=None)[source]

Get a new (or recycled) connection. Note: This function may block if there are too many connections open.

Return redis.StrictRedis:
 A new redis connection.
reload(cfg=None, fake_redis=False)[source]

Reload the pool, disconnecting previous connections and creating a new pool.

Parameters:(dict) (cfg) – See documentation for __init__.

Locking

The locking implementation is available as separte class and can be used as multiprocess lock.

class redicts.Lock(redis_conn, key, expire_timeout=30, acquire_timeout=10)[source]

Implement a distributed, thread-safe lock for redis.

The basics are described here: https://redis.io/topics/transactions This lock is more flexible than other locking implementations, since it supports tree-based locking by specifying a dotted path as key.

If you lock an element higher up in the hierarchy, all elements below it will be automatically locked too. It is still possible to lock elements below though, but those will only add their lock to the lock above.

This implementation uses optimistic locking, i.e. retry if some of the to be locked values changed.

acquire()[source]

Acquire the lock and wait if needed

is_locked()[source]

Return True if this lock was already acquired by someone

release()[source]

Release the lock again

Exceptions

exception redicts.InternalError[source]

Raised when the implementation got confused. This should only happen when somebody else tampers with the locking keys in redis.

exception redicts.LockTimeout[source]

Raised when lock creation fails within the timeout