Memcached - Prepend Data

Memcached prepend 命令用于在现有密钥中添加一些数据.数据存储在密钥的现有数据之前.

语法

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

prepend key flags exptime bytes [noreply]
value

语法中的关键字如下所述 :

  • key : 它是在
    Memcached中存储和检索数据的密钥的名称.

  • flags : 它是服务器存储的32位无符号整数和用户提供的数据,并在检索项目时返回数据.

  • exptime : 它是以秒为单位的到期时间. 0表示没有延迟.如果exptime超过30天,则Memcached将其用作UNIX时间戳到期.

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

  • noreply(可选) : 这是一个参数通知服务器不发送任何回复.

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

输出

命令的输出如下所示 :

STORED

  • STORED 表示成功.

  • NOT_STORED 表示Memcached服务器中不存在密钥.

  • CLIENT_ERROR 表示错误.

示例

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

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

使用Java应用程序预先添加数据

要在Memcached服务器中预先添加数据,您需要使用Memcached prepend 方法.

示例

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("it1352", 900, "memcached").isDone());
      
      // Get value from cache
      System.out.println("Get from Cache:"+mcc.get("it1352"));
      
      // now append some data into existing key
      System.out.println("Prepend to cache:"+mcc.prepend("it1352", "redis").isDone());
      
      // get the updated key
      System.out.println("Get from Cache:"+mcc.get("it1352"));
   }
}

输出

在编译和执行程序时,你会到达请参阅以下输出 :

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