golang sortedset 實現(基於redis skiplist)

來源:互聯網
上載者:User

標籤:

在遊戲中熱門排行榜是很常見的需求,之前一直使用的是redis的sortedset,剛好看到一個lua的實現,於是就移植到golang版本,github地址:https://github.com/Skycrab/go-zset,使用方法和redis 的cli命令基本一致。

     使用了cgo封裝,skiplist的實現基本和redis的一致,雖然cgo用起來很方便,但涉及到C中回調go時比較費事,

感覺還不如lua來得清爽。

        go-zet並沒有提供持久化的方法,我在https://github.com/Skycrab/cham/blob/master/lib/zset/zset.go中提供了持久化。主要是沒有好的持久化方案,不像redis使用rdb格式。在cham中,和redis一樣,fork後寫檔案,

只不過我使用的是json格式。

//use fork snapshot to savefunc (z *zset) BgStore(name string) error {f, err := helper.LockFile(name, true)if err != nil {return err}pid, errno := helper.Fork()if errno != 0 {return errors.New("fork error," + errno.Error())}//childif pid == 0 {buf := bufio.NewWriter(f)encoder := json.NewEncoder(buf)encoder.Encode(z.tbl)buf.Flush()f.Close()os.Exit(0)}return nil}

helper代碼如下:

package helperimport ("os""runtime""syscall")func Fork() (int, syscall.Errno) {r1, r2, err := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)if err != 0 {return 0, err}if runtime.GOOS == "darwin" && r2 == 1 {r1 = 1}return int(r1), 0}// referer http://ikarishinjieva.github.io/blog/blog/2014/03/20/go-file-lock/func LockFile(name string, truncate bool) (*os.File, error) {flag := os.O_RDWR | os.O_CREATEif truncate {flag |= os.O_TRUNC}f, err := os.OpenFile(name, flag, 0666)if err != nil {return nil, err}if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {f.Close()return nil, err}return f, nil}

看看test基本就知道用法了

package zsetimport ("fmt""testing""time")func equal(a []string, b []string) bool {if len(a) != len(b) {return false}for i := 0; i < len(a); i++ {if a[i] != b[i] {return false}}return true}func assert(t *testing.T, ok bool, s string) {if !ok {t.Error(s)}}func TestBase(t *testing.T) {z := New()assert(t, z.Count() == 0, "empty Count error")z.Add(1, "12")z.Add(1, "32")assert(t, z.Count() == 2, "not empty Count error")var score float64var ex boolscore, ex = z.Score("12")assert(t, score == 1, "Score error")z.Add(2, "12")assert(t, z.Count() == 2, "after add duplicate Count error")score, ex = z.Score("12")assert(t, score == 2, "after add Score error")z.Rem("12")assert(t, z.Count() == 1, "after rem Count error")score, ex = z.Score("12")assert(t, ex == false, "not exist Score error")fmt.Println("")}func TestRangeByScore(t *testing.T) {z := New()z.Add(2, "22")z.Add(1, "11")z.Add(3, "33")s := "TestRangeByScore error"assert(t, equal(z.RangeByScore(2, 3), []string{"22", "33"}), s)assert(t, equal(z.RangeByScore(0, 5), []string{"11", "22", "33"}), s)assert(t, equal(z.RangeByScore(10, 5), []string{}), s)assert(t, equal(z.RangeByScore(10, 0), []string{"33", "22", "11"}), s)}func TestRange(t *testing.T) {z := New()z.Add(100.1, "1")z.Add(100.9, "9")z.Add(100.5, "5")assert(t, equal(z.Range(1, 3), []string{"1", "5", "9"}), "Range1 error")assert(t, equal(z.Range(3, 1), []string{"9", "5", "1"}), "Range2 error")assert(t, equal(z.RevRange(1, 2), []string{"9", "5"}), "RevRange1 error")assert(t, equal(z.RevRange(3, 2), []string{"1", "5"}), "RevRange2 error")}func TestRank(t *testing.T) {z := New()assert(t, z.Rank("kehan") == 0, "Rank empty error")z.Add(1111.1111, "kehan")assert(t, z.Rank("kehan") == 1, "Rank error")z.Add(222.2222, "lwy")assert(t, z.Rank("kehan") == 2, "Rank 2 error")assert(t, z.RevRank("kehan") == 1, "RevRank error")}func TestLimit(t *testing.T) {z := New()z.Add(1, "1")z.Add(2, "2")z.Add(3, "3")z.Limit(1)assert(t, z.Count() == 1, "Limit error")assert(t, z.Rank("3") == 0, "Limit Rank error")z.Add(4.4, "4")z.Add(5.5, "5")z.Add(0.5, "0.5")z.Dump()assert(t, z.RevLimit(4) == 0, "RevLimit error")assert(t, z.RevLimit(0) == 4, "RevLimit2 error")}func TestStore(t *testing.T) {z := New()z.Add(1, "1")z.Add(2, "2")z.Add(3, "3")z.BgStore("zdb.json")go func() {time.Sleep(time.Second * 2)fmt.Println("continue add")z.Add(4, "4")z.Add(5, "5")}()}func TestReStore(t *testing.T) {z := New()assert(t, len(z.tbl) == 0, "restore error")z.ReStore("zdb.json")fmt.Println(z.tbl)assert(t, len(z.tbl) != 0, "restore2 error")assert(t, z.Count() == len(z.tbl), "restore3 error")}




golang sortedset 實現(基於redis skiplist)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.