Memcached-删除key

本文概述

  • Ubuntu中的示例
  • Windows中的示例
  • 使用Java应用程序删除数据
Memcached delete命令用于从Memcached服务器删除现有密钥。这意味着它将从缓存中删除一个项目(如果存在)。
句法
delete key [noreply]

这里,
key:它是从Memcached存储和检索的密钥数据。
noreply:这是一个可选参数。它用于通知服务器不发送任何答复。
返回值
此命令将返回以下值:
删除:表示删除成功。
错误:删除数据或语法错误时显示错误。
NOT_FOUND:表明该密钥在Memcached服务器中不存在。
Ubuntu中的示例 让我们看一下delete命令的示例。在这里, 我们将使用” 城市” 作为关键字, 并在900秒的时间到期后将” 班加罗尔” 存储在其中。此后, 使用delete命令删除键” city” 。
set city 0 900 9bangaloreSTOREDget cityVALUE city 0 9bangaloreENDdelete cityDELETEDget cityENDdelete cityNOT_FOUND

Memcached-删除key

文章图片
Windows中的示例
set city 0 900 9bangaloreSTOREDget cityVALUE city 0 9bangaloreENDdelete cityDELETEDget cityENDdelete cityNOT_FOUND

Memcached服务器:
Memcached-删除key

文章图片
Memcached客户端:
Memcached-删除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 localhostMemcachedClient mcc = new MemcachedClient(newInetSocketAddress("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 cacheSystem.out.println("Get from Cache:"+mcc.get("city")); // delete value from cacheSystem.out.println("Delete from Cache:"+mcc.delete("city").isDone()); // check whether value exists or notSystem.out.println("Get from Cache:"+mcc.get("city")); }}

输出
Connection to server successfulset status: trueGet from Cache: BangaloreDelete from Cache: trueGet 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 localhostMemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); System.out.println("Connection to server sucessful."); // add data to memcached serverFuture fo = mcc.set("srcmini", 900, "largest online tutorials library"); // print status of set methodSystem.out.println("set status:" + fo.get()); // retrieve and check the value from cacheSystem.out.println("srcmini value in cache - " + mcc.get("srcmini")); // try to delete data with existing keyFuture fo = mcc.delete("srcmini"); // print status of delete methodSystem.out.println("delete status:" + fo.get()); // retrieve and check the value from cacheSystem.out.println("srcmini value in cache - " + mcc.get("codingground")); // Shutdowns the memcached clientmcc.shutdown(); }catch(Exception ex)System.out.println(ex.getMessage()); }}

【Memcached-删除key】输出
Connection to server successfulset status:truesrcmini value in cache - largest online tutorials librarydelete status:truesrcmini value in cache - null

    推荐阅读