閱讀如下內容時,手邊打開一個redis-cli一塊兒輸入,輸入命令敲擊回車鍵前在心中想好你的答案,若是結果不合你的預期,請分析緣由,使極大地提升學習效率。若是沒有條件,每一個數據類型後有代碼運行結果,供你參考。java
Reids做爲一個key-value型存儲系統,下面咱們就分別從key和value提及:redis
key數據庫
key支持非二進制安全的字符類型(not binary-safe strings)。它不支持空格和換行等,key的命名通常建議使用object-type:id:field
,即對象類型 :對象ID:對象屬性方式。安全
對於key的命名,不要太長,佔內存,致使查詢慢;不要過短,顯然car:1:color比c:1:c的可讀性高太多app
key的相關操做:dom
keys *
exists car
del car
type car
randomkey
rename car train
renamenx car train
dbsize
expire train 60
ttl train
move key 1
flushdb
flushall
Value學習
Redis有這豐富的數據類型,包括Strings,Hashes,Lists,Sets和Ordered Sets日誌
1. 字符串 Stringscode
字符串類型是Redis中最基本的數據類型,它能夠存儲任何形式的字符串,包括二進制數據。能夠用它存儲用戶的郵箱、JSON話的對象甚至是一張圖片。一個字符串類型鍵容許存儲的數據最大容量是512MB。對象
字符串類型是其餘4中數據類型的基礎,其餘數據類型和字符串類型的差異從某種角度來講只是組織字符串的形式不一樣。
set age 101
setnx price 101
get age
getrange age 0 1
getset age 12
mset age1 1 age2 2
mget age1 age2
setex age3 60 3
strlen age
incr age
incrby age 12
incrbyfloat price 12.4
decr age
decrby age 12
append age years
127.0.0.1:6379> set age 101 OK 127.0.0.1:6379> setnx price 101 (integer) 1 127.0.0.1:6379> get age "101" 127.0.0.1:6379> getrange age 0 1 "10" 127.0.0.1:6379> getset age 12 "101" 127.0.0.1:6379> mset age1 1 age2 2 OK 127.0.0.1:6379> mget age1 age2 1) "1" 2) "2" 127.0.0.1:6379> setex age3 60 3 OK 127.0.0.1:6379> strlen age (integer) 2 127.0.0.1:6379> incr age (integer) 13 127.0.0.1:6379> incrby age 12 (integer) 25 127.0.0.1:6379> incrbyfloat price 12.4 "113.4" 127.0.0.1:6379> decr age (integer) 24 127.0.0.1:6379> decrby age 12 (integer) 12 127.0.0.1:6379> append age years (integer) 7
2. 哈希 Hashes
哈希類型是一個String類型的字段(field)和字段值(value)的映射表,字段值只能是字符串,換句話說,散列類型不能嵌套其餘的數據類型,每一個hash能夠存儲2^32-1鍵值對(40多億)。
提示:除了散列類型,Redis的其餘數據類型一樣不支持數據類型嵌套。好比集合類型的每一個元素都只能是字符串,不能是另外一個集合或散列表等。
散列類型適合存儲對象:使用對象類別和ID構成鍵名,使用字段表示對象的屬性,而字段值則存儲屬性值。例如要存儲ID爲2的汽車對象,能夠分別使用名爲color、name和price的3個字段存儲該汽車的顏色、名稱和價格:
hset car price 500 hset car name BMW hset car color red
hset student name zhangsan
hsetnx student age 12
hmset student sex boy address beijing
hget student name
hmget student name age
hgetall student
hexists student name
hincrby student age 1
hincrbyfloat student age 1.2
hdel student age address
hvals student
hkeys student
hlen student
127.0.0.1:6379> hset student name zhangsan (integer) 1 127.0.0.1:6379> hsetnx student age 12 (integer) 1 127.0.0.1:6379> hmset student sex boy address beijing OK 127.0.0.1:6379> hget student name "zhangsan" 127.0.0.1:6379> hmget student name age 1) "zhangsan" 2) "12" 127.0.0.1:6379> hgetall student 1) "name" 2) "zhangsan" 3) "age" 4) "12" 5) "sex" 6) "boy" 7) "address" 8) "beijing" 127.0.0.1:6379> hexists student name (integer) 1 127.0.0.1:6379> hincrby student age 1 (integer) 13 127.0.0.1:6379> hincrbyfloat student age 1.2 "14.2" 127.0.0.1:6379> hdel student age address (integer) 2 127.0.0.1:6379> hvals student 1) "zhangsan" 2) "boy" 127.0.0.1:6379> hkeys student 1) "name" 2) "sex" 127.0.0.1:6379> hlen student (integer) 2
3. 列表 List
列表類型(List)能夠存儲一個有序的字符串列表,常見的操做有向列表兩端添加元素,或者獲取列表的某一個片斷。
其內部使用雙向鏈表實現,因此向列表兩端添加元素、取元素的時間複雜度爲O(1),獲取越接近兩端的元素速度越快;使用鏈表的代價是經過索引訪問元素比較慢,設想在IPhone發售當天有1000我的在排隊,蘋果公司打算給第486位顧客免費贈送一部,工做人員不得不一個一個數到第486我的。若是是新來的人想加入隊伍,直接排到隊尾就能夠。
lpush student xiaoli xiaowang xiaoliu xiaozhang
rpush student zhangsan lisi wangwu
lpushx student xiaoming
rpushx student maliu
lpop student
rpop student
lindex student 0
linsert student before xiaozhang zhangsan
llen student
lrange student 0 4
lrem student 2 zhangsan
lset student 2 xiaolei
ltrim student 0 3
127.0.0.1:6379> lpush student xiaoli xiaowang xiaoliu xiaozhang (integer) 4 127.0.0.1:6379> rpush student zhangsan lisi wangwu (integer) 7 127.0.0.1:6379> lpushx student xiaoming (integer) 8 127.0.0.1:6379> rpushx student maliu (integer) 9 127.0.0.1:6379> lpop student "xiaoming" 127.0.0.1:6379> rpop student "maliu" 127.0.0.1:6379> lindex student 0 "xiaozhang" 127.0.0.1:6379> linsert student before xiaozhang zhangsan (integer) 8 127.0.0.1:6379> llen student (integer) 8 127.0.0.1:6379> lrange student 0 4 1) "zhangsan" 2) "xiaozhang" 3) "xiaoliu" 4) "xiaowang" 5) "xiaoli" 127.0.0.1:6379> lrem student 2 zhangsan (integer) 2 127.0.0.1:6379> lset student 2 xiaolei OK 127.0.0.1:6379> ltrim student 0 3 OK 127.0.0.1:6379> lrange student 0 3 1) "xiaozhang" 2) "xiaoliu" 3) "xiaolei" 4) "xiaoli"
4. 集合 Set
集合的概念高中數學中就已經學過,集合中的每個元素都是不一樣的,且沒有順序。一個集合類型(Set)鍵能夠存儲至多2^32-1個字符串。
集合類型的經常使用操做是向集合中加入或刪除元素、判斷某個元素是否存在等,因爲集合類型在Redis內部是使用值爲空的散列表(hash table)實現的,因此這些操做的時間複雜度都是O(1)。最方便的是多個集合類型鍵之間還能夠進行交集、並集和差集運算。
sadd student zhangsan lisi
,sadd monitor wangwu zhangsan
scard student
smembers student
srem student lisi
sismember student zhangsan
sinter student monitor
sinterstore sinter student monitor
sunion student monitor
sunionstore sunion student monitor
sdiff monitor student
sdiffstore sdiff monitor student
srandmember sunion 2
spop sunion
smove monitor student wangwu
127.0.0.1:6379> sadd student zhangsan lisi (integer) 2 127.0.0.1:6379> sadd monitor wangwu zhangsan (integer) 2 127.0.0.1:6379> scard student (integer) 2 127.0.0.1:6379> smembers student 1) "zhangsan" 2) "lisi" 127.0.0.1:6379> srem student lisi (integer) 1 127.0.0.1:6379> sismember student zhangsan (integer) 1 127.0.0.1:6379> sinter student monitor 1) "zhangsan" 127.0.0.1:6379> sinterstore sinter student monitor (integer) 1 127.0.0.1:6379> sunion student monitor 1) "zhangsan" 2) "wangwu" 127.0.0.1:6379> sunionstore sunion student monitor (integer) 2 127.0.0.1:6379> sdiff monitor student 1) "wangwu" 127.0.0.1:6379> sdiffstore sdiff monitor student (integer) 1 127.0.0.1:6379> srandmember sunion 2 1) "zhangsan" 2) "wangwu" 127.0.0.1:6379> spop sunion "zhangsan" 127.0.0.1:6379> smove monitor student wangwu (integer) 1
5. 有序集合 Sorted Set
有序集合與集合的區別在於「有序」二字,不只能夠完成插入、刪除和判斷元素是否存在等集合類型支持的操做,並且還能有序的獲取前N個元素。
有序集合類型在某些方面和列表類型有些類似。
可是兩者有着很大的區別,這使得它們的應用場景也是不一樣的。
有序集合類型算得上是Redis的5種數據類型中最高級的類型了,在學習時能夠與列表類型和集合類型對照理解。
zadd score 50 zhangsan 60 lisi 70 wangwu 80 zhaoliu 90 yangqi 120 xiaoming 130 xiaozhang 140 xiaoli 150 xiaoliu
更新分數 zadd score 100 zhangsan
zscore score zhangsan
zrange score 0 -1 withscores
zrevrange score 0 -1 withscores
(
;min和max還支持無窮大,-inf和+inf分別表明負無窮和正無窮。LIMIT offset count
中,offset表示從第一個元素開始偏移的位數,count表示輸出的數量。 如:zrangebyscore score 50 (80 limit 1 2
zrevrangebyscore score 80 50
zincrby score 5 lisi
zcard score
zcount score 50 100
zrem score wangwu
zremrangebyrank score 0 1
zremrangebyscore score 90 100
zrank score xiaoliu
zrevrank score xiaoliu
127.0.0.1:6379> zadd score 50 zhangsan 60 lisi 70 wangwu 80 zhaoliu 90 yangqi 120 xiaoming 130 xiaozhang 140 xiaoli 150 xiaoliu (integer) 9 127.0.0.1:6379> zadd score 100 zhangsan (integer) 0 127.0.0.1:6379> zscore score zhangsan "100" 127.0.0.1:6379> zrange score 0 -1 withscores 1) "lisi" 2) "60" 3) "wangwu" 4) "70" 5) "zhaoliu" 6) "80" 7) "yangqi" 8) "90" 9) "zhangsan" 10) "100" 11) "xiaoming" 12) "120" 13) "xiaozhang" 14) "130" 15) "xiaoli" 16) "140" 17) "xiaoliu" 18) "150" 127.0.0.1:6379> zrevrange score 0 -1 withscores 1) "xiaoliu" 2) "150" 3) "xiaoli" 4) "140" 5) "xiaozhang" 6) "130" 7) "xiaoming" 8) "120" 9) "zhangsan" 10) "100" 11) "yangqi" 12) "90" 13) "zhaoliu" 14) "80" 15) "wangwu" 16) "70" 17) "lisi" 18) "60" 127.0.0.1:6379> zrangebyscore score 50 (80 limit 1 2 1) "wangwu" 127.0.0.1:6379> zrevrangebyscore score 80 50 1) "zhaoliu" 2) "wangwu" 3) "lisi" 127.0.0.1:6379> zincrby score 5 lisi "65" 127.0.0.1:6379> zcard score (integer) 9 127.0.0.1:6379> zcount score 50 100 (integer) 5 127.0.0.1:6379> zrem score wangwu (integer) 1 127.0.0.1:6379> zremrangebyrank score 0 1 (integer) 2 127.0.0.1:6379> zremrangebyscore score 90 100 (integer) 2 127.0.0.1:6379> zrank score xiaoliu (integer) 3 127.0.0.1:6379> zrevrank score xiaoliu (integer) 0