Redis 基礎

一、獲得鍵名列表
127.0.0.1:6379> KEYS *
(empty list or set)
127.0.0.1:6379> set bar 1
OK
127.0.0.1:6379> KEYS *
1) "bar"

KEYS ×列出redis所有的鍵,由於數據庫開始並沒有任何鍵所以爲空。在添加bar後再使用KEYS就可以列出bar這個鍵
判斷key是否存在:

127.0.0.1:6379> EXISTS bar
(integer) 1
127.0.0.1:6379> EXISTS nobar
(integer) 0

刪除key:
127.0.0.1:6379> DEL bar
(integer) 1
127.0.0.1:6379> DEL bar
(integer) 0
返回1是成功,0是由於key值不存在。
利用xargs技巧:
redis-cli KEYS "user:*" | xargs redis-cli DEL,這句性能並不是是否理想,性能更好的刪除語句:
redis-cli DEL `redis-cli KEYS "user:*"`

獲得key數據類型:
127.0.0.1:6379> SET foo 1
127.0.0.1:6379> TYPE foo
string
127.0.0.1:6379> LPUSH bar 1
127.0.0.1:6379> TYPE bar
list


字符串:
賦值與取值:
SET key value
GET key
遞增:INCR key
增加指定的整數:INCRBY key number
減少指定數:DECR key (減一) , DECRBY key number

增加指定浮點數: INCREBYFLOAT key increment
向尾部追加值: APPEND key value

獲得字符串長度 STRLEN key
同時獲得多個鍵值:MGET key [key ...]
MSET key value [key value ... ]

127.0.0.1:6379> MSET key1 hello key2 world key3 ray
OK
127.0.0.1:6379> GET key2
"world"
127.0.0.1:6379> MGET key1 key2 key3
1) "hello"
2) "world"
3) "ray"



INCR:            INCR key-name 將鍵存儲的值加1
DECR:            DECR key-name 將存儲的值減1
INCRBY:            INCRBY key-name amount 將鍵存儲的值加上一個整數amount
DECRBY:            DECRBY key-name amount 將鍵存儲的值減去整數amount
INCRBYFLOAT:    INCRBYFLOAT key-name amount 將鍵存儲的值加上浮點輸amount




位操作:GETBIT key offset
SETBIT  key offset value
BITCOUNT key start end
BITOP    operation destkey key [key...]

APPEND:            APPEND key-name value 將值value 追加到給點的key-name當前存儲的值的末尾
GETRANGE:        GETRANGE key-name start end 獲取一個由偏移量start至end範圍內所有字符串組成的子串,包括start和end在內
SETRANGE:        SETRANGE key-name offset value 將從start偏移開始的子串設置爲給定值
GETBIT:            GETBIT key-name offset 將字節串看作是二進制串(bit string)並返回位串中偏移量爲offset的二進制位的值
SETBIT:            SETBIT key-name offset value 將字節串看作是二進制位串,並將位串中偏移爲offset的二進制位的值設置爲value
BITCOUNT:        BITCOUNT key-name start end 統計二進制位串裏值爲1的二進制位的數量,如果指定了可選的start和end偏移量那麼只對偏移量範圍內的二進制位進行統計
BITOP:            BITOP operation dest-key kay-name  對一個或多個二進制位串執行:AND,OR,XOR,NOT在內的任意一種按位運算操作。並將計算結果保存到dest-key



例子:

127.0.0.1:6379[3]> APPEND ns 'hello'
(integer) 5
127.0.0.1:6379[3]> APPEND ns 'world'
(integer) 10
127.0.0.1:6379[3]> GETRANGE ns 3 7
"lowor"
127.0.0.1:6379[3]> SETRANGE ns 6 'W'
(integer) 10
127.0.0.1:6379[3]> SETRANGE ns 0 'H'
(integer) 10
127.0.0.1:6379[3]> get ns
"HellowWrld"
127.0.0.1:6379[3]> SETBIT ns2 2 1
(integer) 0
127.0.0.1:6379[3]> SETBIT ns2 7 1
(integer) 0
127.0.0.1:6379[3]> get ns2



二、散列:
賦值與取值:
HSET key field value
HGET key filed    
HMSET key field value [filed value ... ]
HMGET key field [field ... ]
HGETALL key

