kazoo.recipe.cache

TreeCache

Maintainer:Jiangge Zhang <tonyseek@gmail.com>
Maintainer:Haochuan Guo <guohaochuan@gmail.com>
Maintainer:Tianwen Zhang <mail2tevin@gmail.com>
Status:Alpha

A port of the Apache Curator’s TreeCache recipe. It builds an in-memory cache of a subtree in ZooKeeper and keeps it up-to-date.

See also: http://curator.apache.org/curator-recipes/tree-cache.html

Public API

class kazoo.recipe.cache.TreeCache(client, path)[source]

The cache of a ZooKeeper subtree.

Parameters:
  • client – A KazooClient instance.
  • path – The root path of subtree.
start()[source]

Starts the cache.

The cache is not started automatically. You must call this method.

After a cache started, all changes of subtree will be synchronized from the ZooKeeper server. Events will be fired for those activity.

See also listen().

Note

This method is not thread safe.

close()[source]

Closes the cache.

A closed cache was detached from ZooKeeper’s changes. And all nodes will be invalidated.

Once a tree cache was closed, it could not be started again. You should only close a tree cache while you want to recycle it.

Note

This method is not thread safe.

listen(listener)[source]

Registers a function to listen the cache events.

The cache events are changes of local data. They are delivered from watching notifications in ZooKeeper session.

This method can be use as a decorator.

Parameters:listener – A callable object which accepting a TreeEvent instance as its argument.
listen_fault(listener)[source]

Registers a function to listen the exceptions.

It is possible to meet some exceptions during the cache running. You could specific handlers for them.

This method can be use as a decorator.

Parameters:listener – A callable object which accepting an exception as its argument.
get_data(path, default=None)[source]

Gets data of a node from cache.

Parameters:
  • path – The absolute path string.
  • default – The default value which will be returned if the node does not exist.
Raises ValueError:
 

If the path is outside of this subtree.

Returns:

A NodeData instance.

get_children(path, default=None)[source]

Gets node children list from in-memory snapshot.

Parameters:
  • path – The absolute path string.
  • default – The default value which will be returned if the node does not exist.
Raises ValueError:
 

If the path is outside of this subtree.

Returns:

The frozenset which including children names.

class kazoo.recipe.cache.TreeEvent[source]

Bases: tuple

The immutable event tuple of cache.

event_data

A NodeData instance.

event_type

An enumerate integer to indicate event type.

classmethod make(event_type, event_data)[source]

Creates a new TreeEvent tuple.

Returns:A TreeEvent instance.
class kazoo.recipe.cache.NodeData[source]

Bases: tuple

The immutable node data tuple of cache.

data

The bytes data of current node.

classmethod make(path, data, stat)[source]

Creates a new NodeData tuple.

Returns:A NodeData instance.
path

The absolute path string of current node.

stat

The stat information of current node.