Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java connects to the Memcached service


May 17, 2021 Memcached



To connect to Memcached using the Java program, you need to add a Memcached jar package to your classpath.

The following procedure assumes that the host of the Memcached service is 127.0.0.1 and the port is 11211.

Connect the instance

Java connection Memcached

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 sucessful.");
         
         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex){
         System.out.println( ex.getMessage() );
      }
   }
}

In this program we use InetSocketAddress to connect IP to a memcached service of 11211 port 127.0.0.1.

By executing the above code, if the connection is successful, the following information is output:

Connection to server successful.

The set action instance

The following uses java.util.concurrent.Future to store data

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 sucessful.");
      
         // 存储数据
         Future fo = mcc.set("w3cschool", 900, "Free Education");
      
         // 查看存储状态
         System.out.println("set status:" + fo.get());
         
         // 输出值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex){
         System.out.println( ex.getMessage() );
      }
   }
}

Execute the program and the output is:

Connection to server successful.
set status:true
W3Cschool value in cache - Free Education

Add action instance

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "Free Education");

         // 打印状态
         System.out.println("set status:" + fo.get());

         // 输出
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 添加
         Future fo = mcc.add("w3cschool", 900, "memcached");

         // 打印状态
         System.out.println("add status:" + fo.get());

         // 添加新key
         fo = mcc.add("codingground", 900, "All Free Compilers");

         // 打印状态
         System.out.println("add status:" + fo.get());
         
         // 输出
         System.out.println("codingground value in cache - " + mcc.get("codingground"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex){
         System.out.println(ex.getMessage());
      }
   }
}

The instance of the replace operation

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 sucessful.");

         // 添加第一个 key=》value 对
         Future fo = mcc.set("w3cschool", 900, "Free Education");

         // 输出执行 add 方法后的状态
         System.out.println("add status:" + fo.get());

         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 添加新的 key
         fo = mcc.replace("w3cschool", 900, "Largest Tutorials' Library");

         // 输出执行 set 方法后的状态
         System.out.println("replace status:" + fo.get());

         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex){
         System.out.println( ex.getMessage() );
      }
   }
}

The instance of the append action

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "Free Education");

         // 输出执行 set 方法后的状态
         System.out.println("set status:" + fo.get());

         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 对存在的key进行数据添加操作
         Future fo = mcc.append("w3cschool", 900, " for All");

         // 输出执行 set 方法后的状态
         System.out.println("append status:" + fo.get());
         
         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("codingground"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Prepend action instance

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "Education for All");

         // 输出执行 set 方法后的状态
         System.out.println("set status:" + fo.get());

         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 对存在的key进行数据添加操作
         Future fo = mcc.prepend("w3cschool", 900, "Free ");

         // 输出执行 set 方法后的状态
         System.out.println("prepend status:" + fo.get());
         
         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("codingground"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Cas operation instance

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "Free Education");

         // 输出执行 set 方法后的状态
         System.out.println("set status:" + fo.get());
            
         // 使用 get 方法获取数据
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 通过 gets 方法获取 CAS token(令牌)
         CASValue casValue = mcc.gets("w3cschool");

         // 输出 CAS token(令牌) 值
         System.out.println("CAS token - " + casValue);

         // 尝试使用cas方法来更新数据
         CASResponse casresp = mcc.cas("w3cschool", casValue.getCas(), 900, "Largest Tutorials-Library");
         
         // 输出 CAS 响应信息
         System.out.println("CAS Response - " + casresp);

         // 输出值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Get action instance

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "Free Education");

         // 输出执行 set 方法后的状态
         System.out.println("set status:" + fo.get());

         // 使用 get 方法获取数据
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Gets operation instance, CAS

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "Free Education");

         // 输出执行 set 方法后的状态
         System.out.println("set status:" + fo.get());
            
         // 从缓存中获取键为 W3Cschool 的值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 通过 gets 方法获取 CAS token(令牌)
         CASValue casValue = mcc.gets("w3cschool");

         // 输出 CAS token(令牌) 值
         System.out.println("CAS value in cache - " + casValue);

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Delete operation instance

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 sucessful.");

         // 添加数据
         Future fo = mcc.set("w3cschool", 900, "World's largest online tutorials library");

         // 输出执行 set 方法后的状态
         System.out.println("set status:" + fo.get());

         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("w3cschool"));

         // 对存在的key进行数据添加操作
         Future fo = mcc.delete("w3cschool");

         // 输出执行 delete 方法后的状态
         System.out.println("delete status:" + fo.get());

         // 获取键对应的值
         System.out.println("w3cschool value in cache - " + mcc.get("codingground"));

         // 关闭连接
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Incr/Decr action instance

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 sucessful.");

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