127.0.0.1:6379> HSET car price 500
(integer) 1
127.0.0.1:6379> HSET car name BMW
(integer) 1
127.0.0.1:6379> HGET car name
"BMW"
127.0.0.1:6379> HGET car price
"500"

設置多個鍵值:
HMSET  key field1 value1 filed2 value2
HMGET獲得多個字段值:
127.0.0.1:6379> HMGET car price name
1) "500"
2) "BMW"
HMGETALL car 獲取所有字段和字段值
127.0.0.1:6379> HGETALL car
1) "price"
2) "500"
3) "name"
4) "BMW"

判斷字段是否存在: HEXISTS key field
127.0.0.1:6379> HEXISTS car model
(integer) 0
127.0.0.1:6379> HSET car model c200
(integer) 1
127.0.0.1:6379> HEXISTS car model
(integer) 1

字段不存在時賦值: HSETNX key field value ,如果字段存在,HSETNX將不執行任何操作。
127.0.0.1:6379> HSETNX car model e400
(integer) 0
127.0.0.1:6379> HGET car model
"c200"

增加數字:HINCRBY key field increment
刪除字段: HDEL key filed [filed ... ]

命令拾遺:
之獲取字段或字段值:
HKEYS key
HVALS key
HVALS 獲取鍵中所有的字段值,例如:
127.0.0.1:6379> HVALS car
1) "500"
2) "BMW"
3) "c200"

獲得字段數量:HLEN key

列表類型:
左邊增加元素:    LPUSH key value [value ... ]
右邊增加元素:    RPUSH key value [value ... ]
127.0.0.1:6379> LPUSH numbers 1
(integer) 1
127.0.0.1:6379> LPUSH numbers 2 3
(integer) 3
127.0.0.1:6379> RPUSH numbers 0 -1
(integer) 5
numbers 數據如下:
[ 3 2 1 0 -1 ]

從列表兩端彈出元素:
LPOP key,彈出左邊第一個元素
RPOP key,彈出右邊第一個元素

LEN key 獲取列表元素中的元素個數
LRANGE key start stop , 獲得列表片段(從0開始),stop值大於實際範圍則會返回右邊所有元素
127.0.0.1:6379> LRANGE numbers 0 2
1) "2"
2) "1"
3) "0"
127.0.0.1:6379> LRANGE numbers 0 200
1) "2"
2) "1"
3) "0"

刪除列表中指定的值:
LREM key count value,刪除前count個值爲value的元素。當count大於0時,LREM從列表左邊開始刪除,當count小於0時,LREM從右邊開始刪除前count個value。
當count等於0,LREM會刪除所有值爲value的元素127.0.0.1:6379> RPUSH numbers 2
(integer) 4
127.0.0.1:6379> LRANGE numbers 0 -1
1) "2"
2) "1"
3) "0"
4) "2"
127.0.0.1:6379> LREM numbers -1 2
(integer) 1
127.0.0.1:6379> LRANGE numbers 0 -1
1) "2"
2) "1"
3) "0"

命令拾遺:
獲取/設置指定所以的元素值:LINDEX key value    返回指定索引的元素,索引從0開始
LSET key index value     將索引爲index的元素賦值爲value
127.0.0.1:6379> LINDEX numbers 0
"2"
127.0.0.1:6379> LINdEX numbers -1
"0"
127.0.0.1:6379> LSET numbers 1 7
OK
127.0.0.1:6379> LINdEX numbers 1
"7"

只保留列表指定片段
LTRIM key start end
LTRIM 刪除指定範圍之外的所有元素
127.0.0.1:6379> LRANGE numbers 0 -1
1) "2"
2) "7"
3) "0"
127.0.0.1:6379> LTRIM numbers 1 2
OK
127.0.0.1:6379> LRANGE numbers 0 -1
1) "7"
2) "0"

向列表插入元素:
LINSERT key BEFORE|AFTER pivot value
127.0.0.1:6379> LRANGE numbers 0 -1
1) "7"
2) "0"
127.0.0.1:6379> LINSERT numbers AFTER 7 3
(integer) 3
127.0.0.1:6379> LRANGE numbers 0 -1
1) "7"
2) "3"
3) "0"
127.0.0.1:6379> LINSERT numbers BEFORE 7 1
(integer) 4
127.0.0.1:6379> LRANGE numbers 0 -1
1) "1"
2) "7"
3) "3"
4) "0"

