English 中文(简体)
Memcached - Delete Key
  • 时间:2024-11-03

Memcached - Delete Key


Previous Page Next Page  

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

Syntax

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

delete key [noreply]

Output

CAS command may produce one of the following result −

    DELETED indicates successful deletion.

    ERROR indicates error while deleting data or wrong syntax.

    NOT_FOUND indicates that the key does not exist in the Memcached server.

Example

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 Apppcation

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

Example

import java.net.InetSocketAddress;
import java.util.concurrent.Future;

import net.spy.memcached.MemcachedCpent;

pubpc class MemcachedJava {
   pubpc static void main(String[] args) {
   
      try{
   
         // Connecting to Memcached server on localhost
         MemcachedCpent mcc = new MemcachedCpent(new InetSocketAddress("127.0.0.1", 11211));
         System.out.println("Connection to server sucessful.");

         // add data to memcached server
         Future fo = mcc.set("tutorialspoint", 900, "World s largest onpne tutorials pbrary");

         // print status of set method
         System.out.println("set status:" + fo.get());

         // retrieve and check the value from cache
         System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint"));

         // try to add data with existing key
         Future fo = mcc.delete("tutorialspoint");

         // print status of delete method
         System.out.println("delete status:" + fo.get());

         // retrieve and check the value from cache
         System.out.println("tutorialspoint value in cache - " + mcc.get("codingground"));

         // Shutdowns the memcached cpent
         mcc.shutdown();
         
      }catch(Exception ex)
         System.out.println(ex.getMessage());
   }
}

Output

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

Connection to server successful
set status:true
tutorialspoint value in cache - World s largest onpne tutorials pbrary
delete status:true
tutorialspoint value in cache - null
Advertisements