美文网首页
5. Redis key相关命令

5. Redis key相关命令

作者: 大明白 | 来源:发表于2015-11-20 17:55 被阅读451次

KEYS pattern

返回所有给定模式的key(pattern类似于正则)

  • *: 匹配任意个字符

  • ?: 匹配单个任意字符

  • \x: 匹配特殊字符 ? *

      127.0.0.1:6379> KEYS *
      1) "str1"
      2) "str6"
      3) "yiibai"
      4) "tedis"
      5) "str2"
      6) "userinfo"
      7) "str3"
      8) "str4"
      9) "redis"
      10) "page2"
      11) "page"
      12) "str5"
      127.0.0.1:6379> KEYS str*
      1) "str1"
      2) "str6"
      3) "str2"
      4) "str3"
      5) "str4"
      6) "str5"
      127.0.0.1:6379> KEYS str[1-3]
      1) "str1"
      2) "str2"
      3) "str3"
      127.0.0.1:6379> KEYS page?
      1) "page2"
    

EXISTS key

检测指定key是否存在,存在返回1,否则返回0

127.0.0.1:6379> EXISTS userinfo
(integer) 1

TYPE key

返回key存储的类型,如果不存在则返回none

127.0.0.1:6379> TYPE userinfo
hash
127.0.0.1:6379> TYPE redis
string
127.0.0.1:6379> TYPE notExists
none

EXPIRE key seconds

设置key的过期时间,如果key不存在则返回0,否则返回1.如果key已经存在过期时间则再设置会覆盖之前的过期时间

127.0.0.1:6379> EXPIRE tedis 5
(integer) 1
127.0.0.1:6379> TTL tedis
(integer) 2
127.0.0.1:6379> TTL tedis
(integer) -2
127.0.0.1:6379> EXISTS tedis
(integer) 0
127.0.0.1:6379> EXPIRE tedis 5
(integer) 0   // tedis不存在

EXPIREAT key timestamp

设置在指定timestamp过期

127.0.0.1:6379> EXPIREAT redis 1448018396
(integer) 1
127.0.0.1:6379> TTL redis
(integer) 6960

PEXPIRE key milliseconds

设定过期时间为毫秒,其他与EXPIRE相同

127.0.0.1:6379> PEXPIRE redis 60000
(integer) 1
127.0.0.1:6379> TTL redis
(integer) 58

PEXPIREAT key timestamp

设定指定timestamp过期,单位是毫秒,其他与EXPIREBY相同

127.0.0.1:6379> PEXPIREAT redis 1448018396000
(integer) 1
127.0.0.1:6379> TTL redis
(integer) 6662

TTL key

以秒为单位返回key的剩余时间,如果没有设置过期时间,则返回-1,如果key不存在,则返回-2

127.0.0.1:6379> TTL redis
(integer) 6523
127.0.0.1:6379> TTL stronger
(integer) -2
127.0.0.1:6379> TTL str1
(integer) -1

PTTL key

以毫秒为单位,返回key的剩余时间,如果没有设置过期时间,则返回-1,如果key不存在,则返回-2

127.0.0.1:6379> PTTL redis
(integer) 6505944
127.0.0.1:6379> PTTL str1
(integer) -1
127.0.0.1:6379> PTTL stronger
(integer) -2

PERSIST key

将带有过期时间的key变为永久的key,成功返回1,key不存在或者已经是永久的key则返回0

127.0.0.1:6379> TTL redis
(integer) 6377
127.0.0.1:6379> PERSIST redis
(integer) 1
127.0.0.1:6379> TTL redis
(integer) -1

DEL key [key]##

删除指定的key 一个或多个,返回值是删除的key的个数

127.0.0.1:6379> DEL str5
(integer) 1
127.0.0.1:6379> DEL str5
(integer) 0
127.0.0.1:6379> DEL str3 str4
(integer) 2

RANDOMKEY

随机的从当前数据库中返回一个KEY

RENAME key newkey##

重命名key为newkey,如果newkey是已经存在的key则覆盖它,如果key和newkey相同则报错,如果rename一个不存在的key会保存

127.0.0.1:6379> KEYS *
1) "str1"
2) "str6"
3) "yiibai"
4) "str2"
5) "userinfo"
6) "redis"
7) "page2"
8) "page"
127.0.0.1:6379> RENAME str6 str3
OK
127.0.0.1:6379> RENAME str3 str2
OK
127.0.0.1:6379> KEYS *
1) "str1"
2) "str2"
3) "yiibai"
4) "userinfo"
5) "redis"
6) "page2"
7) "page"
127.0.0.1:6379> GET str2
"text6"
127.0.0.1:6379> GET str1
"text1"
127.0.0.1:6379> RENAME str2 str2
(error) ERR source and destination objects are the same

RENAMENX key newkey

必须重命名这个新名称不存在才能成功,失败返回0

127.0.0.1:6379> EXISTS str1
(integer) 1
127.0.0.1:6379> RENAMENX str2 str1
(integer) 0

DUMP key

序列化个定的key,返回序列化之后的值

127.0.0.1:6379> DUMP redis
"\x00\x0bhello world\x06\x00\x9d\x11!9S\x92\x9c\xa7"

RESTORE key ttl value

反序列化字符串到key上,这个key不能是已存在的,否则出粗。ttl设定有效时间的毫秒数,设置0表示永久

127.0.0.1:6379> RESTORE str5 0 "\x00\x0bhello world\x06\x00\x9d\x11!9S\x92\x9c\xa7"
OK
127.0.0.1:6379> RESTORE str2 0 "\x00\x0bhello world\x06\x00\x9d\x11!9S\x92\x9c\xa7"
(error) BUSYKEY Target key name already exists.

MOVE key DbId

移动key从一个数据库到另外一个数据库中,如果移动的key不存在则操作失败;如果目的数据库已经存在key则操作失败返回0,成功返回1

127.0.0.1:6379> MOVE str5 2
(integer) 1  // 成功
127.0.0.1:6379> SELECT 2
OK
127.0.0.1:6379[2]> KEYs *
1) "str5"
127.0.0.1:6379[2]> SELECT 0
OK
127.0.0.1:6379> KEYS str*
1) "str1"
2) "str2"
127.0.0.1:6379> set str5 "from 0"
OK
127.0.0.1:6379> KEYS str*
1) "str1"
2) "str2"
3) "str5"
127.0.0.1:6379> MOVE str5 2
(integer) 0  // 失败,2中已存在key

OBJECT MIGRATE SCAN SORT 后续讲解

相关文章

  • 5. Redis key相关命令

    KEYS pattern 返回所有给定模式的key(pattern类似于正则) *: 匹配任意个字符 ?: 匹配单...

  • Redis命令详解:Keys

    介绍完Redis连接相关命令后,再来介绍一下与Key相关的命令,Redis作为一个key-value数据库,对Ke...

  • Redis键(Key)

    Redis keys 命令 下表给出了与 Redis 键相关的基本命令: 序号命令及描述 1DEL key 该命令...

  • redis hashset 用法

    1. Redis HashSet相关命令 HSET key field value:为指定的Key设定Field/...

  • Redis数据库浅析

    相关命令 FLUSHDB 删除当前Redis DB的所有的key FLUSHALL 删除所有Redis DB的所有...

  • Redis基本命令(数据类型)

    Redis命令大全 Redis命令大全 Key常用命令 keys * exists key(是否存在) move ...

  • Redis

    Redis Redis 是一个高性能的key-value数据库,支持主从同步。 配置文件 其他相关命令 清库命令 ...

  • Redis 键(key) 命令

    Redis 键(key) 命令 1> del 命令:该命令用于在 key 存在是删除 key格式:DEL key返...

  • redis 简单记录

    redis关注点: 1、redis命令:Redis 命令参考 — Redis 命令参考 字符串:SET key v...

  • redis数据结构详解之Hash

    redis中hash对单个key/value操作的命令演示 redis中hash的添加命令hset,如果key不存...

网友评论

      本文标题:5. Redis key相关命令

      本文链接:https://www.haomeiwen.com/subject/peilhttx.html