將元素從一個列表轉到另一個列表
RPOPLPUSH source destination


RPUSH:            RPUSH key-name value 將一個或者多個value推入列表的右端
LPUSH:            LPUSH key-name value 將一個或者多個value推入列表的左端
RPOP:            RPOP key-name 移除並返回最右端的元素
LPOP:            LPOP key-name 移除並返回最左端的元素
LINDEX:            LINDEX key-name offset 返回列表偏移量爲offset的元素
LRANGE:            LRANGE key-name start end 返回列表從start偏移到end範圍內所有元素.其中偏移量start和end的元素也包含在被返回的元素之內
LTRIM:            LTRIM key-name start end 對列表進行修剪只保留從start 到 end範圍內的元素.其中偏移量爲start和end的元素會保留下來


集合類型:
增加/刪除元素:
SADD key member [member ... ]
SREM key member [ member ... ]
127.0.0.1:6379> SADD letters a
(integer) 1
127.0.0.1:6379> SADD letters a b c
(integer) 2
127.0.0.1:6379> SMEMBERS letters
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> SREM lettes c
(integer) 0
127.0.0.1:6379> SMEMBERS letters
1) "b"
2) "a"
3) "c"
判斷元素是否在集合中:
SISMEMBER key member

127.0.0.1:6379> SISMEMBER letters a
(integer) 1
127.0.0.1:6379> SISMEMBER letters d
(integer) 0

集合運算:
差:SDIFF key [key ...]
交:SINTER key [key ...]
並:SUNION key [key ...]


127.0.0.1:6379> SADD setA 1 2 3
(integer) 3
127.0.0.1:6379> SADD setB 2 3 4
(integer) 3
127.0.0.1:6379> SDIFF setA setB
1) "1"
127.0.0.1:6379> SDIFF setB setA
1) "4"

127.0.0.1:6379> SINTER setA setB
1) "2"
2) "3"
127.0.0.1:6379> SUNION setA setB
1) "1"
2) "2"
3) "3"
4) "4"

命令拾遺:
獲取集合中元素個數:
SCARD key
127.0.0.1:6379> SCARD setA
(integer) 3

集合運算並將結果存儲:
SDIFFSTORE destination key [key ...]
SINTERSTORE destination key [key ...]
SUNIONSTORE destination key [key ...]
隨機獲取集合元素:
SRANDMEMBER key [count]
127.0.0.1:6379> SRANDMEMBER letters 3
1) "a"
2) "b"
3) "c"
從集合中彈出一個元素:
SPOP key


SADD:            SADD key-name item  將一個或者多個元素添加到集合裏面,並返回被添加元素當原中原本部存在於集合裏的元素數量.
SREM:            SRM key-name item  從集合裏面移除一個或多個元素,並返回被移除元素的數量.
SISMEMER:        SISMEMER key-name 返回集合包含的所有元素
SRANDMEMBER:    SRANDMEMBER key-name [count] 從集合裏隨機返回一個或多個元素,當count爲正數時,命令返回隨機元素不重複,負數時可能會重複
SPOP:            SPOP key-name隨機地移除集合中的一個元素,並返回被移除的元素
SMOVE:            SMOVE source dest item 如果集合source 包含元素item,那麼從集合source裏面移除item,並將元素item添加到集合dest中.


集合運算:
SDIFF:            SDIFF key-name1 key-name2 差集
SDIFFSTORE:        SDIFF dest key-name1 key-name2 差集,並把結果保存在dest
SINTER:            SINTER key-name1 key-name2 交集運算
SINTERSTORE:    SINTERSTORE: dest key-name1 key-name2 交集運算,並把結果存儲到dest
SUNION:            SUNION key-name1 key-name2 並集
SUNIONSTORE:    SUNIONSTORE dest key-name1 key-name2 並集,並把結果存儲到dest

有序集合:
增加元素:
ZADD key score member [score member ... ]
127.0.0.1:6379> ZADD scoreboard 89 Tom 67 Peter 90 David 100 Ray
(integer) 4
127.0.0.1:6379> ZADD scoreboard 76 Peter
(integer) 0
127.0.0.1:6379> ZADD testborard 17E+307 a
(integer) 1
127.0.0.1:6379> ZADD testboard 1.5 b
(integer) 1
獲取元素:
127.0.0.1:6379> ZSCORE testboard b
"1.5"
127.0.0.1:6379> ZSCORE scoreboard Tom
"89"
127.0.0.1:6379> ZSCORE scoreboard Ray
"100"

