Java 连接 Memcached 服务
使用 Java 程序连接 Memcached,需要在你的 classpath 中添加 Memcached jar 包。
本站 jar 包下载地址:spymemcached-2.10.3.jar。
Google Code jar 包下载地址:spymemcached-2.10.3.jar(需要科学上网)。
以下程序假定 Memcached 服务的主机为 127.0.0.1,端口为 11211。
连接实例
Java 连接 Memcached
MemcachedJava.java 文件:
import net.spy.memcached.MemcachedClient;
import java.net.*;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
mcc.shutdown();
}catch(Exception ex){
System.out.println( ex.getMessage() );
}
}
}
该程序中我们使用 InetSocketAddress 连接 IP 为 127.0.0.1 端口 为 11211 的 memcached 服务。
执行以上代码,如果连接成功会输出以下信息:
Connection to server successful.
set 操作实例
以下使用 java.util.concurrent.Future 来存储数据
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex){
System.out.println( ex.getMessage() );
}
}
}
执行程序,输出结果为:
Connection to server successful.
set status:true
runoob value in cache - Free Education
add 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
fo = mcc.add("runoob", 900, "memcached");
System.out.println("add status:" + fo.get());
fo = mcc.add("runoob", 900, "All Free Compilers");
System.out.println("add status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
replace 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("add status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
fo = mcc.replace("runoob", 900, "Largest Tutorials' Library");
System.out.println("replace status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex){
System.out.println( ex.getMessage() );
}
}
}
append 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
fo = mcc.append("runoob", 900, " for All");
System.out.println("append status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
]
}
}
prepend 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Education for All");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
fo = mcc.prepend("runoob", 900, "Free ");
System.out.println("prepend status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
CAS 操作实例
MemcachedJava.java 文件:
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{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
CASValue casValue = mcc.gets("runoob");
System.out.println("CAS token - " + casValue);
CASResponse casresp = mcc.cas("runoob", casValue.getCas(), 900, "Largest Tutorials-Library");
System.out.println("CAS Response - " + casresp);
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
get 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
gets 操作实例、CAS
MemcachedJava.java 文件:
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{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "Free Education");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
CASValue casValue = mcc.gets("runoob");
System.out.println("CAS value in cache - " + casValue);
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
delete 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("runoob", 900, "World's largest online tutorials library");
System.out.println("set status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
fo = mcc.delete("runoob");
System.out.println("delete status:" + fo.get());
System.out.println("runoob value in cache - " + mcc.get("runoob"));
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Incr/Decr 操作实例
MemcachedJava.java 文件:
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
try{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server successful.");
Future fo = mcc.set("number", 900, "1000");
System.out.println("set status:" + fo.get());
System.out.println("value in cache - " + mcc.get("number"));
System.out.println("value in cache after increment - " + mcc.incr("number", 111));
System.out.println("value in cache after decrement - " + mcc.decr("number", 112));
mcc.shutdown();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}