标签:
需要下载spymemcached-2.10.3.jar,并把这个jar放到java程序的classpath中才能使用memcached。
在下面的程序,假设memcached服务器的主机IP是192.168.1.111,并在端口11211上运行。
/** * set方法 */ public static void set() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11111)); // not set data into memcached server System.out.println("set status:" + mcc.set("hello", 900, "helloworld")); // Get value from cache System.out.println("Get from Cache:" + mcc.get("hello")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * add方法 */ public static void add() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); mcc.add("key", 900, "memcached"); System.out.println(mcc.get("key")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * replace方法 */ public static void replace() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); // 修改存在的键 System.out.println(mcc.get("key")); mcc.replace("key", 900, "mongodb"); System.out.println(mcc.get("key"));// mongodb // 修改不存在的键 mcc.replace("no", 900, "noexit"); System.out.println(mcc.get("no"));// null mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * append */ public static void append() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.get("key")); mcc.append("key", "nosql"); System.out.println(mcc.get("key")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * prepend */ public static void prepend() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.get("key")); mcc.prepend("key", "redis"); System.out.println(mcc.get("key")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
cas即checked and set的意思,只有当最后一个参数和gets所获取的参数匹配时才能存储,否则返回“EXISTS”。
要运行memcached的cas命令,需要从gets命令得到memcached令牌。
/** * cas方法 */ public static void cas() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.get("key")); long casToken = mcc.gets("key").getCas(); System.out.println(casToken); mcc.cas("key", casToken, 900, "mongodb"); System.out.println(mcc.get("key")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * get方法 */ public static void get() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); // not set data into memcached server System.out.println("set status:" + mcc.set("hello", 900, "helloworld")); // Get value from cache System.out.println("Get from Cache:" + mcc.get("hello")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * gets方法 */ public static void gets() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); long casToken = mcc.gets("key").getCas(); System.out.println(casToken); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * delete方法 */ public static void delete() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.get("key")); mcc.delete("key"); System.out.println(mcc.get("key")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Memcached的incr 和 decr命令用于增加现有键的数值递减。如果键未找到或如果关键的不是数字,则返回NOT_FOUND。那么CLIENT_ERROR不能增加或返回递减非数值错误。
/** * incr方法和decr方法 */ public static void incr_decr() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.get("num")); mcc.incr("num", 5); System.out.println(mcc.get("num")); System.out.println("------------------>"); mcc.decr("num", 10); System.out.println(mcc.get("num")); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * stats方法 */ public static void stats() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.getStats()); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * flush_all方法 */ public static void flush_all() { try { MemcachedClient mcc = new MemcachedClient(new InetSocketAddress( "192.168.1.111", 11211)); System.out.println(mcc.flush().isDone()); mcc.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Memcached总结四:用ava程序连接memcached进行操作
标签:
原文地址:http://www.cnblogs.com/longshiyVip/p/4890525.html