Java Connection Memcached__java

Source: Internet
Author: User
Tags cas getmessage memcached numeric value

Using a Java program to connect to Memcached, you need to add Memcached jar packs to your classpath.

Click to download: Spymemcached-2.10.3.jar

The following procedure assumes that the host of the Memcached service is 127.0.0.1 and the port is 11211.


Connection Instance

Java Connection Memcached

Import net.spy.memcached.MemcachedClient;
Import java.net.*;


public class Memcachedjava {public
   static void Main (string[] args) {
      try{
         /local connection Memcached service
         Memcached Client MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));
         System.out.println ("Connection to Server sucessful.");
         
         Close connection
         Mcc.shutdown ();
         
      } catch (Exception ex) {
         System.out.println (ex.getmessage ());}}}

In this program we use Inetsocketaddress to connect IP to the memcached service with a 127.0.0.1 port of 11211.

Execute the above code and output the following information if the connection succeeds:

Connection to server successful.

Set Operation instance

The following uses Java.util.concurrent.Future to store data

Import java.net.InetSocketAddress;
Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient;

public class Memcachedjava {public
   static void Main (string[] args) {
   
      try{
         //connection local Memcached service
         Memcache Dclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));
         System.out.println ("Connection to Server sucessful.");
      
         Storage data
         Future fo = mcc.set ("Runoob", 900, "free education");
      
         View storage status
         System.out.println ("Set Status:" + Fo.get ());
         
         Output value
         System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Close connection
         Mcc.shutdown ();
         
      } catch (Exception ex) {
         System.out.println (ex.getmessage ());}}}

Execute the program, the output result is:

Connection to server successful.
Set Status:true
runoob value in Cache-free education
Add Operation instance

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add data Future fo = Mcc.set ("Runoob", 900, "free education");

         Print status System.out.println ("Set Status:" + Fo.get ());

         Output System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Add Future fo = Mcc.add ("Runoob", 900, "memcached");

         Print status System.out.println ("Add Status:" + Fo.get ());

         Add new key fo = Mcc.add ("Codingground", 900, "all free compilers");
         
         Print status System.out.println ("Add Status:" + Fo.get ()); Output System.out.println ("Codingground value in Cache-" + mcc.get ("Codingground"));
         
      Close connection Mcc.shutdown ();
      }catch (Exception ex) {System.out.println (Ex.getmessage ()); }
   }
}
Replace action instance

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient;
         public class Memcachedjava {public static void main (string[] args) {try {//Connect local Memcached Service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add the first key= "value to Future fo = Mcc.set (" Runoob ", 900," free education ");

         Outputs the state System.out.println after the Add method is executed ("Add Status:" + Fo.get ());

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Add new key fo = Mcc.replace ("Runoob", 900, "largest tutorials ' Library");

         Outputs the state System.out.println after the set method is executed ("Replace status:" + Fo.get ());

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));
       Close connection Mcc.shutdown ();  
      }catch (Exception ex) {System.out.println (Ex.getmessage ()); }
   }
}
Append Action Instance
Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add data Future fo = Mcc.set ("Runoob", 900, "free education");

         The state System.out.println after the output executes the Set method ("Set Status:" + Fo.get ());

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Data addition operations to existing keys Future fo = mcc.append ("Runoob", 900, "for all");
         
         The state System.out.println ("Append Status:" + Fo.get ()) after the output executes the Set method;

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Codingground"));
        Close connection Mcc.shutdown (); 
      }catch (Exception ex) System.out.println (Ex.getmessage ());
 }
}


prepend Action Instance

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add data Future fo = Mcc.set ("Runoob", 900, "Education for All");

         The state System.out.println after the output executes the Set method ("Set Status:" + Fo.get ());

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Data-adding operations to existing keys Future fo = mcc.prepend ("Runoob", 900, "free");
         
         The state System.out.println ("prepend Status:" + Fo.get ()) after the output executes the Set method;

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Codingground"));
      Close connection Mcc.shutdown ();   
      }catch (Exception ex) System.out.println (Ex.getmessage ()); }
}

