Redis中的所有数据结构均不支持数据类型的嵌套。比如,集合类型的每个元素都只能是字符串,而不能是另一个集合或散列表(Hash)。
字符串(String)
SET - 插入数据
SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX] [GET]
Set key
to hold the string value
. If key
already holds a value, it is overwritten, regardless of its type.
如果当前 key 已经存在 value,则用新 value 覆盖原有的value值。
一旦设置成功(无论当前有没有发生覆盖),则返回OK;如果没设置成功,返回 nil (比如 options中包含了 nx
,这时,只有当当前 key 不存在 value时,才会真正去设置value,即无覆盖发生。否则,就会返回 nil)。
Return Value
- Simple string reply: OK if SET was executed correctly.
- Null reply: a Null Bulk Reply is returned if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.
Options
The SET command supports a set of options that modify its behavior:
-
EX
seconds – Set the specified expire time, in seconds.
-
PX
milliseconds – Set the specified expire time, in milliseconds.
-
NX
– Only set the key if it does not already exist.
127.0.0.1:6379> set a cc
OK
127.0.0.1:6379> set a cccc
OK
127.0.0.1:6379> set a ddd NX
(nil)
-
XX
– Only set the key if it already exist.
-
KEEPTTL
– Retain the time to live associated with the key.
-
GET
– Return the old value stored at key, or nil when key did not exist.
GET - 读数据
Get the value of key
. If the key does not exist the special value nil
is returned. An error is returned if the value stored at key
is not a string, because GET only handles string values.
即,如果当前key没有value值,则返回null
GETSET key value
将键 key
的值设为 value
, 并返回键 key
在被设置之前的旧值。
返回值
返回给定键 key
的旧值。
如果键 key
没有旧值, 也即是说, 键 key
在被设置之前并不存在, 那么命令返回 nil
。
当键 key
存在但不是字符串类型时, 命令返回一个错误。
APPEND - 数据追加
- 如果当前key的value有值则附加到原有string后面,如果没有则写入。返回值为追加后的数据长度
DEL - 数据删除
- Time complexity: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).
Removes the specified keys. A key is ignored if it does not exist.
Return value
Integer reply: The number of keys that were removed.
127.0.0.1:6379> set name wei
OK
127.0.0.1:6379> get name
"wei"
127.0.0.1:6379> append name isgreat
(integer) 10
127.0.0.1:6379> get name
"weiisgreat"
127.0.0.1:6379> del wei
(integer) 0
127.0.0.1:6379> get wei
(nil)
...