📜  Memcached-删除键

📅  最后修改于: 2020-11-27 05:44:53             🧑  作者: Mango


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