sorted set類型如何在golang-redis中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
成都一家集口碑和實力的網(wǎng)站建設服務商,擁有專業(yè)的企業(yè)建站團隊和靠譜的建站技術,10余年企業(yè)及個人網(wǎng)站建設經(jīng)驗 ,為成都成百上千客戶提供網(wǎng)頁設計制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設等服務,包括成都營銷型網(wǎng)站建設,品牌網(wǎng)站制作,同時也為不同行業(yè)的客戶提供成都做網(wǎng)站、網(wǎng)站制作的服務,包括成都電商型網(wǎng)站制作建設,裝修行業(yè)網(wǎng)站制作建設,傳統(tǒng)機械行業(yè)網(wǎng)站建設,傳統(tǒng)農(nóng)業(yè)行業(yè)網(wǎng)站制作建設。在成都做網(wǎng)站,選網(wǎng)站制作建設服務商就選創(chuàng)新互聯(lián)公司。go get github.com/garyburd/redigo/redis
import ( "github.com/garyburd/redigo/redis" )
c, err := redis.Dial("tcp", "192.168.2.225:6379") if err != nil { fmt.Println("connect to redis err", err.Error()) return } defer c.Close()
n,err := c.Do("zadd","key","score","member") //寫
result,err := redis.Values(c.Do("zrange","key",0,-1))//讀
zadd(key, score1,member1,...scoreN,memberN) 向有序結(jié)合添加(更新)一個或多個成員 zcard(key):獲取有序集合的成員 zcount(key,start,end):計算指定區(qū)間的成員數(shù) zincrby(key,increment,member):成員member增加increment zinterstore(dst,numkey,src1,src2..srcN):求交集,并將結(jié)果存儲新的結(jié)合 zlexcount(key,start,end):計算字典區(qū)間成員數(shù)(分數(shù)都相同,按照字典排序) zrange(key,start,end):獲取索引區(qū)間的成員 zrangebylex (key,start,end):通過字典區(qū)間返回區(qū)間內(nèi)有序集合成員 zrangebyscore(key,start,end):通過分數(shù)返回區(qū)間內(nèi)的有序集合 zrank (key,member):返回有序結(jié)合的索引 zrem(key,members1...membersN):刪除一個或多個成員 zremrangebylex(key,start,end):移除集合中給定字典區(qū)間的成員 zremrangebyrank(key,start,end):移除有序集合中給定的排名區(qū)間的所有成員 zremrangebyscore(key,start,end):移除給定分數(shù)區(qū)間的所有元素 zrevange(key,start,end):通過索引,分數(shù)由高到低,返回指定區(qū)域的元素 zrevrangebyscore(key,member):分數(shù)由高向低返回指定區(qū)間的成員數(shù) zrevrank(key,member):分數(shù)從小到大,返回指定成員的排名 zscore(key,member):返回有序集中,成員的分數(shù)值 zunionstore(dst,numkeys,key1...keyN):返回給定的一個或多個集合的并集,并存儲在新的集合中 zscan(key,cursor):迭代有序結(jié)合中的元素(包括元素成員和元素分值)
6.1:zadd
_, err1 := c.Do("zadd", "curbike", 1, "mobike", 2, "xiaolan", 3, "ofo", 4, "xiaoming") _, err2 := c.Do("zadd", "tmpdata", 0, "mobike", 0, "xiaolan", 0, "mysql", 0, "redis", 0, "mongo", 0, "xiaoming") if err1 != nil || err2 != nil { fmt.Println("zadd failed", err.Error()) }
6.2:zcard
num, err := c.Do("zcard", "curbike") if err != nil { fmt.Println("zcard failed", err.Error()) } else { fmt.Printf("curbike's size is %s:", num) }
6.3:zcount
num, err = c.Do("zcount", "curbike", 1, 3) if err != nil { fmt.Println("zcount failed ", err.Error()) } else { fmt.Println("zcount num is :", num) }
6.4:zincrby
num, err = c.Do("zincrby", "curbike", 3, "xiaolan") fmt.Println(reflect.TypeOf(num)) if err != nil { fmt.Println("zincrby failed", err.Error()) } else { fmt.Println("after zincrby the :", num) }
6.5:zinterstore
_, err = c.Do("zinterstore", "internewset", 2, "curbike", "tmpdata") if err != nil { fmt.Println("zinterstore failed", err.Error()) } else { result, err := redis.Values(c.Do("zrange", "internewset", 0, 10)) if err != nil { fmt.Println("interstore failed", err.Error()) } else { fmt.Printf("interstore newset elsements are:") for _, v := range result { fmt.Printf("%s ", v.([]byte)) } fmt.Println() } }
6.6:zlexcount
num, err = c.Do("zlexcount", "tmpdata", "[mongo", "[xiaoming") if err != nil { fmt.Println("zlexcount failed", err.Error()) } else { fmt.Println("zlexcount in tmpdata is :", num) }
6.7:
res, err := redis.Values(c.Do("zrange", "curbike", 0, -1, "withscores")) if err != nil { fmt.Println("zrange in curbike failed", err.Error()) } else { fmt.Printf("curbike's element are follow:") for _, v := range res { fmt.Printf("%s ", v.([]byte)) } fmt.Println() }
6.8:zrangebylex
res, err = redis.Values(c.Do("zrangebylex", "tmpdata", "[mobike", "[redis")) if err != nil { fmt.Println("zrangebylex failed", err.Error()) } else { fmt.Printf("zrangebylex in tmpdata:") for _, v := range res { fmt.Printf("%s ", v.([]byte)) } fmt.Println() }
6.9:zrangebyscore
res, err = redis.Values(c.Do("zrangebyscore", "curbike", "(1", "(5")) if err != nil { fmt.Println("zrangebyscore failed", err.Error()) } else { fmt.Printf("zrangebyscore's element:") for _, v := range res { fmt.Printf("%s ", v.([]byte)) } fmt.Println() }
6.10:zrank
num, err = c.Do("zrank", "internewset", "xiaoming") if err != nil { fmt.Println("zrank failed ", err.Error()) } else { fmt.Println("xiaoming's score is ", num) }
6.11:
_, err = c.Do("zunionstore", "unewzset", 2, "curbike", "tmpdata") if err != nil { fmt.Println("zunionstore failed", err.Error()) } else { res, err = redis.Values(c.Do("zrange", "unewzset", 0, 10)) if err != nil { fmt.Println("zunionstore failed", err.Error()) } else { fmt.Printf("union set are:") for _, v := range res { fmt.Printf("%s ", v.([]byte)) } fmt.Println() } }
6.12:zscore
ret, err := c.Do("zscore", "internewset", "xiaolan") if err != nil { fmt.Println("zscore failed", err.Error()) } else { fmt.Printf("curbike 's xiaolan score is:%s\n", ret) }
6.13:zrevrank
num, err = c.Do("zrevrank", "curbike", "ofo") if err != nil { fmt.Println("zrevrank failed", err.Error()) } else { fmt.Println("ofo's zrevrank is :", num) }
6.14:zrevrangebyscore
res, err = redis.Values(c.Do("zrevrangebyscore", "unewzset", 10, 2)) if err != nil { fmt.Println("zrevrangebyscore failed", err.Error()) } else { fmt.Printf("zrevrangebyscore are:") for _, v := range res { fmt.Printf("%s ", v.([]byte)) } fmt.Println() }
6.15:zrevrange
res, err = redis.Values(c.Do("zrevrange", "unewzset", 0, 10)) err != nil { fmt.Println("zrevrange failed:", err.Error()) } else { fmt.Printf("zrevrange element:") for _, v := range res { fmt.Printf("%s ", v.([]byte)) } fmt.Println() }
6.16:zrem
num, err = c.Do("zrem", "unewzset", "mysql") if err != nil { fmt.Println("zrem failed", err.Error()) } else { fmt.Println("zrem result is:", num) }
6.17:zremrangebyrank
num, err = c.Do("zremrangebyrank", "unewzset", 1, 4) if err != nil { fmt.Println("zremrangebyrank failed", err.Error()) } else { fmt.Println("zremrangebyrank result:", num) }
6.18:zremrangebyscore
num, err = c.Do("zremrangebyscore", "curbike", 2, 5) if err != nil { fmt.Println("zremrangebyscore failed", err.Error()) } else { fmt.Println("zremrangebyscore result:", num) }
完整代碼路徑
補充:go-redis使用之ZSet有序集合
ZSet(sorted set):有序不重復集合
ZSet的每個元素都會關聯(lián)一個float64類型的分數(shù)。redis正是通過分數(shù)來為集合中的成員進行從小到大的排序。
有序集合的成員是的,但分數(shù)(score)卻可以重復。
集合是通過哈希表實現(xiàn)的,所以添加,刪除,查找的復雜度都是O(1)。 集合中較大的成員數(shù)為2的32次方 - 1
func GetRedisClient() *redis.Client { return redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", DB: 0, }) } // redisZsetTest Zset(sorted set有序不重復集合) func redisZsetTest(cli *redis.Client){ lang := []redis.Z{ redis.Z{Score: 90.0, Member: "java"}, redis.Z{Score: 80.0, Member: "go"}, redis.Z{Score: 70.0, Member: "python"}, redis.Z{Score: 60.0, Member: "php"}, redis.Z{Score: 50.0, Member: "ruby"}, } l1:= struct { Score float64 Member interface{} }{ 40, "javaScript", } //l2 :=redis.Z{ // Score: 30, // Member: "Object-C", //} // 添加一個值 cli.ZAdd("lang",l1) // 添加多個值 _,err:=cli.ZAdd("lang",lang...).Result() if err != nil { panic(err) } //升序:查詢zset中指定區(qū)間的成員,-1代表取到最后 fmt.Println("ZRange:",cli.ZRange("lang",0,3).Val()) //[javaScript ruby php python] //降序:查詢zset中指定區(qū)間的成員,-1代表取到最后 fmt.Println("ZRevRange:",cli.ZRevRange("lang",0,-1).Val()) // [java go python php ruby javaScript] // [Go javaScript ruby php python go java] opt:=redis.ZRangeBy{ Min: "50", //最小分數(shù) Max: "90", //較大分數(shù) Offset: 2, //在滿足條件的范圍,從offset下標處開始取值 Count: 3, //查詢結(jié)果集個數(shù) } //升序:根據(jù)opt條件查詢Member成員 fmt.Println(cli.ZRangeByScore("lang",opt).Val()) // [python go java] //降序:根據(jù)opt條件查詢Member成員 fmt.Println(cli.ZRevRangeByScore("lang",opt).Val()) //[python php ruby] //升序:根據(jù)下標范圍返回的redis.Z結(jié)構(gòu)體切片 fmt.Println(cli.ZRangeWithScores("lang",0,3).Val()) //[{40 javaScript} {50 ruby} {60 php} {70 python}] //降序:根據(jù)下標范圍返回的redis.Z結(jié)構(gòu)體切片 fmt.Println(cli.ZRevRangeWithScores("lang",0,-1).Val())//[{90 java} {80 go} {70 python} {60 php} {50 ruby} {40 javaScript}] //升序:根據(jù)opt條件,返回的redis.Z結(jié)構(gòu)體切片 fmt.Println(cli.ZRangeByScoreWithScores("lang",opt).Val()) //降序:根據(jù)opt條件,返回的redis.Z結(jié)構(gòu)體切片 fmt.Println(cli.ZRevRangeByScoreWithScores("lang",opt).Val()) fmt.Println(cli.ZRangeByLex("lang",opt).Val()) fmt.Println(cli.ZRevRangeByLex("lang",opt).Val()) // 獲取指定成員的score f:=cli.ZScore("lang","go").Val() fmt.Println(f) // 80 // 獲取指定成員的下標 fmt.Println(cli.ZRank("lang","python").Val()) //3 // 返回指定區(qū)間的成員個數(shù) fmt.Println(cli.ZCount("lang","50","80").Val())//4 // 返回集合中成員的個數(shù) fmt.Println(cli.ZCard("lang").Val()) //6 // 根據(jù)成員名稱,移除指定成員(可以多個): 0:失敗 0<:成功 fmt.Println(cli.ZRem("lang","c++").Val())//0 fmt.Println(cli.ZRem("lang","javaScript","java").Val())//2 fmt.Println(cli.ZRange("lang",0,-1).Val()) // [ruby php python go] // 升序:根據(jù)下標區(qū)間移除指定成員 fmt.Println(cli.ZRemRangeByRank("lang",1,2).Val()) //2:表示移除了兩個 fmt.Println(cli.ZRangeWithScores("lang",0,-1).Val()) // [{50 ruby} {80 go}] // 升序:根據(jù)分數(shù)區(qū)間移除指定成員 fmt.Println(cli.ZRemRangeByScore("lang","70","90").Val()) //1:表示移除了一個 fmt.Println(cli.ZRangeWithScores("lang",0,-1).Val()) // [{50 ruby}] } func main() { rdb := GetRedisClient() defer rdb.Close() pong := rdb.Ping().Val() fmt.Printf("數(shù)據(jù)庫連接狀態(tài):%v\n", pong) // 數(shù)據(jù)連接狀態(tài):PONG redisZsetTest(rdb) }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設公司,的支持。
當前標題:sortedset類型如何在golang-redis中使用-創(chuàng)新互聯(lián)
鏈接地址:http://jinyejixie.com/article30/dpogpo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、App設計、全網(wǎng)營銷推廣、品牌網(wǎng)站建設、網(wǎng)站內(nèi)鏈、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容