Memcached - 預置資料



Memcached 的 **prepend** 命令用於在現有鍵中新增一些資料。資料儲存在鍵的現有資料之前。

語法

Memcached **prepend** 命令的基本語法如下所示:

prepend key flags exptime bytes [noreply]
value

語法中的關鍵字描述如下:

  • **key** - 它是儲存和檢索 Memcached 中資料的鍵的名稱。

  • **flags** - 它是伺服器與使用者提供的資料一起儲存的 32 位無符號整數,並在檢索專案時與資料一起返回。

  • **exptime** - 以秒為單位的過期時間。0 表示沒有延遲。如果 exptime 超過 30 天,Memcached 將其用作 UNIX 時間戳以進行過期。

  • **bytes** - 需要儲存的資料塊中的位元組數。這是需要儲存在 Memcached 中的資料的長度。

  • **noreply(可選)** - 它是一個引數,通知伺服器不要傳送任何回覆。

  • **value** - 需要儲存的資料。在使用上述選項執行命令後,需要在新行上傳遞資料。

輸出

命令的輸出如下所示:

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("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"));
   }
}

輸出

編譯並執行程式後,您將看到以下輸出:

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

© . All rights reserved.