Memcached Storage Commands
Memcached - Set Data
Memcached set command is used to set a value to key; if the key does not exist, a new key is created and value is assigned to that 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 unique key by which data is accessed.
- 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
- bytes - This is the length of the data in bytes that needs to be stored in Memcached.
- noreply (optional) - This parameter informs the server not to send any reply.
- value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.
Example
In the following example, we use −
key → tutorialspoint
flag → 0
exptime → 900 (expiry time in seconds)
bytes → 9 (size of data balue in bytes)
value → memcached
set tutorialspoint 0 900 9 memcached STORED get tutorialspoint VALUE tutorialspoint 0 9 memcached END
Output
The output of the command is as shown below −
STORED
- STORED indicates success.
- ERROR indicates incorrect syntax or error while saving data.
Set Data Using Java Application
To set a key in Memcached server, we need to use Memcached set method. The set method returns Future Object. We need to includejava.util.concurrent.Future interface in order to store output of set 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."); // now set data into memcached server Future fo = mcc.set("tutorialspoint", 900, "Free Education"); // 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")); // Shutdowns the memcached client mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
This program should set the value memcached with key tutorialspoint. If the key-value pair is set successfully, the get method of Future object should return true.
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 - Free Education
Memcached - Add Data
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 unique key by which data is accessed.
- 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
- bytes - This is the length of the data in bytes that needs to be stored in Memcached.
- noreply (optional) - This parameter informs the server not to send any reply.
- value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.
Example
In the following example, we use −
key → new_key
flag → 0
exptime → 900 (expiry time in seconds)
bytes → 10 (size of data balue in bytes)
value → data_value
add new_key 0 900 10 data_value STORED get new_key VALUE new_key 0 10 data_value END
Output
If the data is stored successfully, the output should be −
STORED
- STORED indicates success.
- NOT_STORED indicates the data is not stored in Memcached.
Failure Output
Now, if we try to add 'new_key' again, it should give the following error −
add new_key 0 900 5 redis NOT_STORED
Add Data Using Java Application
To store data in a Memcached server, we use the Memcached add method.
Example
In last example, we have set key tutorialspoint. In this example, we shall try to demonstrate adding data having existing key.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, "Free Education"); // 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.add("tutorialspoint", 900, "memcached"); // print status of set method System.out.println("add status:" + fo.get()); // adding a new key to memcached server fo = mcc.add("codingground", 900, "All Free Compilers"); // print status of set method System.out.println("add status:" + fo.get()); // retrieve and check the value from cache System.out.println("codingground 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 - Free Education add status:false add status:true codingground value in cache - All Free Compilers
The first 'add status' displays false because the key tutorialspoint already exists in memcached server. The second 'add status' displays true indicates that the key is successfully stored.
Memcached - Replace Data
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 unique key by which data is accessed.
- 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
- bytes - This is the length of the data in bytes that needs to be stored in Memcached.
- noreply (optional) - This parameter informs the server not to send any reply.
- value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.
Example
For example we shall use −
key → mykey
flag → 0
exptime → 900
bytes → 10 (expiry time in seconds)
value → data_value (size of data balue in bytes)
Here, we use 'mykey' as the key and store data_value in it. After this, the same key is replaced with 'some_other_value'.
add mykey 0 900 10 data_value STORED get mykey VALUE mykey 0 10 data_value END replace mykey 0 900 16 some_other_value get key VALUE mykey 0 16 some_other_value END
Output
The output of the command is as shown below −
STORED
- STORED indicates success.
- NOT_STORED indicates the data is not stored in Memcached.
Replace Data Using Java Application
To replace data in a Memcached server, you need to use the Memcachedreplace 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."); // First add a key and value Future fo = mcc.set("tutorialspoint", 900, "Free Education"); // print status of add method System.out.println("add status:" + fo.get()); // retrieve and check the value from cache System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint")); // adding a new key to memcached server fo = mcc.replace("tutorialspoint", 900, "Largest Tutorials' Library"); // print status of set method System.out.println("replace status:" + fo.get()); // retrieve and check the value from cache System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint")); // 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 - Free Education Replace status:true tutorialspoint value in cache - Largest Tutorials' Library
Memcached - Append Data
Memcached append command is used to add data in an existing key. This data is added at the end of the previous value.
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 unique key by which data is accessed.
- 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
- bytes - This is the length of the data in bytes that needs to be stored in Memcached.
- noreply (optional) - This parameter informs the server not to send any reply.
- value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.
Example
In the following example -
- First we store a key (tutorials) and value (memcached) to Memcached
- Then, We retrieve the value using get command and check it
- Then, we append "redis" to the key tutorials
- and, we retrieve it and check again.
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
Output
The output of the append 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.
Append Data Using Java Application
To append data in a Memcached server, you need to use the Memcachedappend 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, "Free Education"); // 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.append("tutorialspoint", 900, " for All"); // print status of set method System.out.println("append 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 - Free Education append status:true tutorialspoint value in cache - Free Education for All
Memcached - Prepend Data
Memcached prepend command is used to add data in an existing key. This data is added 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 unique key by which data is accessed.
- 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
- bytes - This is the length of the data in bytes that needs to be stored in Memcached.
- noreply (optional) - This parameter informs the server not to send any reply.
- value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.
Example
In the following example -
- First we set a key (tutorials) and value (memcached) in Memcached
- Then, We retrieve the value using get command and check it
- Then, we prepend "redis" to the key tutorials
- and, we retrieve it and check again.
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
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.
Prepend Data Using Java Application
To prepend data in a Memcached server, you need to use the Memcachedprepend 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, "Education for All"); // 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.prepend("tutorialspoint", 900, "Free "); // print status of set method System.out.println("prepend 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 - Education for All prepend status:true tutorialspoint value in cache - Free Education for All
Memcached - CAS Command
CAS stands for Check-And-Set or Compare-And-Swap. Memcached CAS command 'checks' and 'set' data item if and only if, no other client process has updated it since last read by this client.
For example, if in the past, we retrieved the value <key, value> as <total, 3784>. If we now try to set or modify this value using CAS command, then CAS command will first check if it is the same value or is changed by some other client. If it is changed, then we should not change it to avoid any race anomalies in the system. If the value is not modified since our last fetch, CAS now sets the updated value.
Memcached server assigns a unique 64-bit CAS token to all items stored in it. We use gets command to retrieve CAS number of any item.
Syntax
The basic syntax of Memcached CAS command is as shown below −
cas key flags exptime bytes unique_cas_token [noreply]
The keywords in the syntax are as described below −
- key - It is the name of the unique key by which data is accessed.
- 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
- bytes - This is the length of the data in bytes that needs to be stored in Memcached.
- unique_cas_token − It is a unique token number obtained from gets comamand.
- noreply (optional) - This parameter informs the server not to send any reply.
- value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.
Example
To execute a CAS command in Memcached, you need to obtain a CAS token from the Memcached gets command. gets command is variation of get command, it is explained in forthcoming chapter.
In this example we should see −
- Incorrect cas statement i.e. unique cas key missing
- casing a non-existing key
- add a key value item
- obtain unique cas key using gets command
- use cas and unique_cas_key to update data item
- use get command and check if data is updated
cas tp 0 900 9 ERROR <− unique cas key missing cas tp 0 900 9 2 memcached NOT_FOUND <− the key tp does not exist set tp 0 900 9 memcached STORED gets tp VALUE tp 0 9 1 memcached END cas tp 0 900 5 1 redis STORED get tp VALUE tp 0 5 redis END
Output
CAS command may produce one of the following result −
- STORED indicates success.
- ERROR indicates error while saving data or wrong syntax.
- EXISTS indicates that someone has modified the CAS data since last fetch.
- NOT_FOUND indicates that the key does not exist in the Memcached server.
CAS Using Java Application
To get CAS data from a Memcached server, you need to use Memcached getsmethod.
Example
import java.net.InetSocketAddress; import java.util.concurrent.Future; import net.spy.memcached.CASValue; import net.spy.memcached.CASResponse; 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, "Free Education"); // print status of set method System.out.println("set status:" + fo.get()); // retrieve value stored for tutorialspoint from cache System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint")); // obtain CAS token value using gets method CASValue casValue = mcc.gets("tutorialspoint"); // display CAS token value System.out.println("CAS token - " + casValue); // try to update data using memcached cas method CASResponse casresp = mcc.cas("tutorialspoint", casValue.getCas(), 900, "Largest Tutorials-Library"); // display CAS Response System.out.println("CAS Response - " + casresp); // retrieve and check the value from cache System.out.println("tutorialspoint value in cache - " + mcc.get("tutorialspoint")); // 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 - Free Education CAS - {CasValue 34/Free Education} CAS Response - OK tutorialspoint value in cache - Largest Tutorials-Library
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment