Java 連接 Memcached 服務(wù)
java 連接 memcached 服務(wù)
使用 java 程序連接 memcached,需要在你的 classpath 中添加 memcached jar 包。
本站 jar 包下載地址:spymemcached-2.10.3.jar。
google code jar 包下載地址:spymemcached-2.10.3.jar(需要科學(xué)上網(wǎng))。
以下程序假定 memcached 服務(wù)的主機為 127.0.0.1,端口為 11211。
1. 連接范例
java 連接 memcached
memcachedjava.java 文件:
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 successful."); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex){ system.out.println( ex.getmessage() ); } } }該程序中我們使用 inetsocketaddress 連接 ip 為 127.0.0.1 端口 為 11211 的 memcached 服務(wù)。
執(zhí)行以上代碼,如果連接成功會輸出以下信息:
connection to server successful.
2. set 操作范例
以下使用 java.util.concurrent.future 來存儲數(shù)據(jù)
memcachedjava.java 文件:
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 successful."); // 存儲數(shù)據(jù) future fo = mcc.set("yapf", 900, "free education"); // 查看存儲狀態(tài) system.out.println("set status:" + fo.get()); // 輸出值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex){ system.out.println( ex.getmessage() ); } } }執(zhí)行程序,輸出結(jié)果為:
connection to server successful. set status:true yapf value in cache - free education
3. add 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "free education"); // 打印狀態(tài) system.out.println("set status:" + fo.get()); // 輸出 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 添加 fo = mcc.add("yapf", 900, "memcached"); // 打印狀態(tài) system.out.println("add status:" + fo.get()); // 添加新key fo = mcc.add("yapf", 900, "all free compilers"); // 打印狀態(tài) system.out.println("add status:" + fo.get()); // 輸出 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex){ system.out.println(ex.getmessage()); } } }
4. replace 操作范例
memcachedjava.java 文件:
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 successful."); // 添加第一個 key=》value 對 future fo = mcc.set("yapf", 900, "free education"); // 輸出執(zhí)行 add 方法后的狀態(tài) system.out.println("add status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 添加新的 key fo = mcc.replace("yapf", 900, "largest tutorials' library"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("replace status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex){ system.out.println( ex.getmessage() ); } } }
5. append 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "free education"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 對存在的key進行數(shù)據(jù)添加操作 fo = mcc.append("yapf", 900, " for all"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("append status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex) { system.out.println(ex.getmessage()); ] } }
6. prepend 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "education for all"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 對存在的key進行數(shù)據(jù)添加操作 fo = mcc.prepend("yapf", 900, "free "); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("prepend status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex) { system.out.println(ex.getmessage()); } } }
7. cas 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "free education"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 使用 get 方法獲取數(shù)據(jù) system.out.println("yapf value in cache - " + mcc.get("yapf")); // 通過 gets 方法獲取 cas token(令牌) casvalue casvalue = mcc.gets("yapf"); // 輸出 cas token(令牌) 值 system.out.println("cas token - " + casvalue); // 嘗試使用cas方法來更新數(shù)據(jù) casresponse casresp = mcc.cas("yapf", casvalue.getcas(), 900, "largest tutorials-library"); // 輸出 cas 響應(yīng)信息 system.out.println("cas response - " + casresp); // 輸出值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex) { system.out.println(ex.getmessage()); } } }
8. get 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "free education"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 使用 get 方法獲取數(shù)據(jù) system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex) { system.out.println(ex.getmessage()); } } }
9. gets 操作范例、cas
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "free education"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 從緩存中獲取鍵為 yapf 的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 通過 gets 方法獲取 cas token(令牌) casvalue casvalue = mcc.gets("yapf"); // 輸出 cas token(令牌) 值 system.out.println("cas value in cache - " + casvalue); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex) { system.out.println(ex.getmessage()); } } }
10. delete 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)據(jù) future fo = mcc.set("yapf", 900, "world's largest online tutorials library"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 對存在的key進行數(shù)據(jù)添加操作 fo = mcc.delete("yapf"); // 輸出執(zhí)行 delete 方法后的狀態(tài) system.out.println("delete status:" + fo.get()); // 獲取鍵對應(yīng)的值 system.out.println("yapf value in cache - " + mcc.get("yapf")); // 關(guān)閉連接 mcc.shutdown(); }catch(exception ex) { system.out.println(ex.getmessage()); } } }
11. incr/decr 操作范例
memcachedjava.java 文件:
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 successful."); // 添加數(shù)字值 future fo = mcc.set("number", 900, "1000"); // 輸出執(zhí)行 set 方法后的狀態(tài) system.out.println("set status:" + fo.get()); // 獲取鍵對應(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()); } } }
相關(guān)文章
- Oracle數(shù)據(jù)庫連接
- Oracle Distinct語句
- Oracle Where子句
- Oracle Or子句
- Oracle SET TRANSACTION語句(設(shè)置事務(wù))
- Oracle 級聯(lián)刪除外鍵
- Oracle Compose()函數(shù)
- Oracle Convert()函數(shù)
- Oracle Initcap()函數(shù)
- Linux Memcached 安裝
- Memcached delete 命令
- DB2服務(wù)器安裝
- DB2數(shù)據(jù)庫
- DB2存儲組
- DB2視圖
- 12類Oracle日期函數(shù)超全面總結(jié)
- PLSQL一些常用知識點梳理總結(jié)
- oracle數(shù)據(jù)庫id自增及生成uuid問題
- 連接Oracle數(shù)據(jù)庫失敗(ORA-12514)故障排除全過程
- 一文教會你配置使用Navicat或PLSQL可視化工具遠程連接Oracle