CAS Action Instances

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;
Import Net.spy.memcached.CASValue;
Import Net.spy.memcached.CASResponse;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add data Future fo = Mcc.set ("Runoob", 900, "free education");
            
         The state System.out.println after the output executes the Set method ("Set Status:" + Fo.get ());

         Use the Get method to obtain the data System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Obtain CAS token (token) Casvalue casvalue = Mcc.gets ("Runoob") by gets method;

         Output CAs token (token) value System.out.println ("CAs token-" + casvalue); Try to update the data using the CAs method casresponse Casresp = Mcc.cas ("RunoOB ", Casvalue.getcas (), 900," largest tutorials-library ");

         Output CAS response information System.out.println ("CAs Response-" + casresp);

         Output value System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));
         
      Close connection Mcc.shutdown ();
   }catch (Exception ex) System.out.println (Ex.getmessage ()); }
}

Get Operation Instance

Import java.net.InetSocketAddress;
Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient;

public class Memcachedjava {public
   static void Main (string[] args) {
   
      try{
   
         //connection local Memcached service
         Memcache Dclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));
         System.out.println ("Connection to Server sucessful.");

         Add Data
         Future fo = mcc.set ("Runoob", 900, "free education");

         The state System.out.println after the output executes the Set method
         ("Set Status:" + Fo.get ());

         Use the Get method to obtain
         the data System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Close connection
         Mcc.shutdown ();
         
      } catch (Exception ex)
         System.out.println (Ex.getmessage ());
   }
}

gets operation instance, CAS

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;
Import Net.spy.memcached.CASValue;
Import Net.spy.memcached.CASResponse;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add data Future fo = Mcc.set ("Runoob", 900, "free education");
            
         The state System.out.println after the output executes the Set method ("Set Status:" + Fo.get ());

         Gets the value of the key to Runoob from the cache System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Obtain CAS token (token) Casvalue casvalue = Mcc.gets ("Runoob") by gets method;

         Output CAs token (token) value System.out.println ("CAS value in Cache-" + casvalue);
         
     Close connection Mcc.shutdown (); }catch (Exception ex) System.out.println (Ex.getmessage ()); }
}

Delete operation instance

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add data Future fo = Mcc.set ("Runoob", 900, "world ' s largest online tutorials library");

         The state System.out.println after the output executes the Set method ("Set Status:" + Fo.get ());

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Runoob"));

         Data-adding operations to existing keys Future fo = mcc.delete ("Runoob");

         The output state System.out.println after the Delete method is executed ("Delete status:" + Fo.get ());

         Gets the value of the key corresponding System.out.println ("Runoob value in Cache-" + mcc.get ("Codingground"));
     Close connection Mcc.shutdown ();    
      }catch (Exception ex) System.out.println (Ex.getmessage ()); }
}

INCR/DECR Action Instance

Import java.net.InetSocketAddress;

Import Java.util.concurrent.Future;

Import net.spy.memcached.MemcachedClient; 
         public class Memcachedjava {public static void main (string[] args) {try{//connection local Memcached service
         Memcachedclient MCC = new Memcachedclient (New Inetsocketaddress ("127.0.0.1", 11211));

         System.out.println ("Connection to Server sucessful.");

         Add numeric value Future fo = Mcc.set ("number", 900, "1000");

         The state System.out.println after the output executes the Set method ("Set Status:" + Fo.get ());

         Gets the value of the key corresponding SYSTEM.OUT.PRINTLN ("value in Cache-" + mcc.get ("number"));

         Self-amplification and output System.out.println ("value in cache after increment-" + MCC.INCR ("number", 111));

         Self-subtraction and output System.out.println ("value in cache after decrement-" + MCC.DECR ("number", 112));
         
      Close connection Mcc.shutdown ();
   }catch (Exception ex) System.out.println (Ex.getmessage ()); }
}


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.