Memcached 简明教程

Memcached - Prepend Data

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

Memcached prepend command is used to add some data in an existing key. The data is stored before the existing data of the key.

Syntax

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

The basic syntax of Memcached prepend command is as shown below −

prepend key flags exptime bytes [noreply]
value

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

The keywords in the syntax are as described below−

  1. key − It is the name of the key by which data is stored and retrieved in Memcached.

  2. flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved.

  3. exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration.

  4. bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached.

  5. noreply (optional) −It is a parameter that informs the server not send any reply.

  6. value − It is the data that needs to be stored. Data needs to be passed on the new line after executing the command with the above options.

Output

命令的输出如下所示:

The output of the command is as shown below −

STORED
  1. STORED indicates success.

  2. NOT_STORED indicates the key does not exist in the Memcached server.

  3. CLIENT_ERROR indicates error.

Example

在以下示例中,我们在不存在的密钥中添加了一些数据。因此,Memcached 返回 NOT_STORED 。在此之后,我们设置一个密钥并在其中置入数据。

In the following example, we add some data in a key that does not exist. Hence, Memcached returns NOT_STORED. After this, we set one key and prepend data into it.

prepend tutorials 0 900 5
redis
NOT_STORED
set tutorials 0 900 9
memcached
STORED
get tutorials
VALUE tutorials 0 14
memcached
END
prepend tutorials 0 900 5
redis
STORED
get tutorials
VALUE tutorials 0 14
redismemcached
END

Prepend Data Using Java Application

要向 Memcached 服务器中置入数据,你需要使用 Memcached prepend 方法。

To prepend data in a Memcached server, you need to use the Memcached prepend method.

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("Prepend to cache:"+mcc.prepend("tutorialspoint", "redis").isDone());

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

Output

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

On compiling and executing the program, you get to see the following output −

Connection to server successful
set status:true
Get from Cache:memcached
Prepend to cache:true
Get from Cache:redismemcached