儲存地理空間資訊.
可用指令
資料結構
新增member包含位置到geo.
127.0.0.1:6379> geoadd cities 121.5598 25.09108 taipei
(integer) 1
127.0.0.1:6379> geoadd cities 121.2168 24.93759 taoyuan
(integer) 1
取得geo中member的位置.
127.0.0.1:6379> geopos cities taipei
1) 1) "121.55980199575424194"
2) "25.09107928759889461"
取得geo中兩個member的距離.
# 預設單位為m
127.0.0.1:6379> geodist cities taipei taoyuan
"38557.7088"
# 單位為km
127.0.0.1:6379> geodist cities taipei taoyuan km
"38.5577"
# 單位為ft
127.0.0.1:6379> geodist cities taipei taoyuan ft
"126501.6694"
# 單位為mi
127.0.0.1:6379> geodist cities taipei taoyuan mi
"23.9587"
取得geo中指定位置半徑內的所有member.
127.0.0.1:6379> geoadd cities 121.5598 25.09108 taipei
(integer) 1
127.0.0.1:6379> geoadd cities 121.2168 24.93759 taoyuan
(integer) 1
127.0.0.1:6379> geoadd cities 120.666 23.01087 kaohsiung
(integer) 1
127.0.0.1:6379> geoadd cities 120.2513 23.1417 tainan
(integer) 1
# 取得cities中距離在121.52250 25.03715 半徑120 km 的member
127.0.0.1:6379> georadius cities 121.52250 25.03715 120 km
1) "taoyuan"
2) "taipei"
# 取得cities中距離在121.52250 25.03715 半徑120 km 的member並加上位置
127.0.0.1:6379> georadius cities 121.52250 25.03715 120 km withcoord
1) 1) "taoyuan"
2) 1) "121.21680110692977905"
2) "24.93758924779512398"
2) 1) "taipei"
2) 1) "121.55980199575424194"
2) "25.09107928759889461"
# 取得cities中距離在121.52250 25.03715 半徑120 km 的membe並加上距離
127.0.0.1:6379> georadius cities 121.52250 25.03715 120 km withdist
1) 1) "taoyuan"
2) "32.7483"
2) 1) "taipei"
2) "7.0785"
# 取得cities中距離在121.52250 25.03715 半徑120 km 的member並將提供位置hash
127.0.0.1:6379> georadius cities 121.52250 25.03715 120 km withhash
1) 1) "taoyuan"
2) (integer) 4050139696016711
2) 1) "taipei"
2) (integer) 4050152556288054
取得geo中指定member半徑內的所有member.
127.0.0.1:6379> geoadd cities 121.5598 25.09108 taipei
(integer) 1
127.0.0.1:6379> geoadd cities 121.2168 24.93759 taoyuan
(integer) 1
127.0.0.1:6379> geoadd cities 120.666 23.01087 kaohsiung
(integer) 1
127.0.0.1:6379> geoadd cities 120.2513 23.1417 tainan
(integer) 1
# 取得cities中距離tainan 半徑120 km的member
127.0.0.1:6379> georadiusbymember cities tainan 120 km
1) "tainan"
2) "kaohsiung"
取得指定member的geo hash值.
127.0.0.1:6379> geohash cities taipei
1) "wsqqwptnhs0"
當作進行基數統計的演算法.
可用指令
資料結構
新增元素到hyperloglog中.
127.0.0.1:6379> pfadd mytest abc def ghi jkl
(integer) 1
取得目前hyperloglog中元素的個數.
127.0.0.1:6379> pfadd mytest abc def ghi jkl
(integer) 1
127.0.0.1:6379> pfcount mytest
(integer) 4
合併多個hyperloglog並寫入到新的hyperloglog.
127.0.0.1:6379> pfadd mylog abc def ghi jkl
(integer) 1
127.0.0.1:6379> pfadd mylog2 mno pqr
(integer) 1
127.0.0.1:6379> pfcount mylog
(integer) 4
127.0.0.1:6379> pfcount mylog2
(integer) 2
# 合併mylog與mylog2並寫入mylog3
127.0.0.1:6379> pfmerge mylog3 mylog mylog2
OK
127.0.0.1:6379> pfcount mylog3
(integer) 6
由一組很長的二進位向量所組成,其中所儲存的資料為0 or 1.主要用途在於快速判斷某個元素是否存在於集合中,可以確定一定不存在,但不能確定一定存在.
可用指令
實際上就是交易開始時會把所有的操作存放到QUEUE後在交易結束時一次執行.
127.0.0.1:6379> set bankaccountA 300
OK
127.0.0.1:6379> set bankaccountB 500
OK
# 帳戶A原本有300
# 帳戶B原本有500
127.0.0.1:6379> mget bankaccountA bankaccountB
1) "300"
2) "500"
# 交易開始
127.0.0.1:6379> multi
OK
# 從帳戶B轉帳到帳戶A
127.0.0.1:6379(TX)> decrby bankaccountB 100
QUEUED
127.0.0.1:6379(TX)> incrby bankaccountA 100
QUEUED
# 交易結束
127.0.0.1:6379(TX)> exec
1) (integer) 400
2) (integer) 400
# 確認結果-成功
127.0.0.1:6379> mget bankaccountA bankaccountB
1) "400"
2) "400"
# 延續上面操作
127.0.0.1:6379> mget bankaccountA bankaccountB
1) "400"
2) "400"
127.0.0.1:6379> multi
OK
# 從帳戶B轉帳到帳戶A
127.0.0.1:6379(TX)> decrby bankaccountB 200
QUEUED
127.0.0.1:6379(TX)> incrby bankaccountA 200
QUEUED
# 取消交易
127.0.0.1:6379(TX)> discard
OK
# 確認結果-無異動
127.0.0.1:6379> mget bankaccountA bankaccountB
1) "400"
2) "400"
# 延續上面操作
127.0.0.1:6379> mget bankaccountA bankaccountB
1) "400"
2) "400"
127.0.0.1:6379> multi
OK
# 從帳戶B轉帳到帳戶A與C各100
127.0.0.1:6379(TX)> decrby bankaccountB 200
QUEUED
# 開啟帳戶C給予string value
127.0.0.1:6379(TX)> set bankaccountC abc
QUEUED
# 此指令會引發失敗
127.0.0.1:6379(TX)> incrby bankaccountC 100
QUEUED
127.0.0.1:6379(TX)> incrby bankaccountA 100
QUEUED
# 交易結束
127.0.0.1:6379(TX)> exec
1) (integer) 200
2) OK
3) (error) ERR value is not an integer or out of range
4) (integer) 500
# 確認結果-非預期的失敗造成結果異常
127.0.0.1:6379> mget bankaccountA bankaccountB bankaccountC
1) "500"
2) "200"
3) "abc"
# 延續上面操作
127.0.0.1:6379> mget bankaccountA bankaccountB bankaccountC
1) "500"
2) "200"
3) "abc"
127.0.0.1:6379> multi
OK
# 從帳戶A轉帳到帳戶C與B各100
127.0.0.1:6379(TX)> decrby bankaccountA 200
QUEUED
# 操作指令錯誤,不會被放到QUEUE
127.0.0.1:6379(TX)> setstring bankaccountC def
(error) ERR unknown command `setstring`, with args beginning with: `bankaccountC`, `def`,
127.0.0.1:6379(TX)> incrby bankaccountC 100
QUEUED
127.0.0.1:6379(TX)> incrby bankaccountB 100
QUEUED
# 交易結束
127.0.0.1:6379(TX)> exec
(error) EXECABORT Transaction discarded because of previous errors.
# 確認結果-由上而下執行,當錯誤已發生則會取消所有操作
127.0.0.1:6379> mget bankaccountA bankaccountB bankaccountC
1) "500"
2) "200"
3) "abc"