//创建MemcachedClient 连接memcached服务器端
MemcachedClient mClient = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("连接memcached成功..........");
//存储数据,key-ruo,value-888,时间是1000秒
Future future = mClient.set("ruo", 1000, "888");
System.out.println("查看ruo的存储状态:"+future.get()+",存储的值:"+mClient.get("ruo"));
//添加数据,如果key已经存储,则不会更新之前的value,返回NOT_STORED
Future f1 = mClient.add("ruo", 1000, "999");
System.out.println("查看添加ruo的存储状态:"+f1.get()+",添加后key:ruo的值是"+mClient.get("ruo"));
//替换replace
Future f2 = mClient.replace("ruo", 1000, "999");
System.out.println("查看替换ruo的存储状态:"+f2.get()+",替换后key:ruo的值是"+mClient.get("ruo"));
//追加数据append,用来向已存在的key,追加数据
Future f3 = mClient.append("ruo","666");
System.out.println("查看替换ruo的存储状态:"+f3.get()+",替换后key:ruo的值是"+mClient.get("ruo"));
//prepend,用来向已存在的key对应的value前,追加数据
Future f4 = mClient.prepend("ruo", "555");
System.out.println("查看替换ruo的存储状态:"+f4.get()+",替换后key:ruo的值是"+mClient.get("ruo"));
//清空数据库
mClient.flush();
//关闭连接
mClient.shutdown();
运行结果
===========================================
2018-10-30 11:23:07.350 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@38a22ea7
查看ruo的存储状态:true,存储的值:888
查看添加ruo的存储状态:false,添加后key:ruo的值是888
查看替换ruo的存储状态:true,替换后key:ruo的值是999
查看替换ruo的存储状态:true,替换后key:ruo的值是999666
查看替换ruo的存储状态:true,替换后key:ruo的值是555999666
2018-10-30 11:23:07.368 INFO net.spy.memcached.MemcachedConnection: Shut down memcached client