Memcached 简明教程
Memcached - Connection
要连接到 Memcached 服务器,您需要在 HOST 和 PORT 名称上使用 telnet 命令。
To connect to a Memcached server, you need to use the telnet command on HOST and PORT names.
Syntax
Memcached telnet 命令的基本语法如下所示:
The basic syntax of Memcached telnet command is as shown below −
$telnet HOST PORT
此处 HOST 和 PORT 分别是 Memcached 服务器正在执行的机器的 IP 和端口号。
Here, HOST and PORT are machine IP and port number respectively, on which the Memcached server is executing.
Example
以下示例演示如何连接到 Memcached 服务器并执行一个简单的设定和获取命令。假设 Memcached 服务器正在主机 127.0.0.1 和端口 11211 上运行。
The following example shows how to connect to a Memcached server and execute a simple set and get command. Assume that the Memcached server is running on host 127.0.0.1 and port 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
Connection from Java Application
要从您的 java 程序连接 Memcached 服务器,您需要将 Memcached jar 添加到您的类路径中,如前一章所示。假设 Memcached 服务器正在主机 127.0.0.1 和端口 11211 上运行。-
To connect the Memcached server from your java program, you need to add the Memcached jar into your classpath as shown in the previous chapter. Assume that the Memcached server is running on host 127.0.0.1 and port 11211. −
Example
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"));
}
}