美文网首页
redis基本使用

redis基本使用

作者: Yi_Feng | 来源:发表于2017-11-16 21:47 被阅读0次

    redis安装与启动

    [root@C7mini ~]#yum install redis
    [root@C7mini ~]#rpm -ql redis
    /etc/logrotate.d/redis
    /etc/redis-sentinel.conf
    /etc/redis.conf
    /etc/systemd/system/redis-sentinel.service.d
    /etc/systemd/system/redis-sentinel.service.d/limit.conf
    /etc/systemd/system/redis.service.d
    /etc/systemd/system/redis.service.d/limit.conf
    /usr/bin/redis-benchmark
    /usr/bin/redis-check-aof
    /usr/bin/redis-check-rdb
    /usr/bin/redis-cli
    /usr/bin/redis-sentinel
    /usr/bin/redis-server
    /usr/bin/redis-shutdown
    /usr/lib/systemd/system/redis-sentinel.service
    /usr/lib/systemd/system/redis.service
    /usr/lib/tmpfiles.d/redis.conf
    /usr/share/doc/redis-3.2.3
    /usr/share/doc/redis-3.2.3/00-RELEASENOTES
    /usr/share/doc/redis-3.2.3/BUGS
    /usr/share/doc/redis-3.2.3/CONTRIBUTING
    /usr/share/doc/redis-3.2.3/MANIFESTO
    /usr/share/doc/redis-3.2.3/README.md
    /usr/share/licenses/redis-3.2.3
    /usr/share/licenses/redis-3.2.3/COPYING
    /var/lib/redis
    /var/log/redis
    /var/run/redis
    
    [root@C7mini ~]#systemctl start redis
    [root@C7mini ~]#ss -ntl
    State       Recv-Q Send-Q Local Address:Port                Peer Address:Port              
    LISTEN      0      128        127.0.0.1:6379                           *:*                  
    
    [root@C7mini ~]#ps aux
    redis      1694  0.1  0.5 142908  5784 ?        Ssl  13:12   0:05 /usr/bin/redis-server 127.0
    

    客户端工具Redis-cli

    [root@C7mini ~]#redis-cli -h
    redis-cli 3.2.3
    
    Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
      -h <hostname>      Server hostname (default: 127.0.0.1). ----------------------------- 指明连接主机,默认是本机
      -p <port>          Server port (default: 6379). -------------------------------------- 默认端口 6379
      -s <socket>        Server socket (overrides hostname and port). ---------------------- socket本地连接
      -a <password>      Password to use when connecting to the server. -------------------- 认证,没有多级授权,没有用户
      -r <repeat>        Execute specified command N times.
      -i <interval>      When -r is used, waits <interval> seconds per command.
                         It is possible to specify sub-second times like -i 0.1.
      -n <db>            Database number.
      -x                 Read last argument from STDIN.
      -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
      -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
      --raw              Use raw formatting for replies (default when STDOUT is
                         not a tty).
      --no-raw           Force formatted output even when STDOUT is not a tty.
      --csv              Output in CSV format.
      --stat             Print rolling stats about server: mem, clients, ...
      --latency          Enter a special mode continuously sampling latency.
      --latency-history  Like --latency but tracking latency changes over time.
                         Default time interval is 15 sec. Change it using -i.
      --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                         Default time interval is 1 sec. Change it using -i.
      --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.
      --slave            Simulate a slave showing commands received from the master.
      --rdb <filename>   Transfer an RDB dump from remote server to local file.
      --pipe             Transfer raw Redis protocol from stdin to server.
      --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                         no reply is received within <n> seconds.
                         Default timeout: 30. Use 0 to wait forever.
      --bigkeys          Sample Redis keys looking for big keys.
      --scan             List all keys using the SCAN command.
      --pattern <pat>    Useful with --scan to specify a SCAN pattern.
      --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                         The test will run for the specified amount of seconds.
      --eval <file>      Send an EVAL command using the Lua script at <file>.
      --ldb              Used with --eval enable the Redis Lua debugger.
      --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in
                         this mode the server is blocked and script changes are
                         are not rolled back from the server memory.
      --help             Output this help and exit.
      --version          Output version and exit.
    
    Examples:
      cat /etc/passwd | redis-cli -x set mypasswd
      redis-cli get mypasswd
      redis-cli -r 100 lpush mylist x
      redis-cli -r 100 -i 1 info | grep used_memory_human:
      redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
      redis-cli --scan --pattern '*:12345*'
    
      (Note: when using --eval the comma separates KEYS[] from ARGV[] items)
    
    When no command is given, redis-cli starts in interactive mode.
    Type "help" in interactive mode for information on available commands
    and settings.
    

    连接redis

    [root@C7mini ~]#redis-cli
    127.0.0.1:6379> 
    

    redis的数据表使用数字为表号,默认表为16个,默认表号0到15,可以使用SELECT查询,每个库自身是个索引,表号也是索引号

    [root@C7mini ~]#redis-cli
    127.0.0.1:6379> SELECT 0
    OK
    127.0.0.1:6379> SELECT 1
    OK
    127.0.0.1:6379[1]> SELECT 15
    OK
    127.0.0.1:6379[15]> SELECT 16
    (error) ERR invalid DB index
    127.0.0.1:6379[15]> 
    

    redis所有的数据都是键值对
    redis的帮助查询使用help加Tab键,Tab键切换不同的命令组。

    generic:通用命令,对所有的类都支持
    string:对字符串操作
    list:列表
    set:集合
    sorted_set:有序集合
    hash:字段
    pubsub:发布订阅队列
    transactions:事务
    connection:连接
    server:服务器
    scripting:脚本
    hyperloglog:
    cluster:集群
    geo:
    APPEND:索引
    

    string字符集

    127.0.0.1:6379[15]> help @string
    
      APPEND key value --------------------------------------------------------------------------- 在原有值进行追加
      summary: Append a value to a key
      since: 2.0.0
    
      BITCOUNT key [start end]
      summary: Count set bits in a string
      since: 2.6.0
    
      BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
      summary: Perform arbitrary bitfield integer operations on strings
      since: 3.2.0
    
      BITOP operation destkey key [key ...]
      summary: Perform bitwise operations between strings
      since: 2.6.0
    
      BITPOS key bit [start] [end]
      summary: Find first bit set or clear in a string
      since: 2.8.7
    
      DECR key --------------------------------------------------------------------------------- 减少
      summary: Decrement the integer value of a key by one
      since: 1.0.0
    
      DECRBY key decrement --------------------------------------------------------------------- 减少多个
      summary: Decrement the integer value of a key by the given number
      since: 1.0.0
    
      GET key ---------------------------------------------------------------------------------- 获取一个键值对
      summary: Get the value of a key
      since: 1.0.0
    
      GETBIT key offset
      summary: Returns the bit value at offset in the string value stored at key
      since: 2.2.0
    
      GETRANGE key start end
      summary: Get a substring of the string stored at a key
      since: 2.4.0
    
      GETSET key value
      summary: Set the string value of a key and return its old value
      since: 1.0.0
    
      INCR key -------------------------------------------------------------------------------- 增加
      summary: Increment the integer value of a key by one
      since: 1.0.0
    
      INCRBY key increment -------------------------------------------------------------------- 增加多个
      summary: Increment the integer value of a key by the given amount
      since: 1.0.0
    
      INCRBYFLOAT key increment
      summary: Increment the float value of a key by the given amount
      since: 2.6.0
    
      MGET key [key ...] ----------------------------------------------------------------- 获取多个键值对
      summary: Get the values of all the given keys
      since: 1.0.0
    
      MSET key value [key value ...] ----------------------------------------------------- 一次创建多个键值对
      summary: Set multiple keys to multiple values
      since: 1.0.1
    
      MSETNX key value [key value ...]
      summary: Set multiple keys to multiple values, only if none of the keys exist
      since: 1.0.1
    
      PSETEX key milliseconds value
      summary: Set the value and expiration in milliseconds of a key
      since: 2.6.0
    
      SET key value [EX seconds] [PX milliseconds] [NX|XX] ------------------------------ 创建一个键值对
      summary: Set the string value of a key
      since: 1.0.0
    
      SETBIT key offset value
      summary: Sets or clears the bit at offset in the string value stored at key
      since: 2.2.0
    
      SETEX key seconds value ----------------------------------------------------------- 定义过期时间
      summary: Set the value and expiration of a key
      since: 2.0.0
    
      SETNX key value ------------------------------------------------------------------- 新建
      summary: Set the value of a key, only if the key does not exist
      since: 1.0.0
    
      SETRANGE key offset value --------------------------------------------------------- 修改一组的key值
      summary: Overwrite part of a string at key starting at the specified offset
      since: 2.2.0
    
      STRLEN key ----------------------------------------------------------------------- 判定一个键的字符串的长度
      summary: Get the length of the value stored in a key
      since: 2.2.0
    

    示例

    127.0.0.1:6379[15]> SET mykey 'hello redis'          创建一个键值对mykey,内容为hello redis
    OK
    127.0.0.1:6379[15]> GET mykey                        获取mykey查看
    "hello redis"
    127.0.0.1:6379[15]> APPEND mykey ',www.redis.io'     在mykey附加www.redis.io
    (integer) 24
    127.0.0.1:6379[15]> GET mykey                        查看mykey
    "hello redis,www.redis.io"
    127.0.0.1:6379[15]> STRLEN mykey                     查看mykey的字符长度
    (integer) 24
    127.0.0.1:6379[15]> SETNX mykey 'hi redis'           新建一个mykey
    (integer) 0
    127.0.0.1:6379[15]> GET mykey                        新建不成功,未能覆盖mykey
    "hello redis,www.redis.io"
    127.0.0.1:6379[15]> SET count 0                      创建一个键值对count,定义内容为0
    OK
    127.0.0.1:6379[15]> INCR count                       对count自增加,加1
    (integer) 1
    127.0.0.1:6379[15]> INCR count
    (integer) 2
    127.0.0.1:6379[15]> INCR count
    (integer) 3
    127.0.0.1:6379[15]> INCRBY count 2                   设定增加大小为2
    (integer) 5
    127.0.0.1:6379[15]> INCRBY count 2
    (integer) 7
    127.0.0.1:6379[15]> DECR count                       对count自减少,减1
    (integer) 6
    127.0.0.1:6379[15]> DECR count 
    (integer) 5
    127.0.0.1:6379[15]> DECRBY count 3                   设定减少大小为3
    (integer) 2
    127.0.0.1:6379[15]> DEL mykey                        删除mykey
    (integer) 1
    127.0.0.1:6379[15]> GET mykey                        查看删除情况
    (nil)
    

    list列表,列表即数组,L开头left,R开头right,L表示从左侧入站,R表示从右侧入站

    127.0.0.1:6379[15]> help @list
    
      BLPOP key [key ...] timeout -------------------------------------------------- 移除拿到列表的第一个数据,没有元素就阻塞,等待元素插入。当队列使用才有用
      summary: Remove and get the first element in a list, or block until one is available
      since: 2.0.0
    
      BRPOP key [key ...] timeout
      summary: Remove and get the last element in a list, or block until one is available
      since: 2.0.0
    
      BRPOPLPUSH source destination timeout --------------------------------------- 从一个列表的最右侧去一个数据放在另一个列表的最左侧,队列有用
      summary: Pop a value from a list, push it to another list and return it; or block until one is available
      since: 2.2.0
    
      LINDEX key index ----------------------------------------------------------- 按索引取数据
      summary: Get an element from a list by its index
      since: 1.0.0
    
      LINSERT key BEFORE|AFTER pivot value---------------------------------------- 指定位置插入
      summary: Insert an element before or after another element in a list
      since: 2.2.0
    
      LLEN key ------------------------------------------------------------------- 取出整个列表的长度(元素个数)
      summary: Get the length of a list
      since: 1.0.0
    
      LPOP key ------------------------------------------------------------------- 从左侧出站,弹站
      summary: Remove and get the first element in a list
      since: 1.0.0
    
      LPUSH key value [value ...] ------------------------------------------------ 从左侧入站,压站,自动创建列表
      summary: Prepend one or multiple values to a list
      since: 1.0.0
    
      LPUSHX key value ----------------------------------------------------------- 只有列表存在时入站
      summary: Prepend a value to a list, only if the list exists
      since: 2.2.0
    
      LRANGE key start stop ----------------------------------------------------- 获取指定范围内的所有元素
      summary: Get a range of elements from a list
      since: 1.0.0
    
      LREM key count value ------------------------------------------------------- 删除一个元素
      summary: Remove elements from a list
      since: 1.0.0
    
      LSET key index value ------------------------------------------------------- 设定指定索引的元素的值
      summary: Set the value of an element in a list by its index
      since: 1.0.0
    
      LTRIM key start stop ------------------------------------------------------- 修改指定范围的所有元素
      summary: Trim a list to the specified range
      since: 1.0.0
    
      RPOP key
      summary: Remove and get the last element in a list
      since: 1.0.0
    
      RPOPLPUSH source destination
      summary: Remove the last element in a list, prepend it to another list and return it
      since: 1.2.0
    
      RPUSH key value [value ...]
      summary: Append one or multiple values to a list
      since: 1.0.0
    
      RPUSHX key value
      summary: Append a value to a list, only if the list exists
      since: 2.2.0
    

    示例

    
    127.0.0.1:6379[15]> LPUSH weekdays Sat ----------------------- 创建一个列表,定义第一个字符为Sat
    (integer) 1
    127.0.0.1:6379[15]> LPUSH weekdays Fri ----------------------- 从左边定义新的字符为Fri
    (integer) 2
    127.0.0.1:6379[15]> LPUSH weekdays Thu ----------------------- 从左边定义新的字符为Thu
    (integer) 3
    127.0.0.1:6379[15]> LINDEX weekdays 0 ------------------------ 查看左边第一个字符
    "Thu"
    127.0.0.1:6379[15]> LINDEX weekdays 2 ------------------------ 查看左边第三个字符
    "Sat"
    127.0.0.1:6379[15]> LPUSHX weekdays Tue ---------------------- 从左边定义新的字符Tue,没有表的情况下不创建
    (integer) 4
    127.0.0.1:6379[15]> LINSERT weekdays BEFORE Thu Wed ---------- 在字符Thu之前插入字符Wed
    (integer) 5
    127.0.0.1:6379[15]> LRANGE weekdays 0 4 ---------------------- 查看表
    1) "Tue"
    2) "Wed"
    3) "Thu"
    4) "Fri"
    5) "Sat"
    127.0.0.1:6379[15]> LPOP weekdays --------------------------- 从左边开始弹出第一个字符
    "Tue"
    127.0.0.1:6379[15]> LRANGE weekdays 0 4 --------------------- 查看表
    1) "Wed"
    2) "Thu"
    3) "Fri"
    4) "Sat"
    127.0.0.1:6379[15]> RPOP weekdays -------------------------- 从右边开始弹出第一个字符
    "Sat"
    127.0.0.1:6379[15]> LRANGE weekdays 0 4 -------------------- 查看表 
    1) "Wed"
    2) "Thu"
    3) "Fri"
    127.0.0.1:6379[15]> LLEN weekdays ------------------------- 查看表的字符长度
    (integer) 3
    

    hash字段,很多命令是由H开头

    127.0.0.1:6379[15]> help @hash
    
      HDEL key field [field ...] -------------------------------------------------- 删除某个键中的子键
      summary: Delete one or more hash fields
      since: 2.0.0
    
      HEXISTS key field ----------------------------------------------------------- 判断某个键中子键是否存在
      summary: Determine if a hash field exists
      since: 2.0.0
    
      HGET key field
      summary: Get the value of a hash field
      since: 2.0.0
    
      HGETALL key ----------------------------------------------------------------- 获取所有的键值对
      summary: Get all the fields and values in a hash
      since: 2.0.0
    
      HINCRBY key field increment
      summary: Increment the integer value of a hash field by the given number
      since: 2.0.0
    
      HINCRBYFLOAT key field increment
      summary: Increment the float value of a hash field by the given amount
      since: 2.6.0
    
      HKEYS key ------------------------------------------------------------------ 获取一个键中的所有子key
      summary: Get all the fields in a hash
      since: 2.0.0
    
      HLEN key ------------------------------------------------------------------- 查看表中元素的个数
      summary: Get the number of fields in a hash
      since: 2.0.0
    
      HMGET key field [field ...]
      summary: Get the values of all the given hash fields
      since: 2.0.0
    
      HMSET key field value [field value ...] -------------------------------------- 设定或修改多个键值对
      summary: Set multiple hash fields to multiple values
      since: 2.0.0
    
      HSCAN key cursor [MATCH pattern] [COUNT count]
      summary: Incrementally iterate hash fields and associated values
      since: 2.8.0
    
      HSET key field value ------------------------------------------------------- 设定或改键值对
      summary: Set the string value of a hash field
      since: 2.0.0
    
      HSETNX key field value
      summary: Set the value of a hash field, only if the field does not exist
      since: 2.0.0
    
      HSTRLEN key field
      summary: Get the length of the value of a hash field
      since: 3.2.0
    
      HVALS key ------------------------------------------------------------------- 获取所有的值
      summary: Get all the values in a hash
      since: 2.0.0
    
    127.0.0.1:6379[15]> HMSET member name jerry age 17 gender female -------- 创建键值对
    OK
    127.0.0.1:6379[15]> HKEYS member ---------------------------------------- 查询所有键
    1) "name"
    2) "age"
    3) "gender"
    127.0.0.1:6379[15]> HVALS member ---------------------------------------- 查询所有值
    1) "jerry"
    2) "17"
    3) "female"
    127.0.0.1:6379[15]> HSTRLEN member name -------------------------------- 查看member表的name的字符长度
    (integer) 5
    127.0.0.1:6379[15]> HGETALL member
    1) "name"
    2) "jerry"
    3) "age"
    4) "17"
    5) "gender"
    6) "female"
    127.0.0.1:6379[15]> HDEL member gender ------------------------------- 删除键值对gender
    (integer) 1
    127.0.0.1:6379[15]> HGETALL member ----------------------------------- 查看所有键值对
    1) "name"
    2) "jerry"
    3) "age"
    4) "17"
    

    set集合

    127.0.0.1:6379[15]> help @set
    
      SADD key member [member ...] -------------------------------------- 向一个集合内添加一个成员或者多个成员
      summary: Add one or more members to a set
      since: 1.0.0
    
      SCARD key --------------------------------------------------------- 得到一个集合内的所有元素的个数
      summary: Get the number of members in a set
      since: 1.0.0
    
      SDIFF key [key ...] ---------------------------------------------------- 计算两个集合的差集
      summary: Subtract multiple sets
      since: 1.0.0
    
      SDIFFSTORE destination key [key ...] ---------------------------------- 计算两个集合的差集并存档
      summary: Subtract multiple sets and store the resulting set in a key
      since: 1.0.0
    
      SINTER key [key ...] --------------------------------------------------- 计算两个集合的交集
      summary: Intersect multiple sets
      since: 1.0.0
    
      SINTERSTORE destination key [key ...] ---------------------------------- 计算两个集合的交集并存档
      summary: Intersect multiple sets and store the resulting set in a key
      since: 1.0.0
    
      SISMEMBER key member --------------------------------------------------- 判断是否是集合的成员
      summary: Determine if a given value is a member of a set
      since: 1.0.0
    
      SMEMBERS key ----------------------------------------------------------- 获取一个集合的所有元素
      summary: Get all the members in a set
      since: 1.0.0
    
      SMOVE source destination member
      summary: Move a member from one set to another
      since: 1.0.0
    
      SPOP key [count] ------------------------------------------------------ 从集合中随机删除一个元素
      summary: Remove and return one or multiple random members from a set
      since: 1.0.0
    
      SRANDMEMBER key [count] ----------------------------------------------- 从集合中随机删除一个元素
      summary: Get one or multiple random members from a set
      since: 1.0.0
    
      SREM key member [member ...] ------------------------------------------ 指明删除某个元素
      summary: Remove one or more members from a set
      since: 1.0.0
    
      SSCAN key cursor [MATCH pattern] [COUNT count]
      summary: Incrementally iterate Set elements
      since: 2.8.0
    
      SUNION key [key ...] -------------------------------------------------- 计算两个集合的并集
      summary: Add multiple sets
      since: 1.0.0
    
      SUNIONSTORE destination key [key ...] --------------------------------- 计算两个集合的并集并存档
      summary: Add multiple sets and store the resulting set in a key
      since: 1.0.0
    

    示例

    127.0.0.1:6379[15]> SADD animals elephant wolf tiger monkey fox dog cat ---- 创建一个集合animals
    (integer) 7
    127.0.0.1:6379[15]> SADD jiaqin dog pig chicken duck fish ------------------ 创建一个集合jiaqin
    (integer) 5
    127.0.0.1:6379[15]> SMEMBERS animals --------------------------------------- 查看集合animals
    1) "wolf"
    2) "tiger"
    3) "monkey"
    4) "dog"
    5) "elephant"
    6) "fox"
    7) "cat"
    127.0.0.1:6379[15]> SMEMBERS jiaqin --------------------------------------- 查看集合jiaqin
    1) "duck"
    2) "dog"
    3) "chicken"
    4) "pig"
    5) "fish"
    127.0.0.1:6379[15]> SCARD animals ----------------------------------------- 查看集合animals内的元素的数量
    (integer) 7
    127.0.0.1:6379[15]> SPOP animals ------------------------------------------ 集合animals随机删除一个元素
    "cat"
    127.0.0.1:6379[15]> SREM jiaqin fish -------------------------------------- 集合jiaqin指定删除fish
    (integer) 1
    127.0.0.1:6379[15]> SMEMBERS jiaqin --------------------------------------- 查看集合jiaqin
    1) "chicken"
    2) "pig"
    3) "duck"
    4) "dog"
    127.0.0.1:6379[15]> SINTER animals jiaqin --------------------------------- 查看animals和jiaqin的交集
    1) "dog"
    127.0.0.1:6379[15]> SDIFF animals jiaqin ---------------------------------- 查看animals和jiaqin的差集,与集合的先后有关
    1) "elephant"
    2) "wolf"
    3) "monkey"
    4) "tiger"
    5) "fox"
    127.0.0.1:6379[15]> SDIFF jiaqin animals
    1) "duck"
    2) "chicken"
    3) "pig"
    127.0.0.1:6379[15]> SUNION animals jiaqin -------------------------------- 查看animals和jiaqin的并集
    1) "duck"
    2) "wolf"
    3) "tiger"
    4) "monkey"
    5) "fox"
    6) "dog"
    7) "elephant"
    8) "pig"
    9) "chicken"
    

    sorted_set有序集合

    127.0.0.1:6379[15]> help @sorted_set
    
      ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
      summary: Add one or more members to a sorted set, or update its score if it already exists
      since: 1.2.0
    
      ZCARD key
      summary: Get the number of members in a sorted set
      since: 1.2.0
    
      ZCOUNT key min max
      summary: Count the members in a sorted set with scores within the given values
      since: 2.0.0
    
      ZINCRBY key increment member
      summary: Increment the score of a member in a sorted set
      since: 1.2.0
    
      ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
      summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
      since: 2.0.0
    
      ZLEXCOUNT key min max
      summary: Count the number of members in a sorted set between a given lexicographical range
      since: 2.8.9
    
      ZRANGE key start stop [WITHSCORES]
      summary: Return a range of members in a sorted set, by index
      since: 1.2.0
    
      ZRANGEBYLEX key min max [LIMIT offset count]
      summary: Return a range of members in a sorted set, by lexicographical range
      since: 2.8.9
    
      ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
      summary: Return a range of members in a sorted set, by score
      since: 1.0.5
    
      ZRANK key member
      summary: Determine the index of a member in a sorted set
      since: 2.0.0
    
      ZREM key member [member ...]
      summary: Remove one or more members from a sorted set
      since: 1.2.0
    
      ZREMRANGEBYLEX key min max
      summary: Remove all members in a sorted set between the given lexicographical range
      since: 2.8.9
    
      ZREMRANGEBYRANK key start stop
      summary: Remove all members in a sorted set within the given indexes
      since: 2.0.0
    
      ZREMRANGEBYSCORE key min max
      summary: Remove all members in a sorted set within the given scores
      since: 1.2.0
    
      ZREVRANGE key start stop [WITHSCORES]
      summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
      since: 1.2.0
    
      ZREVRANGEBYLEX key max min [LIMIT offset count]
      summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
      since: 2.8.9
    
      ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
      summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
      since: 2.2.0
    
      ZREVRANK key member
      summary: Determine the index of a member in a sorted set, with scores ordered from high to low
      since: 2.0.0
    
      ZSCAN key cursor [MATCH pattern] [COUNT count]
      summary: Incrementally iterate sorted sets elements and associated scores
      since: 2.8.0
    
      ZSCORE key member
      summary: Get the score associated with the given member in a sorted set
      since: 1.2.0
    
      ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
      summary: Add multiple sorted sets and store the resulting sorted set in a new key
      since: 2.0.0
    

    示例

    127.0.0.1:6379[15]> ZADD employees 1 tom 3 jerry 9 abama 7 trump 4 bush ---- 创建一个有序数组表
    (integer) 5
    127.0.0.1:6379[15]> ZSCORE employees trump --------------------------------- 查看trump的值
    "7"
    127.0.0.1:6379[15]> ZRANK employees trump ---------------------------------- 查看trump的suoyin
    (integer) 3
    127.0.0.1:6379[15]> ZRANK employees bush ----------------------------------- 查看bush的suoyin
    (integer) 2
    127.0.0.1:6379[15]> ZRANGE employees 1 3 ----------------------------------- 查看索引为1到3的名单
    1) "jerry"
    2) "bush"
    3) "trump"
    127.0.0.1:6379[15]> ZRANGEBYSCORE employees 3 10 --------------------------- 按照得分3到10的顺序排列
    1) "jerry"
    2) "bush"
    3) "trump"
    4) "abama"
    

    pubsub发布订阅队列,一个队列相当于一个频道,有人不断在往里面发送数据,有人不断从外面取得数据,一个人订阅了一个频道,订阅者就能接受到频道的更新信息。

    127.0.0.1:6379[15]> help @pubsub
    
      PSUBSCRIBE pattern [pattern ...] ----------------------------------------------- 基于模式定阅
      summary: Listen for messages published to channels matching the given patterns
      since: 2.0.0
    
      PUBLISH channel message ------------------------------------------------------- 向一个频道放入一则消息
      summary: Post a message to a channel
      since: 2.0.0
    
      PUBSUB subcommand [argument [argument ...]]---------------------------------- 获取指定发布队列的信息
      summary: Inspect the state of the Pub/Sub subsystem
      since: 2.8.0
    
      PUNSUBSCRIBE [pattern [pattern ...]] ---------------------------------------- 基于模式取消定阅
      summary: Stop listening for messages posted to channels matching the given patterns
      since: 2.0.0
    
      SUBSCRIBE channel [channel ...] --------------------------------------------- 定阅频道
      summary: Listen for messages published to the given channels
      since: 2.0.0
    
      UNSUBSCRIBE [channel [channel ...]] ----------------------------------------- 取消定阅
      summary: Stop listening for messages posted to the given channels
      since: 2.0.0
    

    示例

    本机ssh 1:发布
    127.0.0.1:6379[15]> PUBLISH ops 'kernel is come'
    (integer) 1
    127.0.0.1:6379[15]> PUBLISH ops 'kernel is new'
    (integer) 1
    [root@C7mini ~]#redis-cli
    本机ssh 2:定阅、接收
    127.0.0.1:6379> SUBSCRIBE ops dev
    Reading messages... (press Ctrl-C to quit)
    1) "subscribe"
    2) "ops"
    3) (integer) 1
    1) "subscribe"
    2) "dev"
    3) (integer) 2
    1) "message"
    2) "ops"
    3) "kernel is come"
    1) "message"
    2) "ops"
    3) "kernel is new"
    

    相关文章

      网友评论

          本文标题:redis基本使用

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