Java Connect to Memcached Service

To connect to Memcached using a Java program, you need to add the Memcached jar package to your classpath.

Download address of the jar package from this site:spymemcached-2.10.3.jar。

Google Code jar package download address:spymemcached-2.10.3.jar(Requires VPN).

The following program assumes the Memcached server host is 127.0.0.1 and the port is 11211.

Connection Example

Java Connecting to Memcached

MemcachedJava.java file:

import net.spy.memcached.MemcachedClient; import java.net.*; public class MemcachedJava { public static void main(String[] args) { try{ //Connect to local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Close connection mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }

In this program, we use InetSocketAddress to connect to the Memcached service with IP 127.0.0.1 and port 11211.

Run the above code; if the connection is successful, the following information will be output:

Connection to server successful.

set Operation Example

Below we use java.util.concurrent.Future to store data.

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Store data Future fo = mcc.set("example", 900, "Free Education"); //Check storage status System.out.println("set status:" + fo.get()); //Output value System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }

Run the program, the output result is:

Connection to server successful.
set status:true
example value in cache - Free Education

add Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "Free Education"); //Print status System.out.println("set status:" + fo.get()); //Output System.out.println("example value in cache - " + mcc.get("example")); //Add fo = mcc.add("example", 900, "memcached"); //Print status System.out.println("add status:" + fo.get()); //Add new key fo = mcc.add("example", 900, "All Free Compilers"); //Print status System.out.println("add status:" + fo.get()); //Output System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex){ System.out.println(ex.getMessage()); } } }

replace Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add the first key=>value pair Future fo = mcc.set("example", 900, "Free Education"); //Output the status after executing the add method System.out.println("add status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Add a new key fo = mcc.replace("example", 900, "Largest Tutorials' Library"); //Output the status after executing the set method System.out.println("replace status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }

append Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "Free Education"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Perform data append operation on the existing key fo = mcc.append("example", 900, " for All"); //Output the status after executing the set method System.out.println("append status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex) { System.out.println(ex.getMessage()); ] } }

prepend Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "Education for All"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Perform data prepend operation on the existing key fo = mcc.prepend("example", 900, "Free "); //Output the status after executing the set method System.out.println("prepend status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex) { System.out.println(ex.getMessage()); } } }

CAS Operation Example

MemcachedJava.java file:

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{ //Connect to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "Free Education"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Use the get method to retrieve data System.out.println("example value in cache - " + mcc.get("example")); //Use the gets method to obtain the CAS token (token) CASValue casValue = mcc.gets("example"); //Output the CAS token value System.out.println("CAS token - " + casValue); //Try to update data using the cas method CASResponse casresp = mcc.cas("example", casValue.getCas(), 900, "Largest Tutorials-Library"); //Output CAS response information System.out.println("CAS Response - " + casresp); //Output value System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex) { System.out.println(ex.getMessage()); } } }

get Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "Free Education"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Use the get method to retrieve data System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex) { System.out.println(ex.getMessage()); } } }

gets Operation Example, CAS

MemcachedJava.java file:

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{ //Connect to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "Free Education"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Get the value with key 'example' from the cache System.out.println("example value in cache - " + mcc.get("example")); //Use the gets method to obtain the CAS token (token) CASValue casValue = mcc.gets("example"); //Output the CAS token value System.out.println("CAS value in cache - " + casValue); //Close connection mcc.shutdown(); }catch(Exception ex) { System.out.println(ex.getMessage()); } } }

delete Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add data Future fo = mcc.set("example", 900, "World's largest online tutorials library"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Perform data append operation on the existing key fo = mcc.delete("example"); //Output the status after executing the delete method System.out.println("delete status:" + fo.get()); //Get the value corresponding to the key System.out.println("example value in cache - " + mcc.get("example")); //Close connection mcc.shutdown(); }catch(Exception ex) { System.out.println(ex.getMessage()); } } }

Incr/Decr Operation Example

MemcachedJava.java file:

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 to the local Memcached service MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server successful."); //Add a numeric value Future fo = mcc.set("number", 900, "1000"); //Output the status after executing the set method System.out.println("set status:" + fo.get()); //Get the value corresponding to the key System.out.println("value in cache - " + mcc.get("number")); //Increment and output System.out.println("value in cache after increment - " + mcc.incr("number", 111)); //Decrement 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()); } } }
Other Extensions