zoukankan      html  css  js  c++  java
  • Redis实战

    Redis 键(key)

    Redis 键命令用于管理 redis 的键。

    DEL key

    该命令用于在 key 存在时删除 key。

    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

    DUMP key
    序列化给定 key ,并返回被序列化的值。

    返回值
    如果 key 不存在,那么返回 nil 。 否则,返回序列化之后的值。

    127.0.0.1:6379> SET greeting "hello, dumping world!"
    OK
    127.0.0.1:6379> dump greeting
    "x00x15hello, dumping world!ax00,x7fxe7xf1%xed(W"
    127.0.0.1:6379> DUMP not-exists-key
    (nil)
    127.0.0.1:6379>
    

    EXISTS

    EXISTS key
    检查给定 key 是否存在。

    返回值
    若 key 存在返回 1 ,否则返回 0 。

    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

    EXPIRE key seconds
    为给定 key 设置过期时间,以秒计。

    返回值
    设置成功返回 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分钟后该键会自动删除。

    EXPIREAT

    EXPIREAT key timestamp
    EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(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> EXPIREAT runoobkey 1293840000
    (integer) 1
    redis 127.0.0.1:6379> EXISTS runoobkey
    (integer) 0
    

    PEXPIRE

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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"
    

    RENAME 与 RENAMENX 区别

    相同:
    newkey 不存在
    RENAMERENAMENX一样:oldkey删除
    不同:
    newkey 已存在时
    RENAME 会覆盖旧 newkey,RENAMENX 失败。

    TYPE

    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)

  • 相关阅读:
    C++ 虚函数表解析
    C#编写简单的聊天程序
    c#事件与委托
    c#文本控件实现换行
    docker 详细安装及问题排查
    hadoop命令行
    Spark中的多线程并发处理
    CDH6.1.0离线安装——笔记
    linux 常用命令
    Rsync 恢复 libselinux.SO.1
  • 原文地址:https://www.cnblogs.com/tangge/p/10706694.html
Copyright © 2011-2022 走看看