Zookeeper-cache (curator)

Source: Internet
Author: User

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:

    • Pathchildrencache

    • Pathchildrencacheevent

    • Pathchildrencachelistener

    • Childdata

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:

    1. NORMAL: Empty at initial time.

    2. Build_initial_cache: Called before this method returns rebuild() .

    3. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.