Add operation is similar to the set operation but the difference is that is a key is all ready exists NOT_STORED code is returned. This makes add operation more reliable than set and aware of the key overwrite. Syntax is like below
add key flags expiretime bytes value
- key is the identifier of the value
- flags is operations details
- expiretime is the time the key-value will be hold in memory.
- bytes is total size of the value as byte
- value is data we want to save
Let’s try this with our simple telnet connection.
add poftut 0 100 4 test STORED add poftut 0 100 4 test NOT_STORED
- potut is out key
- 0 is flag
- 100 is the timeout for the key-value
- 4 is the size of the value
- test is our value
- STORED successfully saved
- NOT_STORED failed to save
As we see when we try again the same key we get a response NOT_STORED like we stated before.
Python Application
This application will import memcache
library and then create a client object where we will provide some configuration like the memcache host IP address or hostname and the port number. We will than create a python dictionary which holds some value-key pairs and than use client
object add
function to put the samp
dictionary values to the memcache server.
import memcache client=memcache.Client([('127.0.0.1',11211)]) samp={"poftut2":"test"} client.add("sample",samp,time=1000)