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"); // 打印状态 System.out.println("set status:" + fo.get()); // 输出 System.out.println("wenhy value in cache - " + mcc.get("Wenhy")); // 添加 Future fo1 = mcc.add("Wenhy", 900, "memcached"); // 打印状态 System.out.println("add status:" + fo1.get()); // 添加新key Future fo2 = mcc.add("Wenhy123", 900, "All Free Compilers"); // 打印状态 System.out.println("add status:" + fo2.get()); // 输出 System.out.println("Wenhy123 value in cache - " + mcc.get("Wenhy123")); // 关闭连接 mcc.shutdown(); }
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."); //SET操作 //MemcacheTest.setMemcache(mcc); //Add操作 MemcacheTest.addMemcache(mcc); }catch(Exception ex){ System.out.println( ex.getMessage() ); } }
4、replace 操作实例
@SuppressWarnings("rawtypes") private static void replaceMemcache(MemcachedClient mcc) throws Exception { // 添加第一个 key=》value 对 Future fo = mcc.set("Wenhy", 900, "Free Education"); // 输出执行 add 方法后的状态 System.out.println("add status:" + fo.get()); // 获取键对应的值 System.out.println("Wenhy value in cache - " + mcc.get("Wenhy")); // 添加新的 key fo = mcc.replace("Wenhy", 900, "Largest Tutorials' Library"); // 输出执行 replace 方法后的状态 System.out.println("replace status:" + fo.get()); // 获取键对应的值 System.out.println("Wenhy value in cache - " + mcc.get("Wenhy")); // 关闭连接 mcc.shutdown(); }
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."); //SET操作 //MemcacheTest.setMemcache(mcc); //Add操作 //MemcacheTest.addMemcache(mcc); //replace 操作实例 MemcacheTest.replaceMemcache(mcc); }catch(Exception ex){ System.out.println( ex.getMessage() ); } }
5、append 操作实例
@SuppressWarnings("rawtypes") private static void appendMemcache(MemcachedClient mcc) throws Exception { // 添加数据 Future fo = mcc.set("Wenhy", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("Wenhy value in cache - " + mcc.get("Wenhy")); // 对存在的key进行数据添加操作 Future fo1 = mcc.append(900, "Wenhy", " for All"); // 输出执行 set 方法后的状态 System.out.println("append status:" + fo1.get()); // 获取键对应的值 System.out.println("Wenhy value in cache - " + mcc.get("Wenhy")); // 关闭连接 mcc.shutdown(); } 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."); //SET操作 //MemcacheTest.setMemcache(mcc); //Add操作 //MemcacheTest.addMemcache(mcc); //replace 操作实例 //MemcacheTest.replaceMemcache(mcc); //append 操作实例 MemcacheTest.appendMemcache(mcc); }catch(Exception ex){ System.out.println( ex.getMessage() ); } }
结果:
1 set status:true
2 Wenhy value in cache - Free Education
3 append status:true
4 Wenhy value in cache - Free Education for All
备注: prepend 操作实例不做介绍,和append相反,在前面追加
7、delete 操作实例
@SuppressWarnings("rawtypes") private static void deleteMemcache(MemcachedClient mcc) throws Exception { // 添加数据 Future fo = mcc.set("Wenhy", 900, "World's largest online tutorials library"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("Wenhy value in cache - " + mcc.get("Wenhy")); // 对存在的key进行数据添加操作 Future fo1 = mcc.delete("Wenhy"); // 输出执行 delete 方法后的状态 System.out.println("delete status:" + fo1.get()); // 获取键对应的值 System.out.println("Wenhy value in cache - " + mcc.get("Wenhy")); // 关闭连接 mcc.shutdown(); }
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."); //SET操作 //MemcacheTest.setMemcache(mcc); //Add操作 //MemcacheTest.addMemcache(mcc); //replace 操作实例 //MemcacheTest.replaceMemcache(mcc); //append 操作实例 //MemcacheTest.appendMemcache(mcc); //delete操作 MemcacheTest.deleteMemcache(mcc); }catch(Exception ex){ System.out.println( ex.getMessage() ); } }
结果:
1 set status:true 2 Wenhy value in cache - World's largest online tutorials library 3 delete status:true 4 Wenhy value in cache - null
总结:以上就是Memcache与java之间调用的全部内容,Java 连接 Memcached 服务比较简单,主要熟悉使用增加、追加、替换、删除等操作。