<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 連接 Memcached 服務 使用 Java 程序連接 Memcached,需要在你的 classpath 中添加 Memcached jar 包。 本站 jar 包下載地址:[spymemcached-2.10.3.jar](/try/download/spymemcached-2.10.3.jar)。Google Code jar 包下載地址:[spymemcached-2.10.3.jar](http://code.google.com/p/spymemcached/downloads/list)(需要翻墻)。 以下程序假定 Memcached 服務的主機為 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 關閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } } ``` 該程序中我們使用 InetSocketAddress 連接 IP 為 127.0.0.1 端口 為 11211 的 memcached 服務。 執行以上代碼,如果連接成功會輸出以下信息: ``` Connection to server successful. ``` ### set 操作實例 以下使用 java.util.concurrent.Future 來存儲數據 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 存儲數據 Future fo = mcc.set("runoob", 900, "Free Education"); // 查看存儲狀態 System.out.println("set status:" + fo.get()); // 輸出值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 關閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } } ``` 執行程序,輸出結果為: ``` Connection to server successful. set status:true runoob value in cache - Free Education ``` ### add 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "Free Education"); // 打印狀態 System.out.println("set status:" + fo.get()); // 輸出 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 添加 Future fo = mcc.add("runoob", 900, "memcached"); // 打印狀態 System.out.println("add status:" + fo.get()); // 添加新key fo = mcc.add("codingground", 900, "All Free Compilers"); // 打印狀態 System.out.println("add status:" + fo.get()); // 輸出 System.out.println("codingground value in cache - " + mcc.get("codingground")); // 關閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println(ex.getMessage()); } } } ``` ### replace 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加第一個 key=》value 對 Future fo = mcc.set("runoob", 900, "Free Education"); // 輸出執行 add 方法后的狀態 System.out.println("add status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 添加新的 key fo = mcc.replace("runoob", 900, "Largest Tutorials' Library"); // 輸出執行 set 方法后的狀態 System.out.println("replace status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 關閉連接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } } ``` ### append 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "Free Education"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 對存在的key進行數據添加操作 Future fo = mcc.append("runoob", 900, " for All"); // 輸出執行 set 方法后的狀態 System.out.println("append status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("codingground")); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ``` ### prepend 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "Education for All"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 對存在的key進行數據添加操作 Future fo = mcc.prepend("runoob", 900, "Free "); // 輸出執行 set 方法后的狀態 System.out.println("prepend status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("codingground")); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ``` ### CAS 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "Free Education"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 使用 get 方法獲取數據 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 通過 gets 方法獲取 CAS token(令牌) CASValue casValue = mcc.gets("runoob"); // 輸出 CAS token(令牌) 值 System.out.println("CAS token - " + casValue); // 嘗試使用cas方法來更新數據 CASResponse casresp = mcc.cas("runoob", casValue.getCas(), 900, "Largest Tutorials-Library"); // 輸出 CAS 響應信息 System.out.println("CAS Response - " + casresp); // 輸出值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ``` ### get 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "Free Education"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 使用 get 方法獲取數據 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ``` ### gets 操作實例、CAS ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "Free Education"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 從緩存中獲取鍵為 runoob 的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 通過 gets 方法獲取 CAS token(令牌) CASValue casValue = mcc.gets("runoob"); // 輸出 CAS token(令牌) 值 System.out.println("CAS value in cache - " + casValue); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ``` ### delete 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數據 Future fo = mcc.set("runoob", 900, "World's largest online tutorials library"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("runoob")); // 對存在的key進行數據添加操作 Future fo = mcc.delete("runoob"); // 輸出執行 delete 方法后的狀態 System.out.println("delete status:" + fo.get()); // 獲取鍵對應的值 System.out.println("runoob value in cache - " + mcc.get("codingground")); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ``` ### Incr/Decr 操作實例 ``` 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 服務 MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // 添加數字值 Future fo = mcc.set("number", 900, "1000"); // 輸出執行 set 方法后的狀態 System.out.println("set status:" + fo.get()); // 獲取鍵對應的值 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)); // 關閉連接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看