Java 连接 Memcached 服务

java 连接 memcached 服务

使用 java 程序连接 memcached,需要在你的 classpath 中添加 memcached jar 包。

本站 jar 包下载地址:spymemcached-2.10.3.jar

google code jar 包下载地址:spymemcached-2.10.3.jar(需要科学上网)。

以下程序假定 memcached 服务的主机为 127.0.0.1,端口为 11211。

 

1. 连接范例

java 连接 memcached

 

memcachedjava.java 文件:

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 successful.");
         
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex){
         system.out.println( ex.getmessage() );
      }
   }
}


该程序中我们使用 inetsocketaddress 连接 ip 为 127.0.0.1 端口 为 11211 的 memcached 服务。

执行以上代码,如果连接成功会输出以下信息:

connection to server successful.

 

2. set 操作范例

以下使用 java.util.concurrent.future 来存储数据

 

memcachedjava.java 文件:

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 successful.");
      
         // 存储数据
         future fo = mcc.set("yapf", 900, "free education");
      
         // 查看存储状态
         system.out.println("set status:" + fo.get());
         
         // 输出值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex){
         system.out.println( ex.getmessage() );
      }
   }
}


执行程序,输出结果为:

connection to server successful.
set status:true
yapf value in cache - free education

 

3. add 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "free education");
 
         // 打印状态
         system.out.println("set status:" + fo.get());
 
         // 输出
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 添加
         fo = mcc.add("yapf", 900, "memcached");
 
         // 打印状态
         system.out.println("add status:" + fo.get());
 
         // 添加新key
         fo = mcc.add("yapf", 900, "all free compilers");
 
         // 打印状态
         system.out.println("add status:" + fo.get());
         
         // 输出
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex){
         system.out.println(ex.getmessage());
      }
   }
}


 

4. replace 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加第一个 key=》value 对
         future fo = mcc.set("yapf", 900, "free education");
 
         // 输出执行 add 方法后的状态
         system.out.println("add status:" + fo.get());
 
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 添加新的 key
         fo = mcc.replace("yapf", 900, "largest tutorials' library");
 
         // 输出执行 set 方法后的状态
         system.out.println("replace status:" + fo.get());
 
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex){
         system.out.println( ex.getmessage() );
      }
   }
}


 

5. append 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "free education");
 
         // 输出执行 set 方法后的状态
         system.out.println("set status:" + fo.get());
 
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 对存在的key进行数据添加操作
         fo = mcc.append("yapf", 900, " for all");
 
         // 输出执行 set 方法后的状态
         system.out.println("append status:" + fo.get());
         
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex) {
         system.out.println(ex.getmessage());
      ]
   }
}


 

6. prepend 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "education for all");
 
         // 输出执行 set 方法后的状态
         system.out.println("set status:" + fo.get());
 
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 对存在的key进行数据添加操作
         fo = mcc.prepend("yapf", 900, "free ");
 
         // 输出执行 set 方法后的状态
         system.out.println("prepend status:" + fo.get());
         
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex) {
         system.out.println(ex.getmessage());
      }
   }
}


 

7. cas 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "free education");
 
         // 输出执行 set 方法后的状态
         system.out.println("set status:" + fo.get());
            
         // 使用 get 方法获取数据
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 通过 gets 方法获取 cas token(令牌)
         casvalue casvalue = mcc.gets("yapf");
 
         // 输出 cas token(令牌) 值
         system.out.println("cas token - " + casvalue);
 
         // 尝试使用cas方法来更新数据
         casresponse casresp = mcc.cas("yapf", casvalue.getcas(), 900, "largest tutorials-library");
         
         // 输出 cas 响应信息
         system.out.println("cas response - " + casresp);
 
         // 输出值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex) {
         system.out.println(ex.getmessage());
      }
   }
}

 

8. get 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "free education");
 
         // 输出执行 set 方法后的状态
         system.out.println("set status:" + fo.get());
 
         // 使用 get 方法获取数据
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex) {
         system.out.println(ex.getmessage());
      }
   }
}


 

9. gets 操作范例、cas

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "free education");
 
         // 输出执行 set 方法后的状态
         system.out.println("set status:" + fo.get());
            
         // 从缓存中获取键为 yapf 的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 通过 gets 方法获取 cas token(令牌)
         casvalue casvalue = mcc.gets("yapf");
 
         // 输出 cas token(令牌) 值
         system.out.println("cas value in cache - " + casvalue);
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex) {
         system.out.println(ex.getmessage());
      }
   }
}


 

10. delete 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数据
         future fo = mcc.set("yapf", 900, "world's largest online tutorials library");
 
         // 输出执行 set 方法后的状态
         system.out.println("set status:" + fo.get());
 
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 对存在的key进行数据添加操作
        fo = mcc.delete("yapf");
 
         // 输出执行 delete 方法后的状态
         system.out.println("delete status:" + fo.get());
 
         // 获取键对应的值
         system.out.println("yapf value in cache - " + mcc.get("yapf"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(exception ex) {
         system.out.println(ex.getmessage());
      }
   }
}


 

11. incr/decr 操作范例

 

memcachedjava.java 文件:

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 successful.");
 
         // 添加数字值
         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());
      }
   }
}
                        
                        

下一节:php 连接 memcached 服务

memcached 教程

相关文章