位置:首頁 > 數據庫 > Memcached教學 > Memcached刪除/Delete數據

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