Skip to content

Path Cache

Randgalt edited this page Nov 18, 2011 · 10 revisions

Description

A Path Cache is used to watch a ZNode. Whenever a child is added, updated or removed, the Path Cache will change its state to contain the current set of children, the children's data and the children's state.

Participating Classes

  • PathChildrenCache
  • PathChildrenCacheMode
  • PathChildrenCacheListener
  • ChildData

Usage

Creating a PathChildrenCache

public PathChildrenCache(CuratorFramework client,
String path,
PathChildrenCacheMode mode)
Parameters:
client - the client
path - path to watch
mode - caching mode

PathChildrenCacheMode values:

  • CACHE_DATA_AND_STAT - The cache will hold all the children, the data for each child node and the stat for each child node
  • CACHE_DATA - The cache will hold all the children and the data for each child node. ChildData.getStat() will return null.
  • CACHE_PATHS_ONLY - The cache will hold only the children path names. ChildData.getStat() and ChildData.getData() will both return null.

General Usage

The cache must be started by calling start(). Call close() when you are through with the cache.

At any time, call getCurrentData() to get the current state of the cache. You can also register to be notified when a change occurs by calling:

public void addListener(PathChildrenCacheListener listener)
Add a change listener
Parameters:
listener - the listener

Error Handling

PathChildrenCache instances internally monitor a ConnectionStateListener. If a SUSPEND or LOST is received, the cache is reset (the PathChildrenCacheListener will receive a RESET).

Clone this wiki locally