Memcached 简明教程

Memcached - Append Data

Memcached append 命令用于在现有键中添加一些数据。将在键的现有数据后存储数据。

Syntax

Memcached append 的基本语法如下所示 −

append key flags exptime bytes [noreply]
value

语法中的关键字的说明如下−

  1. key :这是数据在 Memcached 中存储和从中检索的键的名称。

  2. flags :它是一个 32 位无符号整数,服务器与用户提供的数据一起存储该整数,并在检索该项时与该数据一同返回。

  3. exptime − 是以秒计的过期时间。0 表示无延迟。如果 exptime 超过 30 天,则 Memcached 会将其用作过期的 UNIX 时间戳。

  4. bytes :它是需要存储的数据块中的字节数。这是需要存储在 Memcached 中的数据长度。

  5. noreply (optional) − 是通知服务器不发送任何回复的参数。

  6. value :它是需要存储的数据。在使用上述选项执行命令后,数据需要在新行上传递。

Output

命令的输出如下所示:

STORED
  1. STORED indicates success.

  2. NOT_STORED 表示键在 Memcached 服务器中不存在。

  3. CLIENT_ERROR indicates error.

Example

在以下示例中,我们尝试向一个不存在的键添加一些数据。因此,Memcached 返回 NOT_STORED 。在此之后,我们设置一个键并将数据追加到其中。

append tutorials 0 900 5
redis
NOT_STORED
set tutorials 0 900 9
memcached
STORED
get tutorials
VALUE tutorials 0 14
memcached
END
append tutorials 0 900 5
redis
STORED
get tutorials
VALUE tutorials 0 14
memcachedredis
END

Append Data Using Java Application

若要将数据追加到 Memcached 服务器,您需要使用 Memcached append 方法。

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

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

      // now append some data into existing key
      System.out.println("Append to cache:"+mcc.append("tutorialspoint", "redis").isDone());

      // get the updated key
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
   }
}

Output

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

Connection to server successful
set status:true
Get from Cache:memcached
Append to cache:true
Get from Cache:memcachedredis