Memcached - 刪除鍵



Memcached delete 命令用於從 Memcached 伺服器刪除現有鍵。

語法

Memcached delete 命令的基本語法如下所示 −

delete key [noreply]

輸出

CAS 命令可能會產生以下結果之一 −

  • DELETED 表示成功刪除。

  • ERROR 表示刪除資料時出錯或語法錯誤。

  • NOT_FOUND 表示該鍵在 Memcached 伺服器中不存在。

示例

在此示例中,我們將 tutorialspoint 用作鍵,並在其中儲存 memcached,其過期時間為 900 秒。之後,它會刪除儲存的鍵。

set tutorialspoint 0 900 9
memcached
STORED
get tutorialspoint
VALUE tutorialspoint 0 9
memcached
END
delete tutorialspoint
DELETED
get tutorialspoint
END
delete tutorialspoint
NOT_FOUND

使用 Java 應用程式刪除資料

要從 Memcached 伺服器刪除資料,你需要使用 Memcached delete 方法。

示例

import java.net.InetSocketAddress;
import java.util.concurrent.Future;

import net.spy.memcached.MemcachedClient;

public class MemcachedJava {
   public static void main(String[] args) {
   
      try{
   
         // Connecting to Memcached server on localhost
         MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
         System.out.println("Connection to server sucessful.");

         // add data to memcached server
         Future fo = mcc.set("tutorialspoint", 900, "World's largest online tutorials library");

         // print status of set method
         System.out.println("set status:" + fo.get());

         // retrieve and check the value from cache
         System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint"));

         // try to add data with existing key
         Future fo = mcc.delete("tutorialspoint");

         // print status of delete method
         System.out.println("delete status:" + fo.get());

         // retrieve and check the value from cache
         System.out.println("tutorialspoint value in cache - " + mcc.get("codingground"));

         // Shutdowns the memcached client
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

輸出

在編譯和執行程式時,你會看到以下輸出 −

Connection to server successful
set status:true
tutorialspoint value in cache - World's largest online tutorials library
delete status:true
tutorialspoint value in cache - null
廣告