logo图片
Memcached教程

Memcached delete key

Memcached delete 命令用于从Memcached服务器中删除现有密钥。这意味着它将从缓存中删除某项(如果存在)。

语法

delete key [noreply]
在这里
key: 它是存储和从Memcached检索的密钥数据。
noreply: : 这是一个可选参数。用来通知服务器不要发送任何答复。

返回值

此命令将返回以下值:
DELETED: 表示删除成功。
ERROR: 删除数据或语法错误时显示错误。
NOT_FOUND: : 表明该密钥在Memcached服务器中不存在。

Ubuntu中的示例

我们来看一下delete命令的示例。在这里,我们将使用"城市"作为关键字,并在900秒的时间到期后将"班加罗尔"存储在其中。此后,使用delete命令删除键" city"。
set city 0 900 9
bangalore
STORED
get city
VALUE city 0 9
bangalore
END
delete city
DELETED
get city
END
delete city
NOT_FOUND
Memcached-Delete Key

Windows中的示例:

set city 0 900 9
bangalore
STORED
get city
VALUE city 0 9
bangalore
END
delete city
DELETED
get city
END
delete city
NOT_FOUND
Memcached服务器:
Memcached-删除键
Memcached客户端:
Memcached-Delete Key

使用Java应用程序删除数据

让我们考虑Memcached服务器正在主机127.0.0.1和端口11211上运行。在这里,我们将使用delete()方法从Memcached服务器删除数据。

示例1

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("city", 900, "bangalore").done);
      
      // Get value from cache
      System.out.println("Get from Cache:"+mcc.get("city"));
      
      // delete value from cache
      System.out.println("Delete from Cache:"+mcc.delete("city").isDone());
      
      // check whether value exists or not
      System.out.println("Get from Cache:"+mcc.get("city"));
   }
}
输出:
Connection to server successful
set status: true
Get from Cache: Bangalore
Delete from Cache: true
Get from Cache: null

示例2

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("lidihuo", 900, "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("lidihuo value in cache-" + mcc.get("lidihuo"));
         // try to delete data with existing key
         future fo = mcc.delete("lidihuo");
         // print status of delete method
         System.out.println("delete status:" + fo.get());
         // retrieve and check the value from cache
         System.out.println("lidihuo 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
lidihuo value in cache-largest online tutorials library
delete status:true
lidihuo value in cache- null

"内容整理不易,点下广告也是鼓励~"
昵称: 邮箱:
Copyright © 2020 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4