Memcached delete command is used to delete an existing key from the Memcached server.
The basic syntax of Memcached delete command is as shown below −
delete key [noreply]
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.
In this example, we use howcodex as a key and store memcached in it with an expiration time of 900 seconds. After this, it deletes the stored key.
set howcodex 0 900 9 memcached STORED get howcodex VALUE howcodex 0 9 memcached END delete howcodex DELETED get howcodex END delete howcodex NOT_FOUND
To delete data from a Memcached server, you need to use the Memcached delete method.
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("howcodex", 900, "World's 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("howcodex value in cache - " + mcc.get("howcodex")); // try to add data with existing key Future fo = mcc.delete("howcodex"); // print status of delete method System.out.println("delete status:" + fo.get()); // retrieve and check the value from cache System.out.println("howcodex value in cache - " + mcc.get("codingground")); // Shutdowns the memcached client mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
On compiling and executing the program, you get to see the following output −
Connection to server successful set status:true howcodex value in cache - World's largest online tutorials library delete status:true howcodex value in cache - null