Memcached 删除键

  • Telnet删除键

    Memcached delete命令用于从Memcached服务器删除现有键。
    Memcached delete命令的基本语法如下所示-
    
    delete key [noreply]
    
    返回
    • DELETED - 表示删除成功。
    • ERROR - 表示删除数据时出错或语法错误。
    • NOT_FOUND - 表示该密钥在Memcached服务器中不存在。
    在此示例中,我们将 jc2182 用作密钥,并将memcached存储在其中,有效时间为900秒。此后,它将删除存储的密钥。
    
    set jc2182 0 900 9
    memcached
    STORED
    get jc2182
    VALUE jc2182 0 9
    memcached
    END
    delete jc2182
    DELETED
    get jc2182
    END
    delete jc2182
    NOT_FOUND
    
  • 使用Java应用程序删除键

    要在Memcached服务器中删除键,您需要使用Memcached delete方法。
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import net.spy.memcached.MemcachedClient;
    
    public class MemcachedJava {
       public static void main(String[] args) {
    
          // Connecting to Memcached server on localhost
          MemcachedClient mcc = null;
          try {
             mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
          } catch (IOException e) {
             // TODO 自动生成的 catch 块
             e.printStackTrace();
          }
          System.out.println("Connection to server sucessfully");
    
          // 设置数据库 第一个参数key 第二个参数过期时间,第三个参数value
          System.out.println("set status:" + mcc.set("jc2182", 900, "memcachd").getStatus());
          System.out.println("Get from Cache:" + mcc.get("jc2182"));
          
          System.out.println("Delete Key " + mcc.delete("jc2182").getStatus());
          System.out.println("Get from Cache:" + mcc.get("jc2182"));
       }
    }
    
    输出
     
    Connection to server sucessfully 
    set status:{OperationStatus success=true:  STORED}
    Get from Cache:memcachd
    Delete Key {OperationStatus success=true:  DELETED}
    Get from Cache:null