golang redis之set型別簡單操作

2021-08-11 07:35:57 字數 3452 閱讀 2604

1:安裝redigo

go get github.com/garyburd/redigo/redis
2:連線redis

3:命令執行

n,err := c.do("sadd","key","value")  //寫

result,err := redis.values(c.do("smembers","key"))//讀

4:set型別常見操作

sadd(key, member):向名稱為key的set中新增元素member

srem(key, member) :刪除名稱為key的set中的元素member

spop(key) :隨機返回並刪除名稱為key的set中乙個元素

smove(srckey, dstkey, member) :移到集合元素

scard(key) :返回名稱為key的set的基數

sismember(key, member) :member是否是名稱為key的set的元素

sinter(key1, key2,…key n) :求交集

sinterstore(dstkey, (keys)) :求交集並將交集儲存到dstkey的集合

sunion(key1, (keys)) :求並集

sunionstore(dstkey, (keys)) :求並集並將並集儲存到dstkey的集合

sdiff(key1, (keys)) :求差集

sdiffstore(dstkey, (keys)) :求差集並將差集儲存到dstkey的集合

smembers(key) :返回名稱為key的set的所有元素

srandmember(key) :隨機返回名稱為key的set的乙個元素

5:示例

5.1:sadd/smembers示例

_, err = c.do("sadd", "myset", "mobike", "foo", "ofo", "bluegogo")

iferr != nil

value, err := redis.values(c.do("smembers", "myset"))

iferr != nil else

fmt.printf("\n")

}

5.2:srandmember示例

ret, err := c.do("srandmember", "myset")

if err != nil else

5.3:srem示例

_, err = c.do("srem", "myset", "bluegogo")

iferr != nil else else

fmt.printf("\n")}}

5.4:smove示例

_, err = c.do("smove", "myset", "dbset", "mobike")

iferr != nil else else

fmt.printf("\n")}}

5.5:scard示例

num, err := c.do("scard", "myset")

iferr != nil else

5.6:sismember

ismember, err := c.do("sismember", "myset", "foo")

iferr != nil else

5.7:sinter示例

_, err = c.do("sadd", "dbset", "foo", "ofo", "xiaolan")

iferr != nil

inner, err := redis.values(c.do("sinter", "myset", "dbset"))

iferr != nil else

fmt.printf("\n")

}

5.8:sinterstore

_, err = c.do("sinterstore", "newset", "dbset", "myset")

iferr != nil else else

fmt.printf("\n")}}

5.9:sunion

unino, err := redis.values(c.do("sunion", "myset", "dbset"))

iferr != nil else

fmt.printf("\n")

}

5.10:sunionstore

_, err = c.do("sunionstore", "unewset", "myset", "dbset")

iferr != nil else else

fmt.printf("\n")}}

5.11:sdiff

diff, err := redis.values(c.do("sdiff", "dbset", "myset"))

iferr != nil else

fmt.printf("\n")

}

5.12:sdiffstore

_, err = c.do("sdiffstore", "dnewset", "dbset", "myset")

iferr != nil else else

fmt.printf("\n")}}

5.13:spop

res, err := c.do("spop", "myset")

iferr != nil else

value, err = redis.values(c.do("smembers", "myset"))

iferr != nil else

fmt.printf("\n")

}

6:執行結果

完整**路徑

golang redis之list型別簡單操作

1 安裝redigo go get github.com garyburd redigo redis2 連線redis 通過ip埠 3 命令執行 n,err c.do lpush key value 寫 result,err redis.values c.do lpop key 讀 4 list型別...

Golang Redis分布式鎖

分布式鎖實現 獲取分布式鎖 func h handler acquirelock cuuid string error return nil return errors.new set distribute lock failed 釋放分布式鎖 func h handler releaselock ...

集合之Set(隨筆)

set介面 儲存無序的 不可重複的資料 以hashset為例說明 1.無序性 不等於隨機性。儲存的資料在底層陣列中並非按照陣列索引的順序新增,而是根據資料的雜湊值決定的。2.不可重複性 保證新增的元素按照equals 判斷時,不能返回true.即 相同的元素只能新增乙個。新增元素的過程 對於新增成功...