Java Connection Memcached Service

Source: Internet
Author: User
Tags cas

Install Memcached http://www.runoob.com/memcached/window-install-memcached.html under Windows

Connect to the Memcached service by using the Telnet command and specifying the host IP and port.

Telnet HOST PORT
127.0.  0.111211  

Add Memcached jar Package

<dependency>    <groupId>net.spy</groupId>    <artifactid>spymemcached</ Artifactid>    <version>2.9.1</version></dependency>

The following procedure assumes that the host for the Memcached service is 127.0.0.1 and the port is 11211. If it is remote access, remember to add the port in the firewall's inbound rules.

public class Memcachedjava {public static void main (string[] args) {try {//Connect local Memcached service memcachedclient MCC = new Me Mcachedclient (New Inetsocketaddress ("127.0.0.1", 11211)); System.out.println ("Connection to Server sucessful."); Future fo = Mcc.set ("memcached", Ten, "memcached");//Add Data System.out.println ("Set Status:" + Fo.get ());// The output executes the set method after the state System.out.println ("get-" + mcc.get ("memcached"));//Use the Get method to get the data fo = Mcc.add ("memcached", Ten, "add");// Add System.out.println ("Add Status:" + Fo.get ()),//output state System.out.println After the Add method is executed ("add-" + mcc.get ("memcached");// Gets the value of the key corresponding to FO = Mcc.replace ("memcached", Ten, "memcached replace"); SYSTEM.OUT.PRINTLN ("Replace status:" + Fo.get ()),//output state System.out.println After the Replace method is executed ("replace-" + mcc.get (" Memcached "));//Gets the value of the key corresponding to FO = Mcc.append (" memcached "," append ");//Adds data to the existing key System.out.println (" Append Status: "+ Fo.get ());//outputs the state System.out.println after the Append method is executed ("append-" + mcc.get ("memcached"));//Gets the value of the key corresponding to FO = Mcc.prepend (" Memcached "," PrepenD ");//Add data to the existing key operation System.out.println (" prepend Status: "+ Fo.get ());//Output The state after the Prepend method is executed System.out.println (" Prepend-"+ mcc.get (" memcached "));//Get the value of the key corresponding to Casvalue Casvalue = Mcc.gets (" memcached ");//Get CAS token (token) by the Gets method syste M.OUT.PRINTLN ("CAs token-" + casvalue);//output CAS token (token) value casresponse Casresp = Mcc.cas ("memcached", Casvalue.getcas () , "CAS");//try using the CAs method to update the data System.out.println ("CAs Response-" + casresp);//output CAS response information System.out.println ("CAS-" + MCC . Get ("memcached"));//output value fo = Mcc.delete ("memcached");//Data add operation to existing key System.out.println ("Delete status:" + Fo.get ( ));//output executes the state System.out.println after the Delete method ("Delete-" + mcc.get ("memcached"));//Gets the value corresponding to the future number = Mcc.set , 10, "1000");//Add numeric value System.out.println ("Set Status:" + Number.get ());//output state System.out.println After the set method is executed ("incr-" + MC C.INCR ("number", 100));//self-increment and output System.out.println ("decr-" + MCC.DECR ("number", 101));//self-decrement and output//close connection Mcc.shutdown ();} catch (Exception ex) {System.out.println (ex.gEtmessage ());}}} 

 

The set command is used to store value (the data value) in the specified key (key). If the set key already exists, the command can update the original data corresponding to the key, that is, the role of the update.

The add command is used to store value (data value) in the specified key (key). If the add key already exists, the data will not be updated (the expired key will be updated), and the previous value will remain the same.

The Replace command is used to replace the value (data value) of the existing key (key). If the key does not exist, the substitution fails.

The append command is used to append data to the value (data value) that already exists key (key).

The prepend command is used to append data to the value (data value) that already exists key (key).

The CAS (check-and-set or Compare-and-swap) command is used to perform a "check and set" operation it can write values only after the current client has the last value, and the value corresponding to the key is not modified by another client.


The GET command gets the value (data value) stored in the key, or null if the key does not exist.

The GET command gets the value (data value) that is stored with the CAS token and returns null if the key does not exist.

The delete command is used to delete the existing key (key).

The incr and DECR commands are used to self-increment or decrement the numeric value of an existing key (key). The data that the INCR and the DECR command operates on must be a decimal 32-bit unsigned integer. If key does not exist return Not_found, if the value of the key is not a number, then Client_error is returned, and other errors return error.

The stats command is used to return statistics such as PID (process number), version number, number of connections, and so on.
The Stats Items command displays the number of item in each slab and the length of storage (the number of seconds the last access distance is now).
The stats Slabs command is used to display information about each slab, including the size, number, usage, etc. of the chunk.
The stats sizes command is used to display the size and number of all item. This information returns two columns, the first column is the size of item, and the second column is the number of item.
The Flush_all command cleans up all key=>value (key = = value) pairs in the cache. This command provides an optional parameter time, which is used to perform a clean-up cache operation after a set period.

 

Transfer from http://www.runoob.com/memcached/java-memcached.html

Java Connection Memcached Service

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.