📜  Memcached-删除数据

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


Memcached delete命令用于从Memcached服务器删除现有密钥。

句法

Memcached delete命令的基本语法如下所示-

delete key

如果成功删除了密钥,则它返回DELETED。如果找不到该键,则它返回NOT_FOUND,否则返回ERROR。

在此示例中,我们将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 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").done);
      
      // Get value from cache
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
      
      // delete value from cache
      System.out.println("Delete from Cache:"+mcc.delete("tutorialspoint").isDone());
      
      // check whether value exists or not
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
   }
}

输出

在编译和执行程序时,您将看到以下输出-

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