Java 连接 Memcached 服务(下)

2021-07-09 14:30:33 浏览数 (2)

replace 操作实例

代码语言:javascript复制
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("runoob", 900, "Free Education");
 
         // 输出执行 add 方法后的状态
         System.out.println("add status:"   fo.get());
 
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 添加新的 key
         fo = mcc.replace("runoob", 900, "Largest Tutorials' Library");
 
         // 输出执行 set 方法后的状态
         System.out.println("replace status:"   fo.get());
 
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex){
         System.out.println( ex.getMessage() );
      }
   }
}

append 操作实例

代码语言:javascript复制
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("runoob", 900, "Free Education");
 
         // 输出执行 set 方法后的状态
         System.out.println("set status:"   fo.get());
 
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 对存在的key进行数据添加操作
         fo = mcc.append("runoob", 900, " for All");
 
         // 输出执行 set 方法后的状态
         System.out.println("append status:"   fo.get());
         
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex) {
         System.out.println(ex.getMessage());
      ]
   }
}

prepend 操作实例

代码语言:javascript复制
mport 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("runoob", 900, "Education for All");
 
         // 输出执行 set 方法后的状态
         System.out.println("set status:"   fo.get());
 
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 对存在的key进行数据添加操作
         fo = mcc.prepend("runoob", 900, "Free ");
 
         // 输出执行 set 方法后的状态
         System.out.println("prepend status:"   fo.get());
         
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex) {
         System.out.println(ex.getMessage());
      }
   }
}

get 操作实例

代码语言:javascript复制
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("runoob", 900, "Free Education");
 
         // 输出执行 set 方法后的状态
         System.out.println("set status:"   fo.get());
 
         // 使用 get 方法获取数据
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex) {
         System.out.println(ex.getMessage());
      }
   }
}

delete 操作实例

代码语言:javascript复制
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("runoob", 900, "World's largest online tutorials library");
 
         // 输出执行 set 方法后的状态
         System.out.println("set status:"   fo.get());
 
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 对存在的key进行数据添加操作
        fo = mcc.delete("runoob");
 
         // 输出执行 delete 方法后的状态
         System.out.println("delete status:"   fo.get());
 
         // 获取键对应的值
         System.out.println("runoob value in cache - "   mcc.get("runoob"));
 
         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex) {
         System.out.println(ex.getMessage());
      }
   }
}

0 人点赞