獲得排名在某個範圍元素列表:
ZRANGE key start stop [WITHSCORES]  從分數值小到大排序
ZREVRANGE key start stop [WITHSCORES]    按分數值從大到小排序

127.0.0.1:6379> ZREVRANGE scoreboard 0 -1
1) "Ray"
2) "David"
3) "Tom"
4) "Peter"
127.0.0.1:6379> ZRANGE scoreboard 0 -1
1) "Peter"
2) "Tom"
3) "David"
4) "Ray"
127.0.0.1:6379> ZRANGE scoreboard 0 -1 WITHSCORES
1) "Peter"
2) "76"
3) "Tom"
4) "89"
5) "David"
6) "90"
7) "Ray"
8) "100"
獲取指定分數範圍的元素
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
倒序:ZREVRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:6379> ZRANGEBYSCORE scoreboard 60 70
(empty list or set)
127.0.0.1:6379> ZRANGEBYSCORE scoreboard 60 80
1) "Peter"

127.0.0.1:6379> ZRANGEBYSCORE scoreboard (76 +inf
1) "Tom"
2) "David"
3) "Ray"
127.0.0.1:6379> ZRANGEBYSCORE scoreboard 76 +inf
1) "Peter"
2) "Tom"
3) "David"
4) "Ray"

右最高分的前三人:
127.0.0.1:6379> ZREVRANGEBYSCORE scoreboard 100 0 LIMIT 0 3
1) "Ray"
2) "David"
3) "Tom"
增加元素分數:
ZINCRBY key increment member
127.0.0.1:6379> ZINCRBY scoreboard 4 Jerry
"4"

127.0.0.1:6379> ZREVRANGEBYSCORE scoreboard 100 0  WITHSCORES
 1) "Ray"
 2) "100"
 3) "David"
 4) "90"
 5) "Tom"
 6) "89"
 7) "Peter"
 8) "76"
 9) "Jerry"
10) "4"
獲得集合元素的數量:
ZCARD key
127.0.0.1:6379> ZCARD scoreboard
(integer) 5

獲得指定分數範圍內的元素個數:
ZCOUNT key min max
127.0.0.1:6379> ZCOUNT scoreboard 90 100
(integer) 2

刪除一個或多個元素
ZREM key  member [member ...]
127.0.0.1:6379> ZREM scoreboard Peter
(integer) 1
127.0.0.1:6379> ZCARD scoreboard
(integer) 4
按照排名範圍刪除元素:
ZREMRANGEBYRANK key start stop
127.0.0.1:6379> ZADD testRem 1 a 2 b 3 c 4 d 5 e 6 f
(integer) 6
127.0.0.1:6379> ZREMRANGEBYRANK testRem 0 2
(integer) 3
127.0.0.1:6379> ZRANGE testRem 0 -1
1) "d"
2) "e"
3) "f"
獲得元素的排名:
ZRANK key member
ZREVRANK key member
127.0.0.1:6379> ZRANK scoreboard Ray
(integer) 3
127.0.0.1:6379> ZREVRANK scoreboard Ray
(integer) 0
計算有序集合的交集:
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

127.0.0.1:6379> ZADD sortedSets1 1 a 2 b
(integer) 2
127.0.0.1:6379> ZADD sortedSets2 10 a 30 b
(integer) 2
127.0.0.1:6379> ZINTERSTORE Result 2 sortedSets1 sortedSets2
(integer) 2
127.0.0.1:6379> ZRANGE Result 0 1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "32"


127.0.0.1:6379> ZINTERSTORE Result 2 sortedSets1 sortedSets2 AGGREGATE MIN
(integer) 2
127.0.0.1:6379> ZRANGE Result 0 1 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "2"

127.0.0.1:6379> ZINTERSTORE Result 2 sortedSets1 sortedSets2 AGGREGATE MAX
(integer) 2
127.0.0.1:6379> ZRANGE Result 0 1 WITHSCORES
1) "a"
2) "10"
3) "b"
4) "30"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章