Memcached replace Command
The Memcached replace command is used to replace the value of an already existing key 's value (data value).
If the key does not exist, the replacement fails and you will get the response NOT_STORED.
Syntax:
The basic syntax format of the replace command is as follows:
replace key flags exptime bytes [noreply]
value
The parameters are described as follows:
- **key:** the key in the key-value structure, used to look up the cached value.
- flags: can include an integer parameter of the key-value pair, which the client uses to store extra information about the key-value pair .
- exptime: the length of time to keep the key-value pair in the cache (in seconds, 0 means forever)
- bytes: the number of bytes stored in the cache
- noreply (optional): this parameter tells the server that it does not need to return data
- value: the stored value (always on the second line) (can be understood directly as the value in the key-value structure)
Example
In the following example we set:
- key → mykey
- flag → 0
- exptime → 900 (in seconds)
- bytes → 10 (number of bytes of stored data)
- value → data_value
In the following example we use the key 'mykey' and store the corresponding value data_value. After execution we replace the value of the same key 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 mykey
VALUE mykey 0 16
some_other_value
END
Output
If the data is added successfully, it outputs:
STORED
Description of the output information:
- STORED: output after a successful save.
- NOT_STORED: output after a failed replacement.