Memcached – Add Data
Memcached add command is used to set a value to a new key. It stores the data, only if it does not already exist. New items are added at the top of the LRU (Least Recently Used). If the key already exists and an add fails, then it will return NOT_STORED and promotes the item to the front of the LRU anyway.
Syntax:
Here,
key: It is the key to the data 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: exptime is expiration time in seconds. 0 means no delay. If it 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 is stored in memcached.
noreply: It is an optional parameter. It is used to inform the server not to send any reply.
value: It is the data that have to store. The data needs to be passed on the new line after executing the command with the above options.
Return Value
This command will return the following value:
STORED: STORED means success
NOT_STORED: NOT_STORED means the data is not stored in memcached.
Example in Ubuntu
Let’s see an example for the add command. Here, we use a city as the key and add value bangalore in it with an expiration time of 900 seconds.
Add with Bad Input
Example in Windows
Let’s see an example for simple set command. Here, we use the city as the key and add value bangalore in it with an expiration time of 900 seconds.
Example with Bad Input
Example in Java Application
Consider that Memcached server is running on host 127.0.0.1and port 11211. Here, we will use add() method to add a key in memcached server.
Example:
Output:
Connection to server successfully add status: false add status: true Get from ct: Delhi