转自阿里云 云数据库 Memcache 版 https://help.aliyun.com/document_detail/26545.html
第一步,登录已有的阿里云ECS服务器,在上面安装Java JDK和常用的IDE(比如Eclipse);
(注意:只有在阿里云的ECS服务器上,才能通过内网访问云数据库Memcache实例。所以,用家里或是公司的电脑直接执行下面的代码示例是看不到结果的)
Java JDK下载地址
Eclipse(下载地址1,下载地址2)
第二步,在把Java开发环境准备好了之后,第一个代码示例如下,把里面的Java代码复制到Eclipse Project里面去。此时的代码是编译不成功的,因为要想调用OCS缓存服务还需要一个第三方提供的jar包下载地址。添加这个jar包之后,代码就能编译通过了。
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";//控制台上的“内网地址”
final String port ="11211"; //默认端口 11211,不用改
final String username = "xxxxxxxxx";//控制台上的“访问账号”
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中存一个key为"ocs"的数据,便于后面验证读取数据
String key = "ocs";
String value = "Open Cache Service, from www.Aliyun.com";
int expireTime = 1000; // 过期时间,单位s; 从写入时刻开始计时,超过expireTime s后,该数据过期失效,无法再读出;
OperationFuture future = cache.set(key, expireTime, value);
//向OCS中存若干个数据,随后可以在OCS控制台监控上看到统计信息
for(int i=0;i<100;i++){
String key="key-"+i;
String value="value-"+i;
//执行set操作,向缓存中存数据
int expireTime = 1000; // 过期时间,单位s
cache.set(key, expireTime, value);
future.get(); // 确保之前(mc.set())操作已经结束
}
System.out.println("Set操作完成!");
//执行get操作,从缓存中读数据,读取key为"ocs"的数据
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"; //控制台上的“内网地址”
final String port = "11211"; //默认端口 11211,不用改
MemcachedClient cache = null;
try {
cache = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(host + ":" + port));
System.out.println("OCS Sample Code");
//向OCS中存一个key为"ocs"的数据,便于后面验证读取数据
String key = "ocs";
String value = "Open Cache Service, from www.Aliyun.com";
int expireTime = 1000; // 过期时间,单位s; 从写入时刻开始计时,超过expireTime s后,该数据过期失效,无法再读出;
OperationFuture future = cache.set(key, expireTime, value);
//向OCS中存若干个数据,随后可以在OCS控制台监控上看到统计信息
for (int i = 0; i < 100; i++) {
String key = "key-" + i;
String value = "value-" + i;
//执行set操作,向缓存中存数据
int expireTime = 1000; // 过期时间,单位s
cache.set(key, expireTime, value);
}
System.out.println("Set操作完成!");
future.get(); // 确保之前(mc.set())操作已经结束
//执行get操作,从缓存中读数据,读取key为"ocs"的数据
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里面打开的OcsSample1.java,根据自己的OCS实例信息修改几个地方。 每个人买到的云数据库Memcache实例的ID都是不重复的,其对应的阿里云内网地址也是独一无二的,这些信息都在云数据库Memcache控制台上显示出来。在同自己的云数据库Memcache实例建立连接的时候,需要根据这些信息修改OcsSample1.java中的对应地方,如以上代码中红色部分所示。 第四步,信息修改完毕,可以运行自己的程序了。运行main函数,会在Eclipse下面的console窗口看到下面这样的结果(请忽略可能出现的红色INFO调试信息)
OCS Sample Code
Set操作完成!
Get操作: Open Cache Service, from www.Aliyun.com
第一步,登录已有的阿里云ECS服务器,在上面安装Java JDK和常用的IDE(比如Eclipse);
(注意:只有在阿里云的ECS服务器上,才能通过内网访问云数据库Memcache实例。所以,用家里或是公司的电脑直接执行下面的代码示例是看不到结果的)
Java JDK下载地址
Eclipse(下载地址1,下载地址2)
第二步,在把Java开发环境准备好了之后,第一个代码示例如下,把里面的Java代码复制到Eclipse Project里面去。此时的代码是编译不成功的,因为要想调用OCS缓存服务还需要一个第三方提供的jar包下载地址。添加这个jar包之后,代码就能编译通过了。
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";//控制台上的“内网地址”
final String port ="11211"; //默认端口 11211,不用改
final String username = "xxxxxxxxx";//控制台上的“访问账号”
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中存一个key为"ocs"的数据,便于后面验证读取数据
String key = "ocs";
String value = "Open Cache Service, from www.Aliyun.com";
int expireTime = 1000; // 过期时间,单位s; 从写入时刻开始计时,超过expireTime s后,该数据过期失效,无法再读出;
OperationFuture future = cache.set(key, expireTime, value);
//向OCS中存若干个数据,随后可以在OCS控制台监控上看到统计信息
for(int i=0;i<100;i++){
String key="key-"+i;
String value="value-"+i;
//执行set操作,向缓存中存数据
int expireTime = 1000; // 过期时间,单位s
cache.set(key, expireTime, value);
future.get(); // 确保之前(mc.set())操作已经结束
}
System.out.println("Set操作完成!");
//执行get操作,从缓存中读数据,读取key为"ocs"的数据
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"; //控制台上的“内网地址”
final String port = "11211"; //默认端口 11211,不用改
MemcachedClient cache = null;
try {
cache = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(host + ":" + port));
System.out.println("OCS Sample Code");
//向OCS中存一个key为"ocs"的数据,便于后面验证读取数据
String key = "ocs";
String value = "Open Cache Service, from www.Aliyun.com";
int expireTime = 1000; // 过期时间,单位s; 从写入时刻开始计时,超过expireTime s后,该数据过期失效,无法再读出;
OperationFuture future = cache.set(key, expireTime, value);
//向OCS中存若干个数据,随后可以在OCS控制台监控上看到统计信息
for (int i = 0; i < 100; i++) {
String key = "key-" + i;
String value = "value-" + i;
//执行set操作,向缓存中存数据
int expireTime = 1000; // 过期时间,单位s
cache.set(key, expireTime, value);
}
System.out.println("Set操作完成!");
future.get(); // 确保之前(mc.set())操作已经结束
//执行get操作,从缓存中读数据,读取key为"ocs"的数据
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里面打开的OcsSample1.java,根据自己的OCS实例信息修改几个地方。 每个人买到的云数据库Memcache实例的ID都是不重复的,其对应的阿里云内网地址也是独一无二的,这些信息都在云数据库Memcache控制台上显示出来。在同自己的云数据库Memcache实例建立连接的时候,需要根据这些信息修改OcsSample1.java中的对应地方,如以上代码中红色部分所示。 第四步,信息修改完毕,可以运行自己的程序了。运行main函数,会在Eclipse下面的console窗口看到下面这样的结果(请忽略可能出现的红色INFO调试信息)
OCS Sample Code
Set操作完成!
Get操作: Open Cache Service, from www.Aliyun.com