1、创建项目,导入Jar包
2、直接上代码,注释很详细(SET操作)
package com.jiayuan.memcacheTest;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
public class MemcacheTest {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
try{
// 本地连接 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessful.");
// 存储数据
Future fo = mcc.set("Wenhy", 900, "Free Education");
// 查看存储状态
System.out.println("set status:" + fo.get());
// 输出值
System.out.println("wenhy value in cache - " + mcc.get("Wenhy"));
// 关闭连接
mcc.shutdown();
}catch(Exception ex){
System.out.println( ex.getMessage() );
}
}
}
3、Add操作
@SuppressWarnings("rawtypes")
private static void addMemcache(MemcachedClient mcc) throws Exception {
// 添加数据
Future fo = mcc.set("Wenhy", 900, "Free Education");
// 打印状态
<