全部产品
Search
文档中心

:Java: Spymemcache

更新时间:Aug 08, 2023

客户端

客户端介绍请参见spymemcached简介

客户端下载地址请参见下载spymemcached

Java代码示例

  1. 准备Java开发环境。登录已有的阿里云ECS服务器,在上面安装Java JDK和常用的IDE(比如 Eclipse)。

    Java JDK: 下载地址

    Eclipse:(下载地址1下载地址2

  2. 第一个代码示例如下,把里面的Java代码复制到Eclipse Project里面去。

    说明

    注意:此时的代码是编译不成功的,因为要想调用Memcache缓存服务还需要一个第三方提供的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";//控制台上的“实例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中存一个key为"ocs"的数据,便于后面验证读取数据
                             String key = "ocs";
                             String value = "Open Cache Service,  from www.Aliyun.com";
                             int expireTime = 1000; // 过期时间,单位s; 从写入时刻开始计时,超过expireTime s后,该数据过期失效,无法再读出;
                             OperationFuture<Boolean> future = cache.set(key, expireTime, value);
                             future.get();  // spymemcached set()是异步的,future.get() 等待cache.set()操作结束,也可以不等待,用户根据自己需求选择
                             //向OCS中存若干个数据,随后可以在OCS控制台监控上看到统计信息
                             for(int i=0;i<100;i++){
                                    key="key-"+i;
                                    value="value-"+i;
    
                                    //执行set操作,向缓存中存数据
                                    expireTime = 1000; // 过期时间,单位s
                                    future = cache.set(key, expireTime, value);
                                    future.get();  //  确保之前(cache.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<Boolean> future = cache.set(key, expireTime, value);
             future.get();
             //向OCS中存若干个数据,随后可以在OCS控制台监控上看到统计信息
             for (int i = 0; i < 100; i++) {
                 key = "key-" + i;
                 value = "value-" + i;
    
                 //执行set操作,向缓存中存数据
                 expireTime = 1000; // 过期时间,单位s
                 future = cache.set(key, expireTime, value);
                 future.get();
             }
    
             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
     }
  3. 在Eclipse里面打开的OcsSample1.java,根据自己的实例信息修改几个地方。

    每个人买到的云数据库Memcache实例的ID都是不重复的,其对应的阿里云内网地址也是独一无二的,这些信息都在云数据库Memcache控制台上显示出来。在同自己的云数据库Memcache实例建立连接的时候,需要根据这些信息修改OcsSample1.java中的对应地方。

  4. 信息修改完毕,可以运行自己的程序了。运行main函数,会在Eclipse下面的console窗口看到下面这样的结果(请忽略可能出现的红色INFO调试信息)。

     OCS Sample Code
     Set操作完成!
     Get操作: Open Cache Service,  from www.Aliyun.com