Java menyambung ke perkhidmatan Memcached
Untuk menggunakan program Java untuk menyambung ke Memcached, anda perlu menambah pakej balang Memcached pada laluan kelas anda.
Program berikut menganggap bahawa hos perkhidmatan Memcached ialah 127.0.0.1 dan portnya ialah 11211.
Contoh sambungan
Sambungan Java ke 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() ); } } }
Dalam program ini, kami menggunakan InetSocketAddress untuk menyambung ke perkhidmatan memcached dengan IP 127.0.0.1 dan port 11211.
Laksanakan kod di atas jika sambungan berjaya, maklumat berikut akan dikeluarkan:
Connection to server successful.
tetapkan contoh operasi
Yang berikut menggunakan java.util.concurrent.Future. untuk menyimpan data
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("php", 900, "Free Education"); // 查看存储状态 System.out.println("set status:" + fo.get()); // 输出值 System.out.println("php value in cache - " + mcc.get("php")); // 关闭连接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
Laksanakan atur cara dan hasil output ialah:
Connection to server successful. set status:true php value in cache - Free Education
tambah instance operasi
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("php", 900, "Free Education"); // 打印状态 System.out.println("set status:" + fo.get()); // 输出 System.out.println("php value in cache - " + mcc.get("php")); // 添加 Future fo = mcc.add("php", 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()); } } }
ganti instance operasi
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("php", 900, "Free Education"); // 输出执行 add 方法后的状态 System.out.println("add status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("php")); // 添加新的 key fo = mcc.replace("php", 900, "Largest Tutorials' Library"); // 输出执行 set 方法后的状态 System.out.println("replace status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("php")); // 关闭连接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
tambah instance operasi
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("php", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("php")); // 对存在的key进行数据添加操作 Future fo = mcc.append("php", 900, " for All"); // 输出执行 set 方法后的状态 System.out.println("append status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("codingground")); // 关闭连接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
prepend operasi instance
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("php", 900, "Education for All"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("php")); // 对存在的key进行数据添加操作 Future fo = mcc.prepend("php", 900, "Free "); // 输出执行 set 方法后的状态 System.out.println("prepend status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("codingground")); // 关闭连接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
CAS operation instance
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("php", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 使用 get 方法获取数据 System.out.println("php value in cache - " + mcc.get("php")); // 通过 gets 方法获取 CAS token(令牌) CASValue casValue = mcc.gets("php"); // 输出 CAS token(令牌) 值 System.out.println("CAS token - " + casValue); // 尝试使用cas方法来更新数据 CASResponse casresp = mcc.cas("php", casValue.getCas(), 900, "Largest Tutorials-Library"); // 输出 CAS 响应信息 System.out.println("CAS Response - " + casresp); // 输出值 System.out.println("php value in cache - " + mcc.get("php")); // 关闭连接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
dapatkan instance operasi
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("php", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 使用 get 方法获取数据 System.out.println("php value in cache - " + mcc.get("php")); // 关闭连接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
mendapatkan instance operasi, CAS
rreee operationrreee operation<🎜 instance
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("php", 900, "Free Education"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 从缓存中获取键为 php 的值 System.out.println("php value in cache - " + mcc.get("php")); // 通过 gets 方法获取 CAS token(令牌) CASValue casValue = mcc.gets("php"); // 输出 CAS token(令牌) 值 System.out.println("CAS value in cache - " + casValue); // 关闭连接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
Contoh operasi 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("php", 900, "World's largest online tutorials library"); // 输出执行 set 方法后的状态 System.out.println("set status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("php")); // 对存在的key进行数据添加操作 Future fo = mcc.delete("php"); // 输出执行 delete 方法后的状态 System.out.println("delete status:" + fo.get()); // 获取键对应的值 System.out.println("php value in cache - " + mcc.get("codingground")); // 关闭连接 mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }