Memcached – Delete Data

Memcached – Delete data ”; Previous Next 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 Print Page Previous Next Advertisements ”;

Memcached – Delete Key

Memcached – Delete Key ”; Previous Next 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 Print Page Previous Next Advertisements ”;

Memcached – Get CAS Data

Memcached – Get CAS Data ”; Previous Next Memcached gets command is used to get the value with CAS token. If the key does not exist in Memcached, then it returns nothing. Syntax The basic syntax of Memcached gets command is as shown below − get key Example set tutorialspoint 0 900 9 memcached STORED gets tutorialspoint VALUE tutorialspoint 0 9 1 memcached END In this example, we use tutorialspoint as the key and store memcached in it with an expiration time of 900 seconds. Get CAS Data Using Java Application To get CAS data from a Memcached server, you need to use the Memcached gets 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.gets(“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:{CasValue 2/memcached} Print Page Previous Next Advertisements ”;

Memcached – Home

Memcached Tutorial PDF Version Quick Guide Resources Job Search Discussion Memcached is an open source, high-performance, distributed memory object caching system. This tutorial provides a basic understanding of all the relevant concepts of Memcached needed to create and deploy a highly scalable and performance-oriented system. Audience This tutorial is designed for software professionals who wish to learn and apply the concepts of Memcached in simple and easy steps. Prerequisites Before proceeding with this tutorial, you need to know the basics of data structures. Print Page Previous Next Advertisements ”;

Memcached – Replace Data

Memcached – Replace Data ”; Previous Next Memcached replace command is used to replace the value of an existing key. If the key does not exist, then it gives the output NOT_STORED. Syntax The basic syntax of Memcached replace command is as shown below − replace key flags exptime bytes [noreply] value The keywords in the syntax are as described below − key − It is the name of the key by which data is stored and retrieved from Memcached. flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved. exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration. bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in the Memcached. noreply (optional) − It is a parameter that informs the server not to send any reply. value − It is the data that needs to be stored. The data needs to be passed on the new line after executing the command with the above options. Output The output of the command is as shown below − STORED STORED indicates success. NOT_STORED indicates the data is not stored in Memcached. Example In the following example, we use ‘key’ as the key and store memcached in it with an expiration time of 900 seconds. After this, the same key is replaced with the value ‘redis’. add key 0 900 9 memcached STORED get key VALUE key 0 9 memcached END replace key 0 900 5 redis get key VALUE key 0 5 redis END Replace Data Using Java Application To replace data in a Memcached server, you need to use the Memcached replace 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”)); // now replace the existing data System.out.println(“Replace cache:”+mcc.replace(“tutorialspoint”, 900, “redis”).done); // get the updated data 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 Replace cache:true Get from Cache:redis Print Page Previous Next Advertisements ”;

Memcached – Add Data

Memcached – Add Data ”; Previous Next Memcached add command is used to set a value to a new key. If the key already exists, then it gives the output NOT_STORED. Syntax The basic syntax of Memcached add command is as shown below − add key flags exptime bytes [noreply] value The keywords in the syntax are as described below − key − It is the name of the key by which data is stored and retrieved from Memcached. flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved. exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration. bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached. noreply (optional) − It is a parameter that informs the server not to send any reply. value − It is the data that needs to be stored. The data needs to be passed on the new line after executing the command with the above options. Output The output of the command is as shown below − STORED STORED indicates success. NOT_STORED indicates the data is not stored in Memcached. Example In the following example, we use ‘key’ as the key and add the value Memcached in it with an expiration time of 900 seconds. add key 0 900 9 memcached STORED get key VALUE key 0 9 Memcached END Failure Output add key 0 900 5 redis NOT_STORED Add Data Using Java Application To add data in a Memcached server, you need to use the Memcached add 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(“add status:”+mcc.add(“tutorialspoint”, 900, “redis”).done); System.out.println(“add status:”+mcc.add(“tp”, 900, “redis”).done); // Get value from cache System.out.println(“Get from Cache tp:”+mcc.get(“tp”)); } } Output On compiling and executing the program, you get to see the following output − Connection to server successful add status:false add status:true Get from Cache tp:redis Print Page Previous Next Advertisements ”;

