Memcached 简明教程

Memcached - Get Data

Memcached get 命令用于获取存储在密钥上的值。如果密钥不存在于 Memcached 中,则它不会返回任何内容。

Syntax

Memcached get 命令的基本语法如下所示 −

get key

Example

在以下示例中,我们使用 tutorialspoint 作为密钥,并在其中存储 memcached,其过期时间为 900 秒。

set tutorialspoint 0 900 9
memcached
STORED
get tutorialspoint
VALUE tutorialspoint 0 9
memcached
END

Get Data Using Java Application

要从 Memcached 服务器获取数据,你需要使用 Memcached get 方法。

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");
      System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);

     // Get value from cache
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
   }
}

Output

编译并执行该程序后,您可以看到以下输出:

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