溫故而知新,本文主要以一些簡單的小例子,簡述Redis相關數據類型和對應的操做命令,僅供學習分享使用,若有不足之處,還請指正。html
Redis支持的數據類型,主要有如下五種:redis
keys命令,用於查詢並顯示知足條件的Key,以下所示:數組
1 127.0.0.1:6379> Keys * 2 1) "K2" 3 2) "K3" 4 3) "k1" 5 4) "K4" 6 5) "K5" 7 127.0.0.1:6379> Keys *2 8 1) "K2" 9 127.0.0.1:6379> Keys K* 10 1) "K2" 11 2) "K3" 12 3) "K4" 13 4) "K5"
randomkey 用於隨機的顯示一個Key,以下所示:app
1 127.0.0.1:6379> RANDOMKEY 2 "K3" 3 127.0.0.1:6379> RANDOMKEY 4 "K2" 5 127.0.0.1:6379> RANDOMKEY 6 "K2" 7 127.0.0.1:6379> RANDOMKEY 8 "K3" 9 127.0.0.1:6379> RANDOMKEY 10 "K5"
type命令,用於顯示值的數據類型,以下所示:dom
1 127.0.0.1:6379> type K5 2 string 3 127.0.0.1:6379> type K2 4 string
del刪除命令,用於刪除某一個鍵,返回刪除的鍵的個數,以下所示:學習
1 127.0.0.1:6379> DEL K3 2 (integer) 1
exists命令,用於判斷是否存在,0表示不存在,大於0表示存在的個數,以下所示:ui
1 127.0.0.1:6379> EXISTS K4 K5 2 (integer) 2 3 127.0.0.1:6379> EXISTS K3 4 (integer) 0
ttl命令 用於查詢鍵當前的是否有效,-1表示永久有效,-2表示失效 ,大於0表示多少秒後失效,以下所示:編碼
1 127.0.0.1:6379> TTL K3 2 (integer) -2 3 127.0.0.1:6379> TTL K4 4 (integer) -1 5 127.0.0.1:6379> TTL K2 6 (integer) -1
expire用於設置鍵的有效時間,即多少秒後失效,以下所示:spa
1 127.0.0.1:6379> EXPIRE K4 10 2 (integer) 1 3 127.0.0.1:6379> TTL K4 4 (integer) 2 5 127.0.0.1:6379> TTL K4 6 (integer) -2
persist命令,用於取消設置的有效時間,以下所示:code
1 127.0.0.1:6379> EXPIRE K5 10 2 (integer) 1 3 127.0.0.1:6379> PERSIST K5 4 (integer) 1 5 127.0.0.1:6379> TTL K5 6 (integer) -1
flushall命令,用於清除全部的內容,以下所示:
1 127.0.0.1:6379> FLUSHALL 2 OK 3 127.0.0.1:6379> kEYS * 4 (empty array)
set,get命令,用於設置或讀取string類型的值,以下所示:
1 127.0.0.1:6379> SET AGE 10 2 OK 3 127.0.0.1:6379> KEYS * 4 1) "K2" 5 2) "AGE" 6 3) "K5" 7 127.0.0.1:6379> GET AGE 8 "10"
getset命令,讀取舊值,並設置新值,以下所示:
1 127.0.0.1:6379> GETSET AGE 15 2 "10" 3 127.0.0.1:6379> GET AGE 4 "15"
getrange命令,用於獲取指定範圍內長度的值,以下所示:
1 127.0.0.1:6379> GETRANGE K2 0 2 2 "V2" 3 127.0.0.1:6379> GETRANGE K2 0 0 4 "V" 5 127.0.0.1:6379> GETRANGE K2 0 -1 6 "V2" 7 127.0.0.1:6379> GETRANGE K2 0 -2 8 "V" 9 127.0.0.1:6379> GETRANGE K2 0 -3 10 "V"
mset,mget命令,用於批量設置,獲取鍵值,以下所示:
1 127.0.0.1:6379> MSET NAME HEX AGE 20 SET MALE 2 OK 3 127.0.0.1:6379> kEYS * 4 1) "AGE" 5 2) "NAME" 6 3) "SET" 7 127.0.0.1:6379> MGET NAME AGE SET 8 1) "HEX" 9 2) "20" 10 3) "MALE" 11 127.0.0.1:6379> MGET NAME AGE 12 1) "HEX" 13 2) "20"
setnx命令,不存在則設置,存在則不設置,返回影響的個數,以下所示:
1 127.0.0.1:6379> SETNX NAME LL 2 (integer) 0 3 127.0.0.1:6379> SETNX SEX FEMALE 4 (integer) 1 5 127.0.0.1:6379> kEYS * 6 1) "SEX" 7 2) "AGE" 8 3) "NAME" 9 4) "SET"
setrange命令用於替換字符串,第2個參數爲偏移量,從0開始,第3個參數爲要替換的新內容,以下所示:
1 127.0.0.1:6379> SETRANGE NAME 2 AAAAAAAA 2 (integer) 10 3 127.0.0.1:6379> GET NAME 4 "HEAAAAAAAA" 5 127.0.0.1:6379> SETRANGE AGE 3 BBBBB 6 (integer) 8 7 127.0.0.1:6379> GET AGE 8 "20\x00BBBBB"
incr ,decr命令,主要用於對數值類value的遞增,遞減操做,若是不能夠轉換爲整數類型,則報錯,以下所示:
1 127.0.0.1:6379> SET SCORE 20 2 OK 3 127.0.0.1:6379> INCR SCORE 4 (integer) 21 5 127.0.0.1:6379> INCR SCORE 6 (integer) 22 7 127.0.0.1:6379> GET SCORE 8 "22" 9 127.0.0.1:6379> DECR SCORE 10 (integer) 21 11 127.0.0.1:6379> DECR SCORE 12 (integer) 20 13 127.0.0.1:6379> GET SCORE 14 "20" 15 127.0.0.1:6379> INCR NAME 16 (error) ERR value is not an integer or out of range
incrby ,decrby命令,按指定的步長進行增長,減小操做。其中第2個參數爲step(步長)以下所示:
1 127.0.0.1:6379> INCRBY SCORE 5 2 (integer) 25 3 127.0.0.1:6379> INCRBY SCORE 5 4 (integer) 30 5 127.0.0.1:6379> GET SCORE 6 "30" 7 127.0.0.1:6379> DECRBY SCORE 3 8 (integer) 27 9 127.0.0.1:6379> DECRBY SCORE 3 10 (integer) 24
incrbyfloat命令,按浮點數進行遞增操做,以下所示:
1 127.0.0.1:6379> INCRBYFLOAT SCORE 0.2 2 "24.2" 3 127.0.0.1:6379> INCRBYFLOAT SCORE 0.2 4 "24.4" 5 127.0.0.1:6379> GET SCORE 6 "24.4"
append命令,用於追加內容,以下所示:
1 127.0.0.1:6379> APPEND NAME ALANHSIANG 2 (integer) 20 3 127.0.0.1:6379> GET NAME 4 "HEAAAAAAAAALANHSIANG"
strlen命令,用於獲取value值的長度,以下所示:
1 127.0.0.1:6379> STRLEN NAME 2 (integer) 20
object encoding命令,用於獲取對象編碼類型,以下所示:
1 127.0.0.1:6379> OBJECT ENCODING NAME 2 "raw" 3 127.0.0.1:6379> OBJECT ENCODING AGE 4 "raw" 5 127.0.0.1:6379> OBJECT ENCODING SCORE 6 "embstr"
lpush,lrange命令,先進後出命令,即從頭部開始插入。lrange命令,用於顯示指定位置的內容。以下所示:
1 127.0.0.1:6379> lpush fruit apple banana pear orange 2 (integer) 4 3 127.0.0.1:6379> lrange fruit 0 -1 4 1) "orange" 5 2) "pear" 6 3) "banana" 7 4) "apple"
rpush命令,先進先出命令,即從尾部插入,以下所示:
1 127.0.0.1:6379> rpush ruit apple banana pear orange 2 (integer) 4 3 127.0.0.1:6379> lrange ruit 0 -1 4 1) "apple" 5 2) "banana" 6 3) "pear" 7 4) "orange"
lpop,rpop命令,用於彈出列表的元素,彈出後則列表元素消失。以下所示:
1 127.0.0.1:6379> lpop ruit 2 "apple" 3 127.0.0.1:6379> rpop ruit 4 "orange" 5 127.0.0.1:6379> lrange ruit 0 -1 6 1) "banana" 7 2) "pear" 8 127.0.0.1:6379> llen ruit 9 (integer) 2
llen命令,用於獲取列表的長度,即元素個數。
1 127.0.0.1:6379> llen ruit 2 (integer) 2 3 127.0.0.1:6379> llen fruit 4 (integer) 4
list元素容許重複,以下所示:
1 127.0.0.1:6379> lpush mylist l1 l2 l3 l1 l4 l5 l1 l6 l7 l8 l1 2 (integer) 11 3 127.0.0.1:6379> lrange mylist 0 -1 4 1) "l1" 5 2) "l8" 6 3) "l7" 7 4) "l6" 8 5) "l1" 9 6) "l5" 10 7) "l4" 11 8) "l1" 12 9) "l3" 13 10) "l2" 14 11) "l1"
lrem命令,刪除列表中的元素,第2個參數表示個數,等於0表示刪除全部符合條件的,第3個參數表示要刪除的內容,以下所示:
1 127.0.0.1:6379> lrem mylist 0 l1 2 (integer) 4 3 127.0.0.1:6379> lrange mylist 0 -1 4 1) "l8" 5 2) "l7" 6 3) "l6" 7 4) "l5" 8 5) "l4" 9 6) "l3" 10 7) "l2"
lrem命令,第2個參數大於0表示從頭至尾開始刪除,刪除指定個數的元素,小於0表示從尾到頭開始刪除,絕對值爲要刪除的個數。以下所示:
1 127.0.0.1:6379> lpush mylist2 l1 l2 l3 l1 l4 l5 l1 l6 l7 l8 l1 2 (integer) 11 3 127.0.0.1:6379> lrem mylist2 -1 l1 4 (integer) 1 5 127.0.0.1:6379> lrange mylist2 0 -1 6 1) "l1" 7 2) "l8" 8 3) "l7" 9 4) "l6" 10 5) "l1" 11 6) "l5" 12 7) "l4" 13 8) "l1" 14 9) "l3" 15 10) "l2" 16 127.0.0.1:6379> lrem mylist2 -1 l1 17 (integer) 1 18 127.0.0.1:6379> lrange mylist2 0 -1 19 1) "l1" 20 2) "l8" 21 3) "l7" 22 4) "l6" 23 5) "l1" 24 6) "l5" 25 7) "l4" 26 8) "l3" 27 9) "l2"
lindex命令,獲取列表中指定索引的值,以下所示:
1 127.0.0.1:6379> lindex mylist2 2 2 "l7"
lset 設置列表指定位置的值,以下所示:
1 127.0.0.1:6379> lset mylist2 2 l7777 2 OK 3 127.0.0.1:6379> lrange mylist2 0 -1 4 1) "l1" 5 2) "l8" 6 3) "l7777" 7 4) "l6" 8 5) "l1" 9 6) "l5" 10 7) "l4" 11 8) "l3" 12 9) "l2"
ltrim命令,對列表進行裁剪,保留指定區域的內容,以下所示:
1 127.0.0.1:6379> ltrim mylist2 0 4 2 OK 3 127.0.0.1:6379> lrange mylist2 0 -1 4 1) "l1" 5 2) "l8" 6 3) "l7777" 7 4) "l6" 8 5) "l1"
linsert 指定位置插入值,第2個參數,分別表示before(元素前),after(元素後),以下所示:
1 127.0.0.1:6379> linsert mylist2 before l6 l99 2 (integer) 6 3 127.0.0.1:6379> lrange mylist2 0 -1 4 1) "l1" 5 2) "l8" 6 3) "l7777" 7 4) "l99" 8 5) "l6" 9 6) "l1" 10 127.0.0.1:6379> linsert mylist2 after l6 l100 11 (integer) 7 12 127.0.0.1:6379> lrange mylist2 0 -1 13 1) "l1" 14 2) "l8" 15 3) "l7777" 16 4) "l99" 17 5) "l6" 18 6) "l100" 19 7) "l1"
rpoplpush命令,彈出一個元素到另一個列表中,以下所示:
1 127.0.0.1:6379> lpush list1 l1 l2 l3 2 (integer) 3 3 127.0.0.1:6379> lpush list3 14 l5 l6 4 (integer) 3 5 127.0.0.1:6379> rpoplpush list1 list3 6 "l1" 7 127.0.0.1:6379> lrange list1 0 -1 8 1) "l3" 9 2) "l2" 10 127.0.0.1:6379> lrange list3 0 -1 11 1) "l1" 12 2) "l6" 13 3) "l5" 14 4) "14"
hset,hget命令,主要用於設置,獲取hash數據類型的內容,以下所示:
1 127.0.0.1:6379> hset user1 id 1 2 (integer) 1 3 127.0.0.1:6379> hset user1 name Alan 4 (integer) 1 5 127.0.0.1:6379> hset user1 age 20 6 (integer) 1 7 127.0.0.1:6379> hget user1 id 8 "1" 9 127.0.0.1:6379> hget user1 name 10 "Alan" 11 127.0.0.1:6379> hget user1 age 12 "20" 13 127.0.0.1:6379> hget user1 14 (error) ERR wrong number of arguments for 'hget' command
hmset命令,能夠一次設置多個內容,以下所示:
1 127.0.0.1:6379> hmset user1 id 2 name hsiang age 25 2 OK 3 127.0.0.1:6379> hget user1 id 4 "2"
hgetall命令,用於獲取鍵對應的全部內容,以下所示:
1 127.0.0.1:6379> HGETALL user1 2 1) "id" 3 2) "2" 4 3) "name" 5 4) "hsiang" 6 5) "age" 7 6) "25"
hexists命令,用於判斷鍵裏面的屬性內容是否存在,大於0表示存在,等於0表示不存在,以下所示:
1 127.0.0.1:6379> HEXISTS user1 addr 2 (integer) 0 3 127.0.0.1:6379> HEXISTS user1 id 4 (integer) 1
hsetnx命令,不存在則設置,存在則不設置,以下所示:
1 127.0.0.1:6379> HSETNX user1 addr beijing 2 (integer) 1 3 127.0.0.1:6379> HSETNX user1 addr beijing 4 (integer) 0 5 127.0.0.1:6379> hgetall user1 6 1) "id" 7 2) "2" 8 3) "name" 9 4) "hsiang" 10 5) "age" 11 6) "25" 12 7) "addr" 13 8) "beijing"
hincrby命令,鍵對應內容field的值遞增命令,以下所示:
1 127.0.0.1:6379> HINCRBY user1 age 1 2 (integer) 26 3 127.0.0.1:6379> HINCRBY user1 age 1 4 (integer) 27 5 127.0.0.1:6379> HINCRBY user1 age 1 6 (integer) 28
hkeys,hvals命令,用於獲取鍵對應的屬性或內容,以下所示:
1 127.0.0.1:6379> HKEYS user1 2 1) "id" 3 2) "name" 4 3) "age" 5 4) "addr" 6 127.0.0.1:6379> HVALS user1 7 1) "2" 8 2) "hsiang" 9 3) "28" 10 4) "beijing"
hlen,用於獲取key對應的field的數量,以下所示:
1 127.0.0.1:6379> hlen user1 2 (integer) 4
sadd命令,用於新增set數據集,set類型的數據會進行去重,以下所示:
1 127.0.0.1:6379> sadd myset s1 s2 s1 s2 s3 s3 2 (integer) 3
smembers命令,用於顯示某一個set中全部的內容,以下所示:
1 127.0.0.1:6379> SMEMBERS myset 2 1) "s2" 3 2) "s1" 4 3) "s3"
srem命令,刪除指定列表中的指定元素,以下所示:
1 127.0.0.1:6379> srem myset s1 2 (integer) 1 3 127.0.0.1:6379> SMEMBERS myset 4 1) "s2" 5 2) "s3"
sismember命令,用於判斷set中是否存在某一元素,以下所示:
1 127.0.0.1:6379> SISMEMBER myset s2 2 (integer) 1 3 127.0.0.1:6379> SISMEMBER myset s1 4 (integer) 0
scard命令,用於獲取集合中成員的個數,以下所示:
1 127.0.0.1:6379> SCARD myset 2 (integer) 2
srandmember命令,用於隨機返回集合中的成員,以下所示:
1 127.0.0.1:6379> SRANDMEMBER myset 2 "s2" 3 127.0.0.1:6379> SRANDMEMBER myset 4 "s2" 5 127.0.0.1:6379> SRANDMEMBER myset 6 "s3" 7 127.0.0.1:6379> SRANDMEMBER myset 8 "s2" 9 127.0.0.1:6379> SRANDMEMBER myset 10 "s3"
第2個參數以下沒有,則默認隨機返回一個變量,以下第2個參數是負值,則會隨機返回負數絕對值的個數的成員,若是成員不足,則會產生重複;若是第2個參數是正數,則會隨機返回指定個數的成員,若是成員不足,則返回最大成員,以下所示:
1 127.0.0.1:6379> SRANDMEMBER myset -6 2 1) "s3" 3 2) "s3" 4 3) "s2" 5 4) "s2" 6 5) "s3" 7 6) "s2" 8 127.0.0.1:6379> SRANDMEMBER myset 6 9 1) "s2" 10 2) "s3"
sdiff命令,取兩個之間的差集,即在第一個裏面,不在第二個裏面,以下所示:
1 127.0.0.1:6379> sadd myset1 s1 s2 s3 s4 2 (integer) 4 3 127.0.0.1:6379> sadd myset2 s3 s4 s5 s6 4 (integer) 4 5 127.0.0.1:6379> sadd myset3 s5 s6 s7 s8 6 (integer) 4 7 127.0.0.1:6379> SDIFF myset1 myset2 8 1) "s2" 9 2) "s1" 10 127.0.0.1:6379> SDIFF myset1 myset2 myset3 11 1) "s2" 12 2) "s1" 13 127.0.0.1:6379> SDIFF myset2 myset1 14 1) "s6" 15 2) "s5"
sunion命令,取兩個集合之間的並集,以下所示:
1 127.0.0.1:6379> SUNION myset1 myset2 2 1) "s2" 3 2) "s4" 4 3) "s3" 5 4) "s5" 6 5) "s6" 7 6) "s1"
sinter命令,取集合之間的交集,如無交集,則返回空數組,以下所示:
1 127.0.0.1:6379> SINTER myset1 myset2 2 1) "s4" 3 2) "s3" 4 127.0.0.1:6379> SINTER myset2 myset3 5 1) "s6" 6 2) "s5" 7 127.0.0.1:6379> SINTER myset1 myset2 myset3 8 (empty array)
zadd命令,用於向有序集合中增長元素,若是元素已存在,則不會新增元素,只會更新分數,以下所示:
1 127.0.0.1:6379> zadd myset4 1 z1 2 (integer) 1 3 127.0.0.1:6379> zadd myset4 2 z2 4 (integer) 1 5 127.0.0.1:6379> zadd myset4 3 z3 6 (integer) 1 7 127.0.0.1:6379> zadd myset4 4 z4 8 (integer) 1 9 127.0.0.1:6379> zadd myset4 5 z4 10 (integer) 0
zscore命令,用於顯示分數,以下所示:
1 127.0.0.1:6379> ZSCORE myset4 z4 2 "5"
zincrby命令,用於增長分數,以下所示:
1 127.0.0.1:6379> ZINCRBY myset4 1 z4 2 "6" 3 127.0.0.1:6379> ZSCORE myset4 z4 4 "6"
zrange命令,用於返回集合中指定範圍的內容,以下所示:
1 127.0.0.1:6379> ZRANGE myset4 0 -1 withscores 2 1) "z1" 3 2) "1" 4 3) "z2" 5 4) "2" 6 5) "z3" 7 6) "3" 8 7) "z4" 9 8) "6" 10 127.0.0.1:6379> ZRANGE myset4 0 -1 11 1) "z1" 12 2) "z2" 13 3) "z3" 14 4) "z4"
zrangebyscore命令,經過分數查詢內容,以下所示:
1 127.0.0.1:6379> ZRANGEBYSCORE myset4 2 6 withscores 2 1) "z2" 3 2) "2" 4 3) "z3" 5 4) "3" 6 5) "z4" 7 6) "6" 8 127.0.0.1:6379> ZRANGEBYSCORE myset4 2 6 9 1) "z2" 10 2) "z3" 11 3) "z4"
zcard命令,用於獲取集合中元素個數,以下所示:
1 127.0.0.1:6379> zcard myset4 2 (integer) 4
zcount命令,獲取分數範圍內的成員個數,以下所示:
1 127.0.0.1:6379> zcount myset4 2 4 2 (integer) 2 3 127.0.0.1:6379> zcount myset4 1 6 4 (integer) 4
zrem命令,刪除集合中,指定的元素,以下所示:
1 127.0.0.1:6379> zrem myset4 z3 2 (integer) 1 3 127.0.0.1:6379> zrange myset4 0 -1 4 1) "z1" 5 2) "z2" 6 3) "z4"
關於Redis學習,本文主要是拋磚引玉,更多命令能夠參考菜鳥教程。
子曰:溫故而知新,能夠爲師矣。子曰:學而不思則罔,思而不學則殆。