Memcached – Append Data

Memcached – Append Data ”; Previous Next Memcached append command is used to add some data in an existing key. The data is stored after the existing data of the key. Syntax The basic syntax of Memcached append command is as shown below − append key flags exptime bytes [noreply] value The keywords in the syntax are as described below− key − It is the name of the key by which data is stored and retrieved from Memcached. flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved. exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration. bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached. noreply (optional) − It is a parameter that informs the server not send any reply. value − It is the data that needs to be stored. The data needs to be passed on the new line after executing the command with the above options. Output The output of the command is as shown below − STORED STORED indicates success. NOT_STORED indicates the key does not exist in the Memcached server. CLIENT_ERROR indicates error. Example In the following example, we try to add some data in a key that does not exist. Hence, Memcached returns NOT_STORED. After this, we set one key and append data into it. append tutorials 0 900 5 redis NOT_STORED set tutorials 0 900 9 memcached STORED get tutorials VALUE tutorials 0 14 memcached END append tutorials 0 900 5 redis STORED get tutorials VALUE tutorials 0 14 memcachedredis END Append Data Using Java Application To append data in a Memcached server, you need to use the Memcached append 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”).isDone()); // Get value from cache System.out.println(“Get from Cache:”+mcc.get(“tutorialspoint”)); // now append some data into existing key System.out.println(“Append to cache:”+mcc.append(“tutorialspoint”, “redis”).isDone()); // get the updated key 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 Append to cache:true Get from Cache:memcachedredis Print Page Previous Next Advertisements ”;

Memcached – Prepend Data

Memcached – Prepend Data ”; Previous Next Memcached prepend command is used to add some data in an existing key. The data is stored before the existing data of the key. Syntax The basic syntax of Memcached prepend command is as shown below − prepend key flags exptime bytes [noreply] value The keywords in the syntax are as described below− key − It is the name of the key by which data is stored and retrieved in Memcached. flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved. exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration. bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached. noreply (optional) −It is a parameter that informs the server not send any reply. value − It is the data that needs to be stored. Data needs to be passed on the new line after executing the command with the above options. Output The output of the command is as shown below − STORED STORED indicates success. NOT_STORED indicates the key does not exist in the Memcached server. CLIENT_ERROR indicates error. Example In the following example, we add some data in a key that does not exist. Hence, Memcached returns NOT_STORED. After this, we set one key and prepend data into it. prepend tutorials 0 900 5 redis NOT_STORED set tutorials 0 900 9 memcached STORED get tutorials VALUE tutorials 0 14 memcached END prepend tutorials 0 900 5 redis STORED get tutorials VALUE tutorials 0 14 redismemcached END Prepend Data Using Java Application To prepend data in a Memcached server, you need to use the Memcached prepend 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”).isDone()); // Get value from cache System.out.println(“Get from Cache:”+mcc.get(“tutorialspoint”)); // now append some data into existing key System.out.println(“Prepend to cache:”+mcc.prepend(“tutorialspoint”, “redis”).isDone()); // get the updated key 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 Prepend to cache:true Get from Cache:redismemcached Print Page Previous Next Advertisements ”;

Memcached – Environment

Memcached – Environment ”; Previous Next Installing Memcached on Ubuntu To install Memcached on Ubuntu, go to terminal and type the following commands − $sudo apt-get update $sudo apt-get install memcached Confirming Memcached Installation To confirm if Memcached is installed or not, you need to run the command given below. This command shows that Memcached is running on the default port 11211. $ps aux | grep memcached To run Memcached server on a different port, execute the command given below. This command starts the server on the TCP port 11111 and listens on the UDP port 11111 as a daemon process. $memcached -p 11111 -U 11111 -u user -d You can run multiple instances of Memcached server through a single installation. Memcached Java Environment Setup To use Memcached in your Java program, you need to download spymemcached-2.10.3.jar and setup this jar into the classpath. Print Page Previous Next Advertisements ”;

Memcached – Get Data

Memcached – Get Data ”; Previous Next 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 Print Page Previous Next Advertisements ”;