Memcached Summary four: Using the AVA program to connect the memcached operation

Source: Internet
Author: User
Tags cas memcached memcached stats

1. Memcached's Java environment settings

You need to download Spymemcached-2.10.3.jar and put this jar in the Java program's classpath to use memcached.

In the following program, assume that the host IP of the memcached server is 192.168.1.111 and run on port 11211.

1. memcached Add data (Set method )
/*** Set Method*/     Public Static voidset () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11111)); //Not set data into memcached serverSystem.out.println ("Set Status:" + mcc.set ("Hello", "HelloWorld")); //Get value from cacheSystem.out.println ("Get from Cache:" + mcc.get ("Hello"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code2. Memcached Add data (addmethod )
/*** Add Method*/     Public Static voidAdd () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); Mcc.add ("Key", "memcached"); System.out.println (Mcc.get ("Key"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code3. memcached Replace/replace data (replace method )
/*** Replace Method*/     Public Static voidreplace () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); //Modify the existing keySystem.out.println (Mcc.get ("Key")); Mcc.replace ("Key", "the", "MongoDB"); System.out.println (Mcc.get ("Key"));//MongoDB//modify keys that do not existMcc.replace ("No", "no", "Noexit"); System.out.println (Mcc.get ("No"));//NULLMcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code4. Memcached Append/append Method (APPend method )
/*** Append*/     Public Static voidappend () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); System.out.println (Mcc.get ("Key")); Mcc.append ("Key", "NoSQL"); System.out.println (Mcc.get ("Key"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code5. Memcached Pre-Add (prepend method )
/*** prepend*/     Public Static voidprepend () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); System.out.println (Mcc.get ("Key")); Mcc.prepend ("Key", "Redis"); System.out.println (Mcc.get ("Key"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code6. Memcached CAS command (CAs method )

CAS is the meaning of checked and set, which can only be stored if the last parameter matches the parameters obtained by the GET, otherwise "EXISTS" is returned.

To run the CAS command for memcached, you need to get the memcached token from the gets command.

/*** CAs method*/     Public Static voidCAs () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); System.out.println (Mcc.get ("Key")); LongCastoken = Mcc.gets ("Key"). Getcas ();            System.out.println (Castoken); Mcc.cas ("Key", Castoken,, "MongoDB"); System.out.println (Mcc.get ("Key"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code7. memcached get/get data (get method )
/*** Get Method*/     Public Static voidget () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); //Not set data into memcached serverSystem.out.println ("Set Status:" + mcc.set ("Hello", "HelloWorld")); //Get value from cacheSystem.out.println ("Get from Cache:" + mcc.get ("Hello"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code8. Memcached gets command (gets method)
/*** Gets Method*/     Public Static voidgets () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); LongCastoken = Mcc.gets ("Key"). Getcas ();            System.out.println (Castoken);        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code9. memcached Delete/delete data (Delete method)
/*** Delete Method*/     Public Static voidDelete () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); System.out.println (Mcc.get ("Key")); Mcc.delete ("Key"); System.out.println (Mcc.get ("Key"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Codememcached Increment Decrement data (incr method and Decr method)

The memcached incr and DECR commands are used to increase the numeric decrement of an existing key. If the key is not found or if the key is not a number, the Not_found is returned. Then client_error cannot increment or return a decrement non-numeric error.

/*** Incr method and Decr method*/     Public Static voidincr_decr () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); System.out.println (Mcc.get ("Num")); MCC.INCR ("Num", 5); System.out.println (Mcc.get ("Num")); System.out.println ("------------------>"); MCC.DECR ("Num", 10); System.out.println (Mcc.get ("Num"));        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View CodeMemcached stats Command (stats command)
    /*** Stats Method*/     Public Static voidstats () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211));            System.out.println (Mcc.getstats ());        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Codememcached Purging Data (Flush method)
/*** Flush_all Method*/     Public Static voidFlush_all () {Try{memcachedclient MCC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211));            System.out.println (Mcc.flush (). IsDone ());        Mcc.shutdown (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
View Code

Memcached Summary four: Using the AVA program to connect the memcached operation

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.