Memcached – Set Data ”; Previous Next Memcached set command is used to set a new value to a new or existing key. Syntax The basic syntax of Memcached set command is as shown below − set 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 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. ERROR indicates incorrect syntax or error while saving data. Example In the following example, we use tutorialspoint as the key and set value 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 Set Data Using Java Application To set a key in Memcached server, you need to use Memcached set 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 ”;
Category: memcached
Memcached – Connection
Memcached – Connection ”; Previous Next To connect to a Memcached server, you need to use the telnet command on HOST and PORT names. Syntax The basic syntax of Memcached telnet command is as shown below − $telnet HOST PORT Here, HOST and PORT are machine IP and port number respectively, on which the Memcached server is executing. Example The following example shows how to connect to a Memcached server and execute a simple set and get command. Assume that the Memcached server is running on host 127.0.0.1 and port 11211. $telnet 127.0.0.1 11211 Trying 127.0.0.1… Connected to 127.0.0.1. Escape character is ”^]”. // now store some data and get it from memcached server set tutorialspoint 0 900 9 memcached STORED get tutorialspoint VALUE tutorialspoint 0 9 memcached END Connection from Java Application To connect the Memcached server from your java program, you need to add the Memcached jar into your classpath as shown in the previous chapter. Assume that the Memcached server is running on host 127.0.0.1 and port 11211. − 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”); //not set data into memcached server 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. The terminal may show few informational messages too, those can be ignored. 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 ”;