Replace operation will change existing key-value. If the key-value do not exists we will get an response like NOT_STORED. Its syntax is similar to add.
replace key flags expiretime bytes value
- replace is the name of the verb
- key the key which replaced
- flag as flag
- expiretime the time range the key-value stored in memcached
- bytes the size of the new value
- value new value
Let’s add new data and check it exists.
add poftut 0 100 4 test STORED get poftut VALUE poftut 0 4 test END
We will replace poftut key-value with new value.
replace poftut 0 100 7 testnew STORED
- replace the verb
- poftut is the key we want to replace its data
- 0 flags
- 100 the second we want to hold new key-value
- 7 is the size of new value
- testnew is new value
- STORED the result of the successful replace operation
Python Application
import memcache client=memcache.Client([('127.0.0.1',11211)]) #Add samp={"poftut2":"test"} client.add("sample",samp,time=1000) #Replace samp={"poftut":"testnew"} client.replace("sample",samp,time=1000) print(client.get("sample"))
We save the code as filename replace.py.After running the code the output we get.
$ python3 replace.py {'poftut': 'testnew'}