redis基本使用

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). ----------------------------- 指明連接主機甘磨,默認(rèn)是本機
  -p <port>          Server port (default: 6379). -------------------------------------- 默認(rèn)端口 6379
  -s <socket>        Server socket (overrides hostname and port). ---------------------- socket本地連接
  -a <password>      Password to use when connecting to the server. -------------------- 認(rèn)證廓鞠,沒有多級授權(quán),沒有用戶
  -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的數(shù)據(jù)表使用數(shù)字為表號浇借,默認(rèn)表為16個事秀,默認(rèn)表號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所有的數(shù)據(jù)都是鍵值對
redis的幫助查詢使用help加Tab鍵瓦糟,Tab鍵切換不同的命令組筒愚。

generic:通用命令,對所有的類都支持
string:對字符串操作
list:列表
set:集合
sorted_set:有序集合
hash:字段
pubsub:發(fā)布訂閱隊列
transactions:事務(wù)
connection:連接
server:服務(wù)器
scripting:腳本
hyperloglog:
cluster:集群
geo:
APPEND:索引

string字符集

127.0.0.1:6379[15]> help @string

  APPEND key value --------------------------------------------------------------------------- 在原有值進(jìn)行追加
  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 ...] ----------------------------------------------------- 一次創(chuàng)建多個鍵值對
  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] ------------------------------ 創(chuàng)建一個鍵值對
  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'          創(chuàng)建一個鍵值對mykey菩浙,內(nèi)容為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                      創(chuàng)建一個鍵值對count,定義內(nèi)容為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                   設(shè)定增加大小為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                   設(shè)定減少大小為3
(integer) 2
127.0.0.1:6379[15]> DEL mykey                        刪除mykey
(integer) 1
127.0.0.1:6379[15]> GET mykey                        查看刪除情況
(nil)

list列表,列表即數(shù)組先嬉,L開頭left轧苫,R開頭right,L表示從左側(cè)入站疫蔓,R表示從右側(cè)入站

127.0.0.1:6379[15]> help @list

  BLPOP key [key ...] timeout -------------------------------------------------- 移除拿到列表的第一個數(shù)據(jù)含懊,沒有元素就阻塞,等待元素插入衅胀。當(dāng)隊列使用才有用
  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 --------------------------------------- 從一個列表的最右側(cè)去一個數(shù)據(jù)放在另一個列表的最左側(cè)岔乔,隊列有用
  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 ----------------------------------------------------------- 按索引取數(shù)據(jù)
  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 ------------------------------------------------------------------- 取出整個列表的長度(元素個數(shù))
  summary: Get the length of a list
  since: 1.0.0

  LPOP key ------------------------------------------------------------------- 從左側(cè)出站,彈站
  summary: Remove and get the first element in a list
  since: 1.0.0

  LPUSH key value [value ...] ------------------------------------------------ 從左側(cè)入站滚躯,壓站雏门,自動創(chuàng)建列表
  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 ----------------------------------------------------- 獲取指定范圍內(nèi)的所有元素
  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 ------------------------------------------------------- 設(shè)定指定索引的元素的值
  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 ----------------------- 創(chuàng)建一個列表,定義第一個字符為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掸掏,沒有表的情況下不創(chuàng)建
(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 ------------------------------------------------------------------- 查看表中元素的個數(shù)
  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 ...] -------------------------------------- 設(shè)定或修改多個鍵值對
  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 ------------------------------------------------------- 設(shè)定或改鍵值對
  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 -------- 創(chuàng)建鍵值對
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 ...] -------------------------------------- 向一個集合內(nèi)添加一個成員或者多個成員
  summary: Add one or more members to a set
  since: 1.0.0

  SCARD key --------------------------------------------------------- 得到一個集合內(nèi)的所有元素的個數(shù)
  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 ---- 創(chuàng)建一個集合animals
(integer) 7
127.0.0.1:6379[15]> SADD jiaqin dog pig chicken duck fish ------------------ 創(chuàng)建一個集合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內(nèi)的元素的數(shù)量
(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的差集,與集合的先后有關(guān)
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 ---- 創(chuàng)建一個有序數(shù)組表
(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發(fā)布訂閱隊列丧凤,一個隊列相當(dāng)于一個頻道募闲,有人不斷在往里面發(fā)送數(shù)據(jù),有人不斷從外面取得數(shù)據(jù)愿待,一個人訂閱了一個頻道浩螺,訂閱者就能接受到頻道的更新信息。

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 ...]]---------------------------------- 獲取指定發(fā)布隊列的信息
  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:發(fā)布
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"
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末年扩,一起剝皮案震驚了整個濱河市蚁廓,隨后出現(xiàn)的幾起案子访圃,更是在濱河造成了極大的恐慌,老刑警劉巖相嵌,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件腿时,死亡現(xiàn)場離奇詭異况脆,居然都是意外死亡,警方通過查閱死者的電腦和手機批糟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門格了,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人徽鼎,你說我怎么就攤上這事盛末。” “怎么了否淤?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵悄但,是天一觀的道長。 經(jīng)常有香客問我石抡,道長檐嚣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任啰扛,我火速辦了婚禮嚎京,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘隐解。我一直安慰自己鞍帝,他們只是感情好谊迄,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布意狠。 她就那樣靜靜地躺著,像睡著了一般脚牍。 火紅的嫁衣襯著肌膚如雪溜嗜。 梳的紋絲不亂的頭發(fā)上宵膨,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機與錄音炸宵,去河邊找鬼辟躏。 笑死,一個胖子當(dāng)著我的面吹牛土全,可吹牛的內(nèi)容都是我干的捎琐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼裹匙,長吁一口氣:“原來是場噩夢啊……” “哼瑞凑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起概页,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤籽御,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體技掏,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡铃将,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了哑梳。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劲阎。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖鸠真,靈堂內(nèi)的尸體忽然破棺而出悯仙,到底是詐尸還是另有隱情,我是刑警寧澤吠卷,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布雁比,位于F島的核電站,受9級特大地震影響撤嫩,放射性物質(zhì)發(fā)生泄漏偎捎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一序攘、第九天 我趴在偏房一處隱蔽的房頂上張望茴她。 院中可真熱鬧,春花似錦程奠、人聲如沸丈牢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽己沛。三九已至,卻和暖如春距境,著一層夾襖步出監(jiān)牢的瞬間申尼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工垫桂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留师幕,地道東北人。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓诬滩,卻偏偏與公主長得像霹粥,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子疼鸟,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Redis介紹 Redis是什么 REmote DIctionary Server(Redis) 是一個由Salv...
    haoxilu閱讀 5,972評論 0 10
  • 什么是redis 鍵值類型 String字符類型 map散列類型 list列表類型 set 集合類型 有序集合類型...
    持續(xù)進(jìn)步者閱讀 384評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理后控,服務(wù)發(fā)現(xiàn),斷路器空镜,智...
    卡卡羅2017閱讀 134,665評論 18 139
  • 1.下載 進(jìn)入官方下載下載穩(wěn)定版本的redis. 2.解壓安裝包到指定目錄(個人喜好而定浩淘,我的放到了文稿/User...
    南飛孤鴻閱讀 6,747評論 0 3
  • 精彩語錄 ■九幽陰靈矾利,諸天神魔。以我血軀馋袜,奉為犧牲。三生七世舶斧,永墮閻羅欣鳖。只為情故,雖死不悔茴厉。 ■我摘了這花泽台,便是這...
    hx78閱讀 437評論 0 1