123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- package redis
- import (
- "errors"
- "fmt"
- "strconv"
- "time"
- red "github.com/go-redis/redis"
- "github.com/tal-tech/go-zero/core/breaker"
- "github.com/tal-tech/go-zero/core/mapping"
- )
- const (
- ClusterType = "cluster"
- NodeType = "node"
- Nil = red.Nil
- blockingQueryTimeout = 5 * time.Second
- readWriteTimeout = 2 * time.Second
- slowThreshold = time.Millisecond * 100
- )
- var ErrNilNode = errors.New("nil redis node")
- type (
- Pair struct {
- Key string
- Score int64
- }
- // Redis defines a redis node/cluster. It is thread-safe.
- Redis struct {
- Addr string
- Type string
- Pass string
- brk breaker.Breaker
- }
- RedisNode interface {
- red.Cmdable
- }
- // GeoLocation is used with GeoAdd to add geospatial location.
- GeoLocation = red.GeoLocation
- // GeoRadiusQuery is used with GeoRadius to query geospatial index.
- GeoRadiusQuery = red.GeoRadiusQuery
- GeoPos = red.GeoPos
- Pipeliner = red.Pipeliner
- // Z represents sorted set member.
- Z = red.Z
- ZStore = red.ZStore
- IntCmd = red.IntCmd
- FloatCmd = red.FloatCmd
- )
- func NewRedis(redisAddr, redisType string, redisPass ...string) *Redis {
- var pass string
- for _, v := range redisPass {
- pass = v
- }
- return &Redis{
- Addr: redisAddr,
- Type: redisType,
- Pass: pass,
- brk: breaker.NewBreaker(),
- }
- }
- // BitCount is redis bitcount command implementation.
- func (s *Redis) BitCount(key string, start, end int64) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.BitCount(key, &red.BitCount{
- Start: start,
- End: end,
- }).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpAnd is redis bit operation (and) command implementation.
- func (s *Redis) BitOpAnd(destKey string, keys ...string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.BitOpAnd(destKey, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpNot is redis bit operation (not) command implementation.
- func (s *Redis) BitOpNot(destKey, key string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.BitOpNot(destKey, key).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpOr is redis bit operation (or) command implementation.
- func (s *Redis) BitOpOr(destKey string, keys ...string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.BitOpOr(destKey, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpXor is redis bit operation (xor) command implementation.
- func (s *Redis) BitOpXor(destKey string, keys ...string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.BitOpXor(destKey, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // BitPos is redis bitpos command implementation.
- func (s *Redis) BitPos(key string, bit int64, start, end int64) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.BitPos(key, bit, start, end).Result()
- return err
- }, acceptable)
- return
- }
- // Blpop uses passed in redis connection to execute blocking queries.
- // Doesn't benefit from pooling redis connections of blocking queries
- func (s *Redis) Blpop(redisNode RedisNode, key string) (string, error) {
- if redisNode == nil {
- return "", ErrNilNode
- }
- vals, err := redisNode.BLPop(blockingQueryTimeout, key).Result()
- if err != nil {
- return "", err
- }
- if len(vals) < 2 {
- return "", fmt.Errorf("no value on key: %s", key)
- }
- return vals[1], nil
- }
- func (s *Redis) BlpopEx(redisNode RedisNode, key string) (string, bool, error) {
- if redisNode == nil {
- return "", false, ErrNilNode
- }
- vals, err := redisNode.BLPop(blockingQueryTimeout, key).Result()
- if err != nil {
- return "", false, err
- }
- if len(vals) < 2 {
- return "", false, fmt.Errorf("no value on key: %s", key)
- }
- return vals[1], true, nil
- }
- func (s *Redis) Del(keys ...string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.Del(keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Eval(script string, keys []string, args ...interface{}) (val interface{}, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.Eval(script, keys, args...).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Exists(key string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.Exists(key).Result()
- if err != nil {
- return err
- }
- val = v == 1
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Expire(key string, seconds int) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.Expire(key, time.Duration(seconds)*time.Second).Err()
- }, acceptable)
- }
- func (s *Redis) Expireat(key string, expireTime int64) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.ExpireAt(key, time.Unix(expireTime, 0)).Err()
- }, acceptable)
- }
- func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GeoAdd(key, geoLocation...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) GeoDist(key string, member1, member2, unit string) (val float64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GeoDist(key, member1, member2, unit).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) GeoHash(key string, members ...string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GeoHash(key, members...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) (val []GeoLocation, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GeoRadius(key, longitude, latitude, query).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) (val []GeoLocation, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GeoRadiusByMember(key, member, query).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) GeoPos(key string, members ...string) (val []*GeoPos, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GeoPos(key, members...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Get(key string) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- if val, err = conn.Get(key).Result(); err == red.Nil {
- return nil
- } else if err != nil {
- return err
- } else {
- return nil
- }
- }, acceptable)
- return
- }
- func (s *Redis) GetBit(key string, offset int64) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.GetBit(key, offset).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Hdel(key, field string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.HDel(key, field).Result()
- if err != nil {
- return err
- }
- val = v == 1
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Hexists(key, field string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.HExists(key, field).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Hget(key, field string) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.HGet(key, field).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Hgetall(key string) (val map[string]string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.HGetAll(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Hincrby(key, field string, increment int) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.HIncrBy(key, field, int64(increment)).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Hkeys(key string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.HKeys(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Hlen(key string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.HLen(key).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Hmget(key string, fields ...string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.HMGet(key, fields...).Result()
- if err != nil {
- return err
- }
- val = toStrings(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Hset(key, field, value string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.HSet(key, field, value).Err()
- }, acceptable)
- }
- func (s *Redis) Hsetnx(key, field, value string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.HSetNX(key, field, value).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Hmset(key string, fieldsAndValues map[string]string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- vals := make(map[string]interface{}, len(fieldsAndValues))
- for k, v := range fieldsAndValues {
- vals[k] = v
- }
- return conn.HMSet(key, vals).Err()
- }, acceptable)
- }
- func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- keys, cur, err = conn.HScan(key, cursor, match, count).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Hvals(key string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.HVals(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Incr(key string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.Incr(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Incrby(key string, increment int64) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.IncrBy(key, int64(increment)).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Keys(pattern string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.Keys(pattern).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Llen(key string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.LLen(key).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Lpop(key string) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.LPop(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Lpush(key string, values ...interface{}) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.LPush(key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Lrange(key string, start int, stop int) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.LRange(key, int64(start), int64(stop)).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Lrem(key string, count int, value string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.LRem(key, int64(count), value).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Mget(keys ...string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.MGet(keys...).Result()
- if err != nil {
- return err
- }
- val = toStrings(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Persist(key string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.Persist(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Pfadd(key string, values ...interface{}) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.PFAdd(key, values...).Result()
- if err != nil {
- return err
- }
- val = v == 1
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Pfcount(key string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.PFCount(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Pfmerge(dest string, keys ...string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- _, err = conn.PFMerge(dest, keys...).Result()
- return err
- }, acceptable)
- }
- func (s *Redis) Ping() (val bool) {
- // ignore error, error means false
- _ = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- val = false
- return nil
- }
- v, err := conn.Ping().Result()
- if err != nil {
- val = false
- return nil
- }
- val = v == "PONG"
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Pipelined(fn func(Pipeliner) error) (err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- _, err = conn.Pipelined(fn)
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Rpop(key string) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.RPop(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Rpush(key string, values ...interface{}) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.RPush(key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Sadd(key string, values ...interface{}) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.SAdd(key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- keys, cur, err = conn.Scan(cursor, match, count).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) SetBit(key string, offset int64, value int) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- _, err = conn.SetBit(key, offset, value).Result()
- return err
- }, acceptable)
- }
- func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- keys, cur, err = conn.SScan(key, cursor, match, count).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Scard(key string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SCard(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Set(key string, value string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.Set(key, value, 0).Err()
- }, acceptable)
- }
- func (s *Redis) Setex(key, value string, seconds int) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.Set(key, value, time.Duration(seconds)*time.Second).Err()
- }, acceptable)
- }
- func (s *Redis) Setnx(key, value string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SetNX(key, value, 0).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) SetnxEx(key, value string, seconds int) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SetNX(key, value, time.Duration(seconds)*time.Second).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Sismember(key string, value interface{}) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SIsMember(key, value).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Srem(key string, values ...interface{}) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.SRem(key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Smembers(key string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SMembers(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Spop(key string) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SPop(key).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Srandmember(key string, count int) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SRandMemberN(key, int64(count)).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Sunion(keys ...string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SUnion(keys...).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Sunionstore(destination string, keys ...string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.SUnionStore(destination, keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Sdiff(keys ...string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SDiff(keys...).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Sdiffstore(destination string, keys ...string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.SDiffStore(destination, keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Ttl(key string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- duration, err := conn.TTL(key).Result()
- if err != nil {
- return err
- }
- val = int(duration / time.Second)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zadd(key string, score int64, value string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZAdd(key, red.Z{
- Score: float64(score),
- Member: value,
- }).Result()
- if err != nil {
- return err
- }
- val = v == 1
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zadds(key string, ps ...Pair) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- var zs []red.Z
- for _, p := range ps {
- z := red.Z{Score: float64(p.Score), Member: p.Key}
- zs = append(zs, z)
- }
- v, err := conn.ZAdd(key, zs...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zcard(key string) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZCard(key).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zcount(key string, start, stop int64) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZCount(key, strconv.FormatInt(start, 10), strconv.FormatInt(stop, 10)).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zincrby(key string, increment int64, field string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZIncrBy(key, float64(increment), field).Result()
- if err != nil {
- return err
- }
- val = int64(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zscore(key string, value string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZScore(key, value).Result()
- if err != nil {
- return err
- }
- val = int64(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zrank(key, field string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.ZRank(key, field).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Zrem(key string, values ...interface{}) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRem(key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zremrangebyscore(key string, start, stop int64) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRemRangeByScore(key, strconv.FormatInt(start, 10),
- strconv.FormatInt(stop, 10)).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zremrangebyrank(key string, start, stop int64) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRemRangeByRank(key, start, stop).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zrange(key string, start, stop int64) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.ZRange(key, start, stop).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) ZrangeWithScores(key string, start, stop int64) (val []Pair, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRangeWithScores(key, start, stop).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) ZRevRangeWithScores(key string, start, stop int64) (val []Pair, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRevRangeWithScores(key, start, stop).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) ZrangebyscoreWithScores(key string, start, stop int64) (val []Pair, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRangeByScoreWithScores(key, red.ZRangeBy{
- Min: strconv.FormatInt(start, 10),
- Max: strconv.FormatInt(stop, 10),
- }).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
- val []Pair, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- if size <= 0 {
- return nil
- }
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRangeByScoreWithScores(key, red.ZRangeBy{
- Min: strconv.FormatInt(start, 10),
- Max: strconv.FormatInt(stop, 10),
- Offset: int64(page * size),
- Count: int64(size),
- }).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zrevrange(key string, start, stop int64) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.ZRevRange(key, start, stop).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) ZrevrangebyscoreWithScores(key string, start, stop int64) (val []Pair, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRevRangeByScoreWithScores(key, red.ZRangeBy{
- Min: strconv.FormatInt(start, 10),
- Max: strconv.FormatInt(stop, 10),
- }).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
- val []Pair, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- if size <= 0 {
- return nil
- }
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.ZRevRangeByScoreWithScores(key, red.ZRangeBy{
- Min: strconv.FormatInt(start, 10),
- Max: strconv.FormatInt(stop, 10),
- Offset: int64(page * size),
- Count: int64(size),
- }).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- func (s *Redis) Zrevrank(key string, field string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.ZRevRank(key, field).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) Zunionstore(dest string, store ZStore, keys ...string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.ZUnionStore(dest, store, keys...).Result()
- return err
- }, acceptable)
- return
- }
- func (s *Redis) String() string {
- return s.Addr
- }
- func (s *Redis) scriptLoad(script string) (string, error) {
- conn, err := getRedis(s)
- if err != nil {
- return "", err
- }
- return conn.ScriptLoad(script).Result()
- }
- func acceptable(err error) bool {
- return err == nil || err == red.Nil
- }
- func getRedis(r *Redis) (RedisNode, error) {
- switch r.Type {
- case ClusterType:
- return getCluster(r.Addr, r.Pass)
- case NodeType:
- return getClient(r.Addr, r.Pass)
- default:
- return nil, fmt.Errorf("redis type '%s' is not supported", r.Type)
- }
- }
- func toPairs(vals []red.Z) []Pair {
- pairs := make([]Pair, len(vals))
- for i, val := range vals {
- switch member := val.Member.(type) {
- case string:
- pairs[i] = Pair{
- Key: member,
- Score: int64(val.Score),
- }
- default:
- pairs[i] = Pair{
- Key: mapping.Repr(val.Member),
- Score: int64(val.Score),
- }
- }
- }
- return pairs
- }
- func toStrings(vals []interface{}) []string {
- ret := make([]string, len(vals))
- for i, val := range vals {
- if val == nil {
- ret[i] = ""
- } else {
- switch val := val.(type) {
- case string:
- ret[i] = val
- default:
- ret[i] = mapping.Repr(val)
- }
- }
- }
- return ret
- }
|