Connecting to Memcached Service with Java
To connect to Memcached using a Java program, you need to add the Memcached JAR file to your classpath.
Download link for the JAR file from this site: spymemcached-2.10.3.jar.
Download link for the JAR file from Google Code: spymemcached-2.10.3.jar (requires a VPN).
The following program assumes that the Memcached service is hosted on 127.0.0.1 with port 11211.
Connection Example
Connecting to Memcached with Java
MemcachedJava.java File:
import net.spy.memcached.MemcachedClient;
import java.net.*;
public class MemcachedJava {
public static void main(String[] args) {
try {
// Connect to Memcached service locally
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
// Close the connection
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
In this program, we use InetSocketAddress to connect to the Memcached service at IP 127.0.0.1 and port 11211.
Running the above code will output the following if the connection is successful:
Connection to server successful.
Set Operation Example
The following uses 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("tutorialpro", 900, "Free Education");
// Check storage status
System.out.println("set status:" + fo.get());
// Output the value
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the connection
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Running the program will output:
Connection to server successful.
set status:true
tutorialpro 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("tutorialpro", 900, "Free Education");
// Print status
System.out.println("set status:" + fo.get());
// Output
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Add
fo = mcc.add("tutorialpro", 900, "memcached");
// Print status
System.out.println("add status:" + fo.get());
// Add new key
fo = mcc.add("tutorialpro", 900, "All Free Compilers");
// Print status
System.out.println("add status:" + fo.get());
// Output
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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("tutorialpro", 900, "Free Education");
// Output the status after adding
System.out.println("add status:" + fo.get());
// Get the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Add a new key
fo = mcc.replace("tutorialpro", 900, "Largest Tutorials' Library");
// Output the status after setting
System.out.println("replace status:" + fo.get());
// Get the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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("tutorialpro", 900, "Free Education");
// Output the status after setting
System.out.println("set status:" + fo.get());
// Get the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Perform data append operation on an existing key
fo = mcc.append("tutorialpro", 900, " for All");
// Output the status after the set method execution
System.out.println("append status:" + fo.get());
// Retrieve the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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("tutorialpro", 900, "Education for All");
// Output the status after the set method execution
System.out.println("set status:" + fo.get());
// Retrieve the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Perform data prepend operation on an existing key
fo = mcc.prepend("tutorialpro", 900, "Free ");
// Output the status after the set method execution
System.out.println("prepend status:" + fo.get());
// Retrieve the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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("tutorialpro", 900, "Free Education");
// Output the status after the set method execution
System.out.println("set status:" + fo.get());
// Retrieve the value using the get method
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Obtain the CAS token using the gets method
CASValue casValue = mcc.gets("tutorialpro");
// Output the CAS token value
System.out.println("CAS token - " + casValue);
// Attempt to update data using the cas method
CASResponse casresp = mcc.cas("tutorialpro", casValue.getCas(), 900, "Largest Tutorials-Library");
// Output the CAS response information
System.out.println("CAS Response - " + casresp);
// Output the value
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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("tutorialpro", 900, "Free Education");
// Output the status after the set method execution
System.out.println("set status:" + fo.get());
// Retrieve the value using the get method
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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("tutorialpro", 900, "Free Education");
// Output the status after the set method execution
System.out.println("set status:" + fo.get());
// Retrieve the value from the cache for the key tutorialpro
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Obtain the CAS token using the gets method
CASValue casValue = mcc.gets("tutorialpro");
// Output the CAS token value
System.out.println("CAS value in cache - " + casValue);
// Close the connection
mcc.shutdown();
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
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("tutorialpro", 900, "World's largest online tutorials library");
// Print the status after executing the set method
System.out.println("set status:" + fo.get());
// Retrieve the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Perform data deletion operation on an existing key
fo = mcc.delete("tutorialpro");
// Print the status after executing the delete method
System.out.println("delete status:" + fo.get());
// Retrieve the value corresponding to the key
System.out.println("tutorialpro value in cache - " + mcc.get("tutorialpro"));
// Close the 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 numeric value
Future fo = mcc.set("number", 900, "1000");
// Print the status after executing the set method
System.out.println("set status:" + fo.get());
// Retrieve the value corresponding to the key
System.out.println("value in cache - " + mcc.get("number"));
// Increment and print
System.out.println("value in cache after increment - " + mcc.incr("number", 111));
// Decrement and print
System.out.println("value in cache after decrement - " + mcc.decr("number", 112));
// Close the connection
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}