Golang-redis sorted set Type simple operation

Source: Internet
Author: User
Tags redis

1: Install Redigo

Go get Github.com/garyburd/redigo/redis

2: Reference Redigo

Import (
    "Github.com/garyburd/redigo/redis"
)

3: Connect 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 ()

4: Command

N,err: = C.do ("Zadd", "Key", "score", "member")  //write
Result,err: = Redis. Values (C.do ("Zrange", "key", 0,-1))//Read

5:sorted Set Simple operation

Zadd (Key, Score1,member1,... Scoren,membern) adds (updates) one or more members to an ordered combination
Zcard (key): Gets the members of an ordered collection
Zcount (key,start,end ): Calculates the number of members of the specified interval
zincrby (key,increment,member): Member member increase increment
Zinterstore (dst,numkey,src1,src2. SRCN): Seek the intersection and store the result in a new binding
Zlexcount (key,start,end): Computes the number of dictionary interval members (fractions are the same, sorted by dictionary)
Zrange (key,start,end): Gets the members of the index interval
Zrangebylex (Key,start,end): Returns an ordered set member
Zrangebyscore (key,start,end) through a dictionary interval: an ordered set Zrank in the interval by a fraction
( Key,member): Returns an ordered associative index
zrem (KEY,MEMBERS1...MEMBERSN): Deletes one or more members
Zremrangebylex (key,start,end): Removes the member Zremrangebyrank (key,start,end) of the given dictionary interval in the collection
zremrangebyscore (key,start,end) Removes all members of the given rank range in the ordered collection : Removes all elements of a given fractional interval
zrevange (key,start,end): Index, score from high to low, returns the element of the specified range
zrevrangebyscore (key,member) : Score from high to low returns the number of members of the specified interval
zrevrank (key,member): score from small to large, returns the rank of the specified member
zscore (Key,member): Returns the fractional value
of an ordered set, a member Zunionstore (Dst,numkeys,key1...keyn): Returns the union of one or more collections and stores them in a new collection
Zscan (key,cursor): Elements in an orderly union of iterations (including element members and element scores)

6: Example
6.1:zadd

_, Err1: = C.do ("Zadd", "Curbike", 1, "Mobike", 2, "Xiaolan", 3, "Ofo", 4, "Xiaoming")
_, Err2: = C.do ("Zadd", "Tmpdat A ", 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, ten))
    if err! = Nil {
        FMT. Println ("Interstore failed", err. Error ())
    } else {
        fmt. Printf ("Interstore newset elsements is:")
        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 is 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, ten))
    if err! = Nil {
        FMT. Println ("Zunionstore failed", err. Error ())
    } else {
        fmt. PRINTF ("union set is:")
        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", ten, 2))
if err! = Nil {
    FMT. Println ("Zrevrangebyscore failed", err. Error ())
} else {
    fmt. Printf ("Zrevrangebyscore is:")
    for _, V: = Range res {
        FMT. Printf ("%s", V. ([]byte))
    }
    fmt. Println ()
}

6.15:zrevrange

Res, err = Redis. Values (C.do ("Zrevrange", "Unewzset", 0, Ten))
 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)
}

7: Example Results

Full code path

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.