memcached——基本操作

参考链接:https://www.runoob.com/memcached/memcached-tutorial.html

memcached的命令大致可以分为三类:存储,查找,与统计。

最为基本的查找命令,就是get了。直接get一个key值,若存在,则会输出它的信息。若不存在,则输出空。

现大致介绍一下memcached的基本操作

set:

set key flags exptime bytes [noreply] 
value 

参数说明:

  • key:键值 key-value 结构中的 key,用于查找缓存值。
  • flags:可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息 。
  • exptime:在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
  • bytes:在缓存中存储的字节数
  • noreply(可选): 该参数告知服务器不需要返回数据
  • value:存储的值(始终位于第二行)(可直接理解为key-value结构中的value)

需要注意的是,输入的bytes数量,不能小于value的长度,否则会报错

set name 0 0 3
haha
CLIENT_ERROR bad data chunk
ERROR

若输入的value小于指定的bytes,则会将将回车也做为输入

set name 0 0 3
a
a
CLIENT_ERROR bad data chunk
ERROR

考虑到回车相当于\r\n,把长度加1

set name 0 0 4
a
a
STORED

个人理解为,当输入的串的长度,大于等于指定的bytes时,才会完成指令,然后校验总长度是否合法。

输入一个正常的长度时:

set name 0 0 3
ccx
STORED

add:

add key flags exptime bytes [noreply]
value

参数与set相同,就不再一一描述。同样,输入的value长度要与指定的bytes相同。

add name 0 0 4
cxcx
NOT_STORED

由于 key=name 之前设置过了,所以这里不会被修改。

add age 0 0 1
9
STORED

由此可见,add只能插入不存在的key。

replace:

replace key flags exptime bytes [noreply]
value

依然是相同的参数

replace name 0 0 4
cxcx
STORED
replace local 0 0 4
fzuu
NOT_STORED

现整理一下set、add、replace的区别

cmd key存在 key不存在
set succ succ
add fail succ
replace succ fail

 

memcached还提供了其它修改数据的操作

append/prepend:

与replace不同的是,replace是直接使用输入的串替换原有的串,而append与prepend分别是向后/向前追加内容。

get name
VALUE name 0 4
cxcx
END
append name 0 0 3
abc
STORED
get name
VALUE name 0 7
cxcxabc
END
prepend name 0 0 3
qwe
STORED
get name 
VALUE name 0 10
qwecxcxabc
END

cas:

cas key flags exptime bytes unique_cas_token [noreply]
value

比普通的set多了个参数 unique_cas_token,需要把通过 gets 命令获取的一个唯一的64位值传过去。若传入的值与此刻通过gets获取得值不同,则不会写入。

set name 0 0 3
ccx
STORED
get name ccx
VALUE name 0 3
ccx
END
gets name cc
VALUE name 0 3 24
ccx
END
cas name 0 0 3
ERROR //参数数量不对
cas name 0 0 3 20
aaa
EXISTS //传入的值应该为24,此处传入20,不会修改key=name的内容
get name
VALUE name 0 3
ccx
END
cas name 0 0  3 24
bbb
STORED
gets name
VALUE name 0 3 25  //再次gets得到的值已自增,若再继续用24,则key=name的内容将不会被 修改
bbb
END
cas names 0 0 3 0
ccx
NOT_FOUND //输入的key不存在

incr/decr:

incr key increment_value
decr key decrement_value

分别为对一个已存在的key的数字value进行增加/减少的操作。若不是数值value,则会输出CLIENT_ERROR

set name 0 0 3
ccx
STORED
incr name 1
CLIENT_ERROR cannot increment or decrement non-numeric value
et
ERROR
set age 0 0 2
10
STORED
incr age 1
11
get age
VALUE age 0 2
11
END
incr age 10
21
get age
VALUE age 0 2
21
END
decr age 5
16
get age
VALUE age 0 2
16
END

get与gets:

get key1 [key2] [key3] ...
gets key1 [key2] [key3] ...
get name
VALUE name 0 3
ccx
END
gets name
VALUE name 0 3 26  //gets仅比get多返回了一个数字,用于cas
ccx
END
get name age  //输入多个key时,会依次显示对应的值
VALUE name 0 3
ccx
VALUE age 0 1
9
END
get names   //get不存在的key时,则会输出空
END

delete:

delete key [noreply]
set name 0 0 3   //插入key = name value = ccx
ccx
STORED             //插入成功
get name
VALUE name 0 3
ccx
END
delete name        //删除 key = name 
DELETED             //删除成功
get name
END

stats:

stats是memcached里的统计cmd,若不加参数,则输出memcached的服务信息。可加的参数如:items,sizes,等。

猜你喜欢

转载自www.cnblogs.com/chinxi/p/12214717.html