📜  Memcached-连接

📅  最后修改于: 2020-11-27 05:41:19             🧑  作者: Mango


要连接到Memcached服务器,您需要在主机名和端口名上使用telnet命令。

句法

Memcached telnet命令的基本语法如下所示-

$telnet HOST PORT

在这里, HOSTPORT分别是正在执行Memcached服务器的计算机IP和端口号。

以下示例显示了如何连接到Memcached服务器并执行简单的set and get命令。假定Memcached服务器正在主机127.0.0.1和端口11211上运行。

$telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
// now store some data and get it from memcached server
set tutorialspoint 0 900 9
memcached
STORED
get tutorialspoint
VALUE tutorialspoint 0 9
memcached
END

来自Java应用程序的连接

要从Java程序连接Memcached服务器,您需要将Memcached jar添加到您的类路径中,如上一章所示。假定Memcached服务器正在主机127.0.0.1和端口11211上运行。-

import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
   public static void main(String[] args) {
      
      // Connecting to Memcached server on localhost
      MemcachedClient mcc = new MemcachedClient(new
      InetSocketAddress("127.0.0.1", 11211));
      System.out.println("Connection to server sucessfully");
      
      //not set data into memcached server
      System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);
      
      //Get value from cache
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
   }
}

输出

在编译和执行程序时,您将看到以下输出-

Connection to server successfully
set status:true
Get from Cache:memcached.

终端也可能只显示很少的参考消息,这些消息可以忽略。