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
添加操作类似于设置操作,但区别在于,键已全部准备就绪,并且已返回NOT_STORED代码。 这使添加操作比设置和意识到密钥覆盖更可靠。 语法如下
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.
expiretime是键值将保留在内存中的时间。
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.
让我们通过简单的telnet连接尝试一下。
add poftut 0 100 4
test
STORED
add poftut 0 100 4
test
NOT_STORED
potut is out key
关键是关键
0 is flag
0是标志
100 is the timeout for the key-value
100是键值的超时
4 is the size of the value
4是数值的大小
test is our value
测试是我们的价值
STORED successfully saved
已成功保存
NOT_STORED failed to save
NOT_STORED保存失败
As we see when we try again the same key we get a response NOT_STORED like we stated before.
正如我们看到的,当我们再次尝试相同的键时,将得到一个NOT_STORED的响应,就像我们之前所说的那样。
Python应用 (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.
此应用程序将导入memcache
库,然后创建一个客户端对象,在此处我们将提供一些配置,例如内存缓存主机IP地址或主机名以及端口号。 我们将创建一个包含一些值-密钥对的python字典,然后使用client
对象add
功能将samp
字典值放入内存缓存服务器。
import memcache
client=memcache.Client([('127.0.0.1',11211)])
samp={"poftut2":"test"}
client.add("sample",samp,time=1000)
翻译自: https://www.poftut.com/memcached-add-operation-python-example/