Memcached Add Data - Memcached

What is Memcached add command ?

Memcached add command is used to set a value to a new key. If it has the same key then it delivers the output NOT_STORED.


Here’s the syntax of Memcached add command:–
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.


Here’s the example, we apply −
key → new_key
flag → 0
exptime → 900 (expiry time in seconds)
bytes → 10 (size of data balue in bytes)
value → data_value


If the data is stored successfully, the output should be –
  • STORED indicates success.
  • NOT_STORED indicates the data is not stored in Memcached.

Failure Output

If you try to add 'new_key' again, then it give the below mentioned error: –

Add Data Using Java Application

Use Memcached add method to store data in a Memcached server:


In last example, we have set key wisdomjobs. In this example, we shall try to demonstrate adding data having existing key.


You will get the below output once you compel and execute the program:
The first 'add status' displays false because the key wisdomjobs already exists in memcached server. The second 'add status' displays true indicates that the key is successfully stored.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Memcached Topics