Memcached刪除/Delete資料


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

語法

memcached delete命令的基本語法如下所示:

delete key

如果鍵成功刪除,則返回DELETED,如果key沒有找到則返回NOT_FOUND,否則返回ERROR。

範例

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

在上面的例子中,我們已經使用yiibai作為鍵,並儲存在memcached其900秒後失效,之後刪除所儲存的鍵。

使用Java應用程式刪除資料

要從memcached伺服器中刪除資料,需要使用memcached的delete方法。

範例

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 sucessfully");
      System.out.println("set status:"+mcc.set("yiibai", 900, "memcached").done);
      //Get value from cache
      System.out.println("Get from Cache:"+mcc.get("yiibai"));
      //delete value from cache
      System.out.println("Delete from Cache:"+mcc.delete("yiibai").isDone());
      //check whether value exists or not
      System.out.println("Get from Cache:"+mcc.get("yiibai"));
   }
}

輸出

當上述程式編譯和執行,它提供了以下的輸出:

Connection to server successfully
set status:true
Get from Cache:memcached
Delete from Cache:true
Get from Cache:null