1、键值通用操作

1、键值通用操作

  • keys pattern:查找当前数据库中所有匹配pattern的键

    • pattern支持*,?,[]匹配
      • *:匹配任意(所有)选项
      • ?:任意一位
      • []:匹配其中包含的任意一项
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "zs"
    4) "a"
    5) "w"
    6) "k"
    7) "ss"
    
    127.0.0.1:6379> keys ?
    1) "a"
    2) "w"
    3) "k"
    
    127.0.0.1:6379> keys [wak]
    1) "a"
    2) "w"
    3) "k"
    
  • randomkey:抽取随机键

    127.0.0.1:6379> randomkey
    "key1"
    127.0.0.1:6379> randomkey
    "k"
    127.0.0.1:6379> randomkey
    "zs"
    
  • exists key:判断是否存在键key,返回1 或 0

    127.0.0.1:6379> exists k
    (integer) 1
    127.0.0.1:6379> exists pp
    (integer) 0
    
  • type key:判断键的类型

    127.0.0.1:6379> type k
    list
    
  • del key

    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "zs"
    4) "a"
    5) "w"
    6) "k"
    7) "ss"
    
    127.0.0.1:6379> del ss
    (integer) 1
    
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "zs"
    4) "a"
    5) "w"
    6) "k"
    
  • rename key newname

    # 正常情况下
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "zs"
    4) "a"
    5) "w"     <---
    6) "k"
    
    127.0.0.1:6379> rename w wa
    OK
    
    127.0.0.1:6379> keys *
    1) "key1"
    2) "wa"    <----
    3) "mykey"
    4) "zs"
    5) "a"
    6) "k"
    
    # 如果重命名与已有键名重回
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "a"   < -- hash
    4) "zs"
    5) "k"
    127.0.0.1:6379> type a
    hash
    127.0.0.1:6379> set aa "new"            <-- string
    OK
    127.0.0.1:6379> rename a aa
    OK
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "aa"                  <-- hash
    4) "zs"
    5) "k"
    127.0.0.1:6379> type aa
    hash
    
    • 重命名(new)与已有键(old)名重合,会覆盖重合的键(old),相当于将键(old)的类型改写为重命名键(new)的类型
  • renamenx key

    • 与rename一样,但当newkey存在时,不进行操作
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "aa"
    4) "zs"
    5) "k"
    
    127.0.0.1:6379> renamenx a aa
    (integer) 0
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "a"      <--没有改变
    4) "aa"
    5) "zs"
    6) "k"
    
  • select 数据库编号(默认使用0号数据库)

  • flushdb:清空数据

  • move key 数据库编号

    #默认0号数据库
    127.0.0.1:6379> set q 123
    OK
    127.0.0.1:6379> move q 1
    (integer) 1
    127.0.0.1:6379> keys *
    1) "key1"
    2) "mykey"
    3) "a"
    4) "aa"
    5) "zs"
    6) "k"          #没有q
    
    127.0.0.1:6379> select 1
    OK
    127.0.0.1:6379[1]> keys *
    (empty list or set)
    127.0.0.1:6379[1]> keys *
    1) "q"
    
  • ttl key:查询有效期

127.0.0.1:6379> ttl a
(integer) -1        -->永久有效
127.0.0.1:6379> ttl q
(integer) -2        -->不存在

  • expire key seconds:设置生命周期
    127.0.0.1:6379> expire o 123
    (integer) 1
    127.0.0.1:6379> ttl o
    (integer) 119
    
  • pexpire key ms:设置有效期,单位为毫秒
  • persist key:设置永久有效
    127.0.0.1:6379> ttl o
    (integer) 33
    127.0.0.1:6379> persist o
    (integer) 1
    127.0.0.1:6379> ttl o
    (integer) -1
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值