Your cart is currently empty!
Author: alien
-
Khóa học miễn phí Memcached – Get Data nhận dự án làm có lương
Memcached – Get Data
Memcached get command is used to get the value stored at key. If the key does not exist in Memcached, then it returns nothing.
Syntax
The basic syntax of Memcached get command is as shown below −
get key
Example
In the following example, we use tutorialspoint as the key and store memcached in it with an expiration time of 900 seconds.
set tutorialspoint 0 900 9 memcached STORED get tutorialspoint VALUE tutorialspoint 0 9 memcached END
Get Data Using Java Application
To get data from a Memcached server, you need to use the Memcached get method.
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("tutorialspoint", 900, "memcached").done); // Get value from cache System.out.println("Get from Cache:"+mcc.get("tutorialspoint")); } }
Output
On compiling and executing the program, you get to see the following output −
Connection to server successfully set status:true Get from Cache:memcached
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 – Delete Data nhận dự án làm có lương
Memcached – Delete data
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
If the key is successfully deleted, then it returns DELETED. If the key is not found, then it returns NOT_FOUND, otherwise it returns ERROR.
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 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 successful"); System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done); // Get value from cache System.out.println("Get from Cache:"+mcc.get("tutorialspoint")); // delete value from cache System.out.println("Delete from Cache:"+mcc.delete("tutorialspoint").isDone()); // check whether value exists or not System.out.println("Get from Cache:"+mcc.get("tutorialspoint")); } }
Output
On compiling and executing the program, you get to see the following output −
Connection to server successful set status:true Get from Cache:memcached Delete from Cache:true Get from 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