Curator provides three types of caching methods: Path Cache,node cache and tree cache.
Path Cache
the Path cache is used to monitor a znode child node . When a child node is added, updated, deleted, the Path cache changes its state, containing the latest child nodes, the data and state of the child nodes . That, as its name indicates, monitors the path.
There are four classes involved in actual use:
Create the path Cache by using the following constructor:
Public Pathchildrencache (curatorframework client, String path, Boolean cachedata)
To use the cache, you must call its start
method without having to call the close
method later. Start has two, one of which can be passed into the StartMode to set the warm-up mode (warm) for the initial cache:
NORMAL: Empty at initial time.
Build_initial_cache: Called before this method returns rebuild()
.
Post_initialized_event: Sends a pathchildrencacheevent.type#initialized event after the cache initializes the data
public void addListener(PathChildrenCacheListener listener)
You can increase the listener listener cache changes.
getCurrentData()
The List<childdata> method returns an object that can traverse all child nodes.
This example, taken from the official example, implements a console-style operation cache. It provides three commands that you can enter in the console.
Set is used to add or update the value of a child node, that is, to update a cache object
Remove is to delete a cached object
List lists all cached objects
also provides a command to help
help
Client.setdata (). Forpath (path, bytes), Client.create (). creatingparentsifneeded (). Forpath (path, bytes); Client.delete (). Forpath (path);
The query cache uses the following method:
For (Childdata Data:cache.getCurrentData ()) {System.out.println (Data.getpath () + "=" + New String (Data.getdata ())) ;}
Node Cache
Node cache is used to monitor a znode. When data is modified or deleted, node cache can update its state to include the latest changes.
The following three classes are involved:
Nodecache
Nodecachelistener
Childdata
If you want to use the cache, you still need to invoke its start
method, without having to call the close
method later.
getCurrentData()
The current state of the node is obtained, and the current value can be obtained by its state. can use
public void addListener(NodeCacheListener listener)
Monitor the change of node state.
private static void addlistener (Final nodecache cache) { // a pathchildrencachelistener is optional . here, it ' s used just to log // changes nodecachelistener listener = new nodecachelistener () { @ override public void NodeChanged () throws Exception { if (Cache.getcurrentdata () != null) System.out.println ("node changed: " + cache.getcurrentdata (). GetPath () + ", value: " + new string ( Cache.getcurrentdata (). GetData ()); } }; Cache.getlistenable (). AddListener (listener); }
Tree Node
Monitoring the child nodes of the current node and node
The following four classes are involved.
Treecache
Treecachelistener
Treecacheevent
Childdata
private static void addlistener (Final treecache cache) { treecachelistener listener = new treecachelistener () { @Override public void childevent (CuratorFramework client, treecacheevent event) throws Exception { switch (Event.gettype ()) { case node_added: { system.out.println ("treenode added: " + zkpaths.getnodefrompath (Event.getData ( ). GetPath ()) + ", value: " + new string ( Event.getdata (). GetData ()); break; } case NODE_UPDATED: { system.out.println ("TreeNode changed: " + zkpaths.getnodefrompath (Event.getdata (). GetPath ()) + ", value: " &nBsp; + new string (Event.getdata (). GetData ())); break; } case NODE_REMOVED: { System.out.println ("treenode removed: " + zkpaths.getnodefrompath (Event.getdata (). GetPath ( )); break; } default: System.out.println ("other event: " + event.gettype (). Name ()); } } }; cache.getlistenable (). AddListener (listener); }
Zookeeper-cache (curator)