1. 程式人生 > >redis 命令大全

redis 命令大全

全域性命令:

1、檢視所有鍵:keys * 2、鍵總數:dbsize 3、檢查鍵是否存在:exists key 4、刪除鍵:del key [key ...] 5、鍵過期:expire key seconds ——當超過過期時間後,會自動刪除鍵,例:expire hello 10 6、鍵的剩餘過期時間:ttl key ——返回鍵的剩餘過期時間:大於等於0的整數:鍵剩餘的過期時間;-1:鍵沒設定過期時間;-2:鍵不存在

7、鍵的資料結構型別:type key ——string(字串)、hash(雜湊)、list(列表)、set(集合)、zset(有序集合);鍵不存在,則返回none

字串型別相關命令:

1、設定值:set key value [ex seconds] [px milliseconds] [nx|xx] ——ex seconds:為鍵設定秒級過期時間;px milliseconds:為鍵設定毫秒級過期時間;nx:鍵必須不存在,才可以設定成功,用於新增;xx:與nx相反,鍵必須存在,才可以設定成功,用於更新。 2、setex key seconds value 3、setnx key value ——setex和setnx兩個命令,它們的作用和ex和nx選項一樣 4、獲取值:get key 5、批量設定值:mset key value [key value ...] 6、批量獲取值:mget key [key ...] 7、計數:incr key ——值不是整數,返回錯誤;值是整數,返回自增後的結果;鍵不存在,按照值為0自增,返回結果為1。 8、自減:decr key 9、自增指定數字:incrby key increment 10、自減指定數字:decrby key decrement 11、自增浮點數:incrbyfloat key increment 12、向字串尾部追加值:append key value 13、字串長度:strlen key 14、設定並返回原值:getset key value 15、設定指定位置的字元:setrange key offeset value ——例:setrange hello 0 e

16、獲取部分字串:getrange key start end                 ——start和end分別是開始和結束的偏移量,偏移量從0開始計算

雜湊型別相關命令

1、設定值:hset key field value 2、設定值:hsetnx key field value ——set和setnx命令一樣,只不過作用域由鍵變為field。 3、獲取值:hget key field 4、刪除field:hdel key field [field ...] 5、計算field個數:hlen key 6、批量設定field-value:hmget key field [field ...] 7、批量獲取field-value:hmset key field value [field value ...] 8、判斷field是否存在:hexists key field 9、獲取所有field:hkeys key 10、獲取所有value:hvals key 11、獲取所有的field-value:hgetall key 12、自增指定數字:hincrby key field increment 13、自增浮點數:hincrbyfloat key field increment

14、計算value的字串長度:hstrlen key field

列表型別相關命令:

1、從右向左插入元素:rpush key value [value ...] 2、從左向右插入元素:lpush key value [value ...] 3、向某個元素前或者後插入元素:linsert key before|after pivot value ——從列表中找到等於pivot的元素,在其前(before)或者後(after)插入一個新的元素value 4、獲取指定範圍內的元素列表:lrange key start end                        ——索引下標從左到右分別是0到N-1,但是從右到左分別是-1到-N。lrange中的end選項包含了自身 5、從左到右獲取元素:lrange key 0 -1 6、獲取列表指定索引下標的元素:lindex key index 7、獲取列表最後一個元素:lindex key -1 8、獲取列表長度:llen key 9、刪除最左側元素:lpop key 10、刪除最右側元素:rpop key 11、刪除指定元素:lrem key count value             ——從列表中找到等於value的元素進行刪除。count>0,從左到右,刪除最多count個元素;count<0,從右到左,刪除最多count絕對值個元素;count=0,刪除所有。 12、按照索引範圍修剪列表:ltrim key start end      ——保留列表key第start+1個到第end+1個元素 13、修改指定索引下標的元素:lset key index newValue 14、阻塞式刪除最左側元素:blpop key [key ...] timeout 15、阻塞式刪除最右側元素:brpop key [key ...] timeout     ——key[key...]:多個列表的鍵;timeout:阻塞時間(單位:秒)。 1)列表為空:如果timeout=3,那麼客戶端要等到3秒後返回,如果timeout=0,那麼客戶端一直阻塞等下去:如果此期間添加了資料,客戶端立即返回 2)列表不為空:客戶端會立即返回。 3)如果是多個鍵,那麼brpop會從左至右遍歷鍵,一旦有一個鍵能彈出元素,客戶端立即返回

4)如果多個客戶端對同一個鍵執行brpop,那麼最先執行brpop命令的客戶端可以獲取到彈出的值。

集合型別相關命令:

1、新增元素:sadd key element [element ...] 2、刪除元素:srem key element [element ...] 3、計算元素個數:scard key 4、判斷元素是否在集合中:sismember key element 5、隨機從集合返回指定個數元素:srandmember key [count] ——[count]是可選引數,如果不寫預設為1 6、從集合隨機刪除元素:spop key [count] 7、獲取所有元素:smembers key 8、求多個集合的交集:sinter key [key ...] 9、求多個集合的並集:suinon key [key ...] 10、求多個集合的差集:sdiff key [key ...] 11、將交集、並集、差集的結果儲存 sinterstore destination key [key ...] suionstore destination key [key ...]

sdiffstore destination key [key ...]

有序集合型別相關命令:

1、新增成員:zadd key score member [score member ...] 2、計算成員個數:zcard key 3、計算某個成員的分數:zscore key member 4、計算成員從分數從低到高的排名:zrank key member 5、計算成員從分數從高到低的排名:zrevrank key member 6、刪除成員:zrem key member [member ...] 7、增加成員的分數:zincrby key increment member 8、返回指定排名範圍的成員: zrange key start end [withscores] ——從低到高返回,如果加上withscores選項,同時會返回成員的分數 zrevrange key start end [withscores] 9、返回指定分數範圍的成員: zrangebyscore key min max [withscores] [limit offset count] zrevrangebyscore key max min [withscores] [limit offset count] zrangebyscore按照分數從低到高返回,withscores選項會同時返回每個成員的分數。[limit offset count]選項可以限制輸出的起始位置和個數;同時min和max還支援開區間(小括號)和閉區間(中括號),-inf和+inf分別代表無限小和無限大,例:zrangebyscore key (200 +inf withscores 10、返回指定分數範圍成員個數:zcount key min max 11、刪除指定排名內的升序元素:zremrangebyrank key start end 12、刪除指定分數範圍的成員:zremrangebyscore key min max 13、集合交集:zinterstore destination numkeys key [key ...] [weights weight [weight ...]] [aggregate sum|min|max] destination:交集計算結果儲存到這個鍵。 numkeys:需要做交集計算鍵的個數。 key[key...]:需要做交集計算的鍵。 weights weight[weight...]:每個鍵的權重,在做交集計算時,每個鍵中的每個member會將自己分數乘以這個權重,每個鍵的權重預設是1。 aggregate sum|min|max:計算成員交集後,分值可以按照sum(和)、min(最小值)、max(最大值)做彙總,預設值是sum。

14、集合並集:zunionstore destination numkeys key [key ...] [weights weight [weight ...]] [aggregate sum|min|max]

單鍵管理通用命令:

1、鍵重新命名:rename key newkey ——為了防止被強行rename,Redis提供了renamenx命令,確保只有newKey不存在時候才被覆蓋 2、隨機返回一個鍵:randomkey 3、鍵在seconds秒後過期:expire key seconds 4、鍵在秒級時間戳timestamp後過期:expireat key timestamp 5、查詢鍵的剩餘過期時間(毫秒級):pttl key 6、鍵在milliseconds毫秒後過期:pexpire key milliseconds 7、鍵在毫秒級時間戳timestamp後過期:pexpireat key milliseconds-timestamp 在使用Redis相關過期命令時,需要注意以下幾點。 1)如果expire key的鍵不存在,返回結果為0: 2)如果過期時間為負值,鍵會立即被刪除,猶如使用del命令一樣 3)對於字串型別鍵,執行set命令會去掉過期時間 清除鍵的過期時間:persist key 4)Redis不支援二級資料結構(例如雜湊、列表)內部元素的過期功能,例如不能對列表型別的一個元素做過期時間設定。

5)setex命令作為set+expire的組合,不但是原子執行,同時減少了一次網路通訊的時間。

遷移鍵相關命令:

1、在Redis內部進行資料遷移:move key db 2、在不同的Redis例項之間進行資料遷移:dump+restore dump key         ——dump命令會將鍵值序列化,格式採用的是RDB格式。 restore key ttl value ——在目標Redis上,restore命令將上面序列化的值進行復原,其中ttl引數代表過期時間,如果ttl=0代表沒有過期時間。 3、在Redis例項間進行資料遷移:migrate host port key|"" destination-db timeout [copy] [replace] [keys key [key ...]] migrate命令整個過程是原子執行的,不需要在多個Redis例項上開啟客戶端的,只需要在源Redis上執行migrate命令即可。 host:目標Redis的IP地址。 port:目標Redis的埠。 key|"":在Redis3.0.6版本之前,migrate只支援遷移一個鍵,所以此處是要遷移的鍵,但Redis3.0.6版本之後支援遷移多個鍵,如果當前需要遷移多個鍵,此處為空字串""。 destination-db:目標Redis的資料庫索引,例如要遷移到0號資料庫,這裡就寫0。 timeout:遷移的超時時間(單位為毫秒)。 [copy]:如果新增此選項,遷移後並不刪除源鍵。 [replace]:如果新增此選項,migrate不管目標Redis是否存在該鍵都會正常遷移進行資料覆蓋。

[keys key[key...]]:遷移多個鍵,例如要遷移key1、key2、key3,此處填寫“keys key1 key2 key3”。

遍歷鍵:

1、全量遍歷鍵:keys pattern     ——pattern使用的是glob風格的萬用字元:*代表匹配任意字元。[]代表匹配部分字元; 2、漸進式遍歷:scan cursor [match pattern] [count number] ——面向雜湊型別、集合型別、有序集合的掃描遍歷對應的命令分別是hscan、sscan、zscan ·cursor是必需引數,實際上cursor是一個遊標,第一次遍歷從0開始,每次scan遍歷完都會返回當前遊標的值,直到遊標值為0,表示遍歷結束。 ·match pattern是可選引數,它的作用的是做模式的匹配。

·count number是可選引數,它的作用是表明每次要遍歷的鍵個數,預設值是10,此引數可以適當增大。

資料庫管理命令:

1、切換資料庫:select dbIndex ——select 0 操作將切換到第一個資料庫 2、清除當前資料庫:flushdb

3、清除所有資料庫:flushall

慢查詢:

1、獲取慢查詢日誌:slowlog get [n] 2、獲取慢查詢日誌列表當前的長度:slowlog len

3、慢查詢日誌重置:slowlog reset

事務相關:

1、事務開始:multi 2、事務結束:exec 3、停止事務:discard

4、判斷key沒有被其他客戶端修改過:watch key

Bitmaps相關:

1、設定值:setbit key offset value ——設定鍵的第offset個位的值(從0算起) 2、獲取值:gitbit key offset 3、獲取Bitmaps指定範圍值為1的個數:bitcount key [start][end] ——[start]和[end]代表起始和結束位元組數 4、Bitmaps間的運算:bitop op destkey key[key....] bitop是一個複合操作,它可以做多個Bitmaps的and(交集)、or(並集)、not(非)、xor(異或)操作並將結果儲存在destkey中。

5、計算Bitmaps中第一個值為targetBit的偏移量:bitpos key targetBit [start] [end]

HyperLogLog相關:

1、向HyperLogLog新增元素:pfadd key element [element …] 2、計算一個或多個HyperLogLog的獨立總數:pfcount key [key …]

3、合併:pfmerge destkey sourcekey [sourcekey ...] ——求出多個HyperLogLog的並集並賦值給destkey

釋出訂閱相關:

1、釋出訊息:publish channel message 2、訂閱訊息:subscribe channel [channel ...] 3、取消訂閱:unsubscribe [channel [channel ...]] 4、按照模式訂閱和取消訂閱: psubscribe pattern [pattern...] punsubscribe [pattern [pattern ...]] 5、檢視活躍的頻道:pubsub channels [pattern] ——所謂活躍的頻道是指當前頻道至少有一個訂閱者,其中[pattern]是可以指定具體的模式 6、檢視頻道訂閱數:pubsub numsub [channel ...]

7、檢視模式訂閱數:pubsub numpat

GEO相關:

1、增加地理位置資訊:geoadd key longitude latitude member [longitude latitude member ...] ——longitude、latitude、member分別是該地理位置的經度、緯度、成員 2、獲取地理位置資訊:geopos key member [member ...] 3、獲取兩個地理位置的距離:geodist key member1 member2 [unit] 4、獲取指定位置範圍內的地理資訊位置集合: georadius key longitude latitude radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key] georadiusbymember key member radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key] georadius和georadiusbymember兩個命令的作用是一樣的,都是以一個地理位置為中心算出指定半徑內的其他地理資訊位置,不同的是georadius命令的中心位置給出了具體的經緯度,georadiusbymember只需給出成員即可。其中radiusm|km|ft|mi是必需引數,指定了半徑(帶單位) ·withcoord:返回結果中包含經緯度。 ·withdist:返回結果中包含離中心節點位置的距離。 ·withhash:返回結果中包含geohash,有關geohash後面介紹。 ·COUNT count:指定返回結果的數量。 ·asc|desc:返回結果按照離中心節點的距離做升序或者降序。 ·store key:將返回結果的地理位置資訊儲存到指定鍵。 ·storedist key:將返回結果離中心節點的距離儲存到指定鍵。 5、將二維經緯度轉換為一維字串:geohash key member [member ...] 6、刪除地理位置資訊:zrem key member --------------------- 本文來自 weixin_38364973 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/weixin_38364973/article/details/80527033?utm_source=copy