使用 Java 程序連接 Memcached,需要在你的 classpath 中添加 Memcached jar 包。
以下程序假定 Memcached 服務(wù)的主機(jī)為 127.0.0.1,端口為 11211。
Java 連接 Memcached
import net.spy.memcached.MemcachedClient; import java.net.*; public class MemcachedJava { public static void main(String[] args) { try{ // 本地連接 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
該程序中我們使用 InetSocketAddress 連接 IP 為 127.0.0.1 端口 為 11211 的 memcached 服務(wù)。
執(zhí)行以上代碼,如果連接成功會(huì)輸出以下信息:
Connection to server successful.
以下使用 java.util.concurrent.Future 來(lái)存儲(chǔ)數(shù)據(jù)
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 存儲(chǔ)數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 查看存儲(chǔ)狀態(tài) System.out.println("set status:" + fo.get()); // 輸出值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
執(zhí)行程序,輸出結(jié)果為:
Connection to server successful. set status:true W3Cschool value in cache - Free Education
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 打印狀態(tài) System.out.println("set status:" + fo.get()); // 輸出 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 添加 Future fo = mcc.add("w3cschool", 900, "memcached"); // 打印狀態(tài) System.out.println("add status:" + fo.get()); // 添加新key fo = mcc.add("codingground", 900, "All Free Compilers"); // 打印狀態(tài) System.out.println("add status:" + fo.get()); // 輸出 System.out.println("codingground value in cache - " + mcc.get("codingground")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println(ex.getMessage()); } } }
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try { //連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加第一個(gè) key=》value 對(duì) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 輸出執(zhí)行 add 方法后的狀態(tài) System.out.println("add status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 添加新的 key fo = mcc.replace("w3cschool", 900, "Largest Tutorials' Library"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("replace status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 對(duì)存在的key進(jìn)行數(shù)據(jù)添加操作 Future fo = mcc.append("w3cschool", 900, " for All"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("append status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("codingground")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Education for All"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 對(duì)存在的key進(jìn)行數(shù)據(jù)添加操作 Future fo = mcc.prepend("w3cschool", 900, "Free "); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("prepend status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("codingground")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
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{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 使用 get 方法獲取數(shù)據(jù) System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 通過(guò) gets 方法獲取 CAS token(令牌) CASValue casValue = mcc.gets("w3cschool"); // 輸出 CAS token(令牌) 值 System.out.println("CAS token - " + casValue); // 嘗試使用cas方法來(lái)更新數(shù)據(jù) CASResponse casresp = mcc.cas("w3cschool", casValue.getCas(), 900, "Largest Tutorials-Library"); // 輸出 CAS 響應(yīng)信息 System.out.println("CAS Response - " + casresp); // 輸出值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 使用 get 方法獲取數(shù)據(jù) System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
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{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "Free Education"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 從緩存中獲取鍵為 W3Cschool 的值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 通過(guò) gets 方法獲取 CAS token(令牌) CASValue casValue = mcc.gets("w3cschool"); // 輸出 CAS token(令牌) 值 System.out.println("CAS value in cache - " + casValue); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)據(jù) Future fo = mcc.set("w3cschool", 900, "World's largest online tutorials library"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("w3cschool")); // 對(duì)存在的key進(jìn)行數(shù)據(jù)添加操作 Future fo = mcc.delete("w3cschool"); // 輸出執(zhí)行 delete 方法后的狀態(tài) System.out.println("delete status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 System.out.println("w3cschool value in cache - " + mcc.get("codingground")); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.MemcachedClient; public class MemcachedJava { public static void main(String[] args) { try{ // 連接本地的 Memcached 服務(wù) MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數(shù)字值 Future fo = mcc.set("number", 900, "1000"); // 輸出執(zhí)行 set 方法后的狀態(tài) System.out.println("set status:" + fo.get()); // 獲取鍵對(duì)應(yīng)的值 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)); // 關(guān)閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
更多建議: