Redis 键命令用于管理 redis 的键。css
该命令用于在 key 存在时删除 key。html
127.0.0.1:6379> set w3ckey redis OK 127.0.0.1:6379> EXISTS w3ckey (integer) 1 127.0.0.1:6379> del w3ckey (integer) 1 127.0.0.1:6379> EXISTS w3ckey (integer) 0 127.0.0.1:6379>
DUMP key
序列化给定 key ,并返回被序列化的值。mysql
返回值
若是 key 不存在,那么返回 nil 。 不然,返回序列化以后的值。redis
127.0.0.1:6379> SET greeting "hello, dumping world!" OK 127.0.0.1:6379> dump greeting "\x00\x15hello, dumping world!\a\x00,\x7f\xe7\xf1%\xed(W" 127.0.0.1:6379> DUMP not-exists-key (nil) 127.0.0.1:6379>
EXISTS key
检查给定 key 是否存在。sql
返回值
若 key 存在返回 1 ,不然返回 0 。mongodb
redis 127.0.0.1:6379> set runoob-new-key newkey OK redis 127.0.0.1:6379> EXISTS runoob-new-key (integer) 1 redis 127.0.0.1:6379>
EXPIRE key seconds
为给定 key 设置过时时间,以秒计。shell
返回值
设置成功返回 1 。 当 key 不存在或者不能为 key 设置过时时间时(好比在低于 2.1.3 版本的 Redis 中你尝试更新 key 的过时时间)返回 0数据库
redis 127.0.0.1:6379> SET runooobkey redis OK redis 127.0.0.1:6379> EXPIRE runooobkey 60 (integer) 1
以上实例中咱们为键 runooobkey 设置了过时时间为 1 分钟,1分钟后该键会自动删除。swift
EXPIREAT key timestamp
EXPIREAT 的做用和 EXPIRE 相似,都用于为 key 设置过时时间。 不一样在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。cookie
返回值
设置成功返回 1 。 当 key 不存在或者不能为 key 设置过时时间时(好比在低于 2.1.3 版本的 Redis 中你尝试更新 key 的过时时间)返回 0 。
redis 127.0.0.1:6379> SET runoobkey redis OK redis 127.0.0.1:6379> EXPIREAT runoobkey 1293840000 (integer) 1 redis 127.0.0.1:6379> EXISTS runoobkey (integer) 0
PEXPIRE key milliseconds
设置 key 的过时时间以毫秒
计。
redis> SET mykey "Hello" "OK" redis> PEXPIRE mykey 1500 (integer) 1 redis> TTL mykey (integer) 1 redis> PTTL mykey (integer) 1498 redis>
PEXPIREAT key milliseconds-timestamp
设置 key 过时时间的时间戳(unix timestamp) 以毫秒计
返回值
设置成功返回 1 。 当 key 不存在或者不能为 key 设置过时时间时(好比在低于 2.1.3 版本的 Redis 中你尝试更新 key 的过时时间)返回 0 。
redis 127.0.0.1:6379> SET runoobkey redis OK redis 127.0.0.1:6379> PEXPIREAT runoobkey 1555555555005 (integer) 1
KEYS pattern
查找全部符合给定模式( pattern)的 key 。
首先建立一些 key,并赋上对应值:
127.0.0.1:6379> SET runoob1 redis OK 127.0.0.1:6379> SET runoob2 mysql OK 127.0.0.1:6379> SET runoob3 mongodb OK
查找以 runoob 为开头的 key:
127.0.0.1:6379> keys runoob* 1) "runoob3" 2) "runoob2" 3) "runoob1"
获取 redis 中全部的 key 可用使用 *。
127.0.0.1:6379> KEYS * 1) "runoob3" 2) "runoob1" 3) "runoob2"
MOVE key db
将当前数据库的 key 移动到给定的数据库 db 当中。
# key 存在于当前数据库 redis> SELECT 0 # redis默认使用数据库 0,为了清晰起见,这里再显式指定一次。 OK redis> SET song "secret base - Zone" OK redis> MOVE song 1 # 将 song 移动到数据库 1 (integer) 1 redis> EXISTS song # song 已经被移走 (integer) 0 redis> SELECT 1 # 使用数据库 1 OK redis:1> EXISTS song # 证明 song 被移到了数据库 1 (注意命令提示符变成了"redis:1",代表正在使用数据库 1) (integer) 1 # 当 key 不存在的时候 redis:1> EXISTS fake_key (integer) 0 redis:1> MOVE fake_key 0 # 试图从数据库 1 移动一个不存在的 key 到数据库 0,失败 (integer) 0 redis:1> select 0 # 使用数据库0 OK redis> EXISTS fake_key # 证明 fake_key 不存在 (integer) 0 # 当源数据库和目标数据库有相同的 key 时 redis> SELECT 0 # 使用数据库0 OK redis> SET favorite_fruit "banana" OK redis> SELECT 1 # 使用数据库1 OK redis:1> SET favorite_fruit "apple" OK redis:1> SELECT 0 # 使用数据库0,并试图将 favorite_fruit 移动到数据库 1 OK redis> MOVE favorite_fruit 1 # 由于两个数据库有相同的 key,MOVE 失败 (integer) 0 redis> GET favorite_fruit # 数据库 0 的 favorite_fruit 没变 "banana" redis> SELECT 1 OK redis:1> GET favorite_fruit # 数据库 1 的 favorite_fruit 也是 "apple"
PERSIST key
移除 key 的过时时间,key 将持久保持。
redis> SET mykey "Hello" OK redis> EXPIRE mykey 10 # 为 key 设置生存时间 (integer) 1 redis> TTL mykey (integer) 10 redis> PERSIST mykey # 移除 key 的生存时间 (integer) 1 redis> TTL mykey (integer) -1
PTTL key
以毫秒为单位返回 key 的剩余的过时时间。
返回值
当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 不然,以毫秒为单位,返回 key 的剩余生存时间。
注意:在 Redis 2.8 之前,当 key 不存在,或者 key 没有设置剩余生存时间时,命令都返回 -1 。
# 不存在的 key redis> FLUSHDB OK redis> PTTL key (integer) -2 # key 存在,但没有设置剩余生存时间 redis> SET key value OK redis> PTTL key (integer) -1 # 有剩余生存时间的 key redis> PEXPIRE key 10086 (integer) 1 redis> PTTL key (integer) 6179
TTL key
以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
# 不存在的 key redis> FLUSHDB OK redis> TTL key (integer) -2 # key 存在,但没有设置剩余生存时间 redis> SET key value OK redis> TTL key (integer) -1 # 有剩余生存时间的 key redis> EXPIRE key 10086 (integer) 1 redis> TTL key (integer) 10084
RANDOMKEY
从当前数据库中随机返回一个 key 。
# 数据库不为空 redis> MSET fruit "apple" drink "beer" food "cookies" # 设置多个 key OK redis> RANDOMKEY "fruit" redis> RANDOMKEY "food" redis> KEYS * # 查看数据库内全部key,证实 RANDOMKEY 并不删除 key 1) "food" 2) "drink" 3) "fruit" # 数据库为空 redis> FLUSHDB # 删除当前数据库全部 key OK redis> RANDOMKEY (nil)
RENAME oldkey newkey
修改 key 的名称
返回值
更名成功时提示 OK ,失败时候返回一个错误。
当 oldkey 和 newkey 相同,或者 oldkey 不存在时,返回一个错误。 当 newkey 已经存在时,RENAME
命令将覆盖旧值。
# key 存在且 newkey 不存在 redis> SET message "hello world" OK redis> RENAME message greeting OK redis> EXISTS message # message 不复存在 (integer) 0 redis> EXISTS greeting # greeting 取而代之 (integer) 1 # 当 key 不存在时,返回错误 redis> RENAME fake_key never_exists (error) ERR no such key # newkey 已存在时, RENAME 会覆盖旧 newkey redis> SET pc "lenovo" OK redis> SET personal_computer "dell" OK redis> RENAME pc personal_computer OK redis> GET pc (nil) redis:1> GET personal_computer # 原来的值 dell 被覆盖了 "lenovo"
RENAMENX oldkey newkey
仅当 newkey 不存在时,将 oldkey 更名为 newkey 。
# newkey 不存在,更名成功 redis> SET player "MPlyaer" OK redis> EXISTS best_player (integer) 0 redis> RENAMENX player best_player (integer) 1 # newkey存在时,失败 redis> SET animal "bear" OK redis> SET favorite_animal "butterfly" OK redis> RENAMENX animal favorite_animal (integer) 0 redis> get animal "bear" redis> get favorite_animal "butterfly"
相同:
newkey 不存在
RENAME
和RENAMENX
同样:oldkey删除
不一样:
newkey 已存在时
RENAME
会覆盖旧 newkey,RENAMENX
失败。
TYPE key
返回 key 所储存的值的类型。
# 字符串 redis> SET weather "sunny" OK redis> TYPE weather string # 列表 redis> LPUSH book_list "programming in scala" (integer) 1 redis> TYPE book_list list # 集合 redis> SADD pat "dog" (integer) 1 redis> TYPE pat set
runoob,Redis 键(key)