Your cart is currently empty!
Category: memcached
-
Khóa học miễn phí Memcached – Delete Key nhận dự án làm có lương
Memcached – Delete Key
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 Application
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.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("tutorialspoint", 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("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 client mcc.shutdown(); }catch(Exception ex) System.out.println(ex.getMessage()); } }
Output
On compiling 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 online tutorials library delete status:true tutorialspoint value in cache - null
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí Memcached – Incr/Decr nhận dự án làm có lương
Memcached – Increment Decrement Data
Memcached incr and decr commands are used to increment or decrement the numeric value of an existing key. If the key is not found, then it returns NOT_FOUND. If the key is not numeric, then it returns CLIENT_ERROR cannot increment or decrement non-numeric value. Otherwise, ERROR is returned.
Syntax – incr
The basic syntax of Memcached incr command is as shown below −
incr key increment_value
Example
In this example, we use visitors as key and set 10 initially into it, thereafter we increment the visitors by 5.
set visitors 0 900 2 10 STORED get visitors VALUE visitors 0 2 10 END incr visitors 5 15 get visitors VALUE visitors 0 2 15 END
Syntax – decr
The basic syntax of Memcached decr command is as shown below
decr key decrement_value
Example
set visitors 0 900 2 10 STORED get visitors VALUE visitors 0 2 10 END decr visitors 5 5 get visitors VALUE visitors 0 1 5 END
Incr/Decr Using Java Application
To increment or decrement data in a Memcached server, you need to use Memcached incr or decr methods respectively.
Example
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 sucessfully"); System.out.println("set status:"+mcc.set("count", 900, "5").isDone()); // Get value from cache System.out.println("Get from Cache:"+mcc.get("count")); // now increase the stored value System.out.println("Increment value:"+mcc.incr("count", 2)); // now decrease the stored value System.out.println("Decrement value:"+mcc.decr("count", 1)); // now get the final stored value System.out.println("Get from Cache:"+mcc.get("count")); } }
Output
On compiling and executing the program, you get to see the following output −
Connection to server successfully set status:true Get from Cache:5 Increment value:7 Decrement value:6 Get from Cache:6
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc