Java: Spymemcache
客戶端
客戶端介紹請(qǐng)參見(jiàn)spymemcached簡(jiǎn)介。
客戶端下載地址請(qǐng)參見(jiàn)下載spymemcached。
Java代碼示例
準(zhǔn)備Java開(kāi)發(fā)環(huán)境。登錄已有的阿里云ECS服務(wù)器,在上面安裝Java JDK和常用的IDE(比如 Eclipse)。
Java JDK: 下載地址
第一個(gè)代碼示例如下,把里面的Java代碼復(fù)制到Eclipse Project里面去。
說(shuō)明注意:此時(shí)的代碼是編譯不成功的,因?yàn)橐胝{(diào)用Memcache緩存服務(wù)還需要一個(gè)第三方提供的jar 包下載地址。添加這個(gè)jar包之后,代碼就能編譯通過(guò)了。
OcsSample1.java代碼示例 (需要用戶名和密碼)
import java.io.IOException; import java.util.concurrent.ExecutionException; import net.spy.memcached.AddrUtil; import net.spy.memcached.ConnectionFactoryBuilder; import net.spy.memcached.ConnectionFactoryBuilder.Protocol; import net.spy.memcached.MemcachedClient; import net.spy.memcached.auth.AuthDescriptor; import net.spy.memcached.auth.PlainCallbackHandler; import net.spy.memcached.internal.OperationFuture; public class OcsSample1 { public static void main(String[] args) { final String host = "xxxxxxxx.m.yyyyyyyyyy.ocs.aliyuncs.com";//控制臺(tái)上的“內(nèi)網(wǎng)地址” final String port ="11211"; //默認(rèn)端口 11211,不用改 final String username = "xxxxxxxxx";//控制臺(tái)上的“實(shí)例ID”,新版ocs的username可以置空 final String password = "my_password";//郵件中提供的“密碼” MemcachedClient cache = null; try { AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password)); cache = new MemcachedClient( new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY) .setAuthDescriptor(ad) .build(), AddrUtil.getAddresses(host + ":" + port)); System.out.println("OCS Sample Code"); //向OCS中存一個(gè)key為"ocs"的數(shù)據(jù),便于后面驗(yàn)證讀取數(shù)據(jù) String key = "ocs"; String value = "Open Cache Service, from www.Aliyun.com"; int expireTime = 1000; // 過(guò)期時(shí)間,單位s; 從寫(xiě)入時(shí)刻開(kāi)始計(jì)時(shí),超過(guò)expireTime s后,該數(shù)據(jù)過(guò)期失效,無(wú)法再讀出; OperationFuture<Boolean> future = cache.set(key, expireTime, value); future.get(); // spymemcached set()是異步的,future.get() 等待cache.set()操作結(jié)束,也可以不等待,用戶根據(jù)自己需求選擇 //向OCS中存若干個(gè)數(shù)據(jù),隨后可以在OCS控制臺(tái)監(jiān)控上看到統(tǒng)計(jì)信息 for(int i=0;i<100;i++){ key="key-"+i; value="value-"+i; //執(zhí)行set操作,向緩存中存數(shù)據(jù) expireTime = 1000; // 過(guò)期時(shí)間,單位s future = cache.set(key, expireTime, value); future.get(); // 確保之前(cache.set())操作已經(jīng)結(jié)束 } System.out.println("Set操作完成!"); //執(zhí)行g(shù)et操作,從緩存中讀數(shù)據(jù),讀取key為"ocs"的數(shù)據(jù) System.out.println("Get操作:"+cache.get(key)); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } if (cache != null) { cache.shutdown(); } }//eof }
OcsSample2.java代碼示例(不需要用戶名和密碼)
import java.io.IOException; import java.util.concurrent.ExecutionException; import net.spy.memcached.AddrUtil; import net.spy.memcached.BinaryConnectionFactory; import net.spy.memcached.MemcachedClient; import net.spy.memcached.internal.OperationFuture; public class OcsSample2 { public static void main(String[] args) { final String host = "xxxxxxxx.m.yyyyyyyyyy.ocs.aliyuncs.com"; //控制臺(tái)上的“內(nèi)網(wǎng)地址” final String port = "11211"; //默認(rèn)端口 11211,不用改 MemcachedClient cache = null; try { cache = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(host + ":" + port)); System.out.println("OCS Sample Code"); //向OCS中存一個(gè)key為"ocs"的數(shù)據(jù),便于后面驗(yàn)證讀取數(shù)據(jù) String key = "ocs"; String value = "Open Cache Service, from www.Aliyun.com"; int expireTime = 1000; // 過(guò)期時(shí)間,單位s; 從寫(xiě)入時(shí)刻開(kāi)始計(jì)時(shí),超過(guò) expireTime s后,該數(shù)據(jù)過(guò)期失效,無(wú)法再讀出; OperationFuture<Boolean> future = cache.set(key, expireTime, value); future.get(); //向OCS中存若干個(gè)數(shù)據(jù),隨后可以在OCS控制臺(tái)監(jiān)控上看到統(tǒng)計(jì)信息 for (int i = 0; i < 100; i++) { key = "key-" + i; value = "value-" + i; //執(zhí)行set操作,向緩存中存數(shù)據(jù) expireTime = 1000; // 過(guò)期時(shí)間,單位s future = cache.set(key, expireTime, value); future.get(); } System.out.println("Set操作完成!"); //執(zhí)行g(shù)et操作,從緩存中讀數(shù)據(jù),讀取key為"ocs"的數(shù)據(jù) System.out.println("Get操作:" + cache.get(key)); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } if (cache != null) { cache.shutdown(); } }//eof }
在Eclipse里面打開(kāi)的OcsSample1.java,根據(jù)自己的實(shí)例信息修改幾個(gè)地方。
每個(gè)人買到的云數(shù)據(jù)庫(kù)Memcache實(shí)例的ID都是不重復(fù)的,其對(duì)應(yīng)的阿里云內(nèi)網(wǎng)地址也是獨(dú)一無(wú)二的,這些信息都在云數(shù)據(jù)庫(kù)Memcache控制臺(tái)上顯示出來(lái)。在同自己的云數(shù)據(jù)庫(kù)Memcache實(shí)例建立連接的時(shí)候,需要根據(jù)這些信息修改OcsSample1.java中的對(duì)應(yīng)地方。
信息修改完畢,可以運(yùn)行自己的程序了。運(yùn)行main函數(shù),會(huì)在Eclipse下面的console窗口看到下面這樣的結(jié)果(請(qǐng)忽略可能出現(xiàn)的紅色I(xiàn)NFO調(diào)試信息)。
OCS Sample Code Set操作完成! Get操作: Open Cache Service, from www.Aliyun.com