Memcached 简明教程

Memcached - Delete data

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

Memcached delete command is used to delete an existing key from the Memcached server.

Syntax

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

The basic syntax of Memcached delete command is as shown below −

delete key

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

If the key is successfully deleted, then it returns DELETED. If the key is not found, then it returns NOT_FOUND, otherwise it returns ERROR.

Example

在此示例中,我们使用 tutorialspoint 作为密钥,并将 memcached 存储在其中,其到期时间为 900 秒。此后,它将删除存储的密钥。

In this example, we use tutorialspoint as a key and store memcached in it with an expiration time of 900 seconds. After this, it deletes the stored key.

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

Delete Data Using Java Application

若要从 Memcached 服务器删除数据,您需要使用 Memcached delete 方法。

To delete data from a Memcached server, you need to use the Memcached delete method.

Example

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"));
   }
}

Output

编译并执行该程序后,您可以看到以下输出:

On compiling and executing the program, you get to see the following output −

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