12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474 |
- package redis
- import (
- "context"
- "errors"
- "fmt"
- "strconv"
- "time"
- red "github.com/go-redis/redis/v8"
- "github.com/zeromicro/go-zero/core/breaker"
- "github.com/zeromicro/go-zero/core/mapping"
- "github.com/zeromicro/go-zero/core/syncx"
- )
- const (
- // ClusterType means redis cluster.
- ClusterType = "cluster"
- // NodeType means redis node.
- NodeType = "node"
- // Nil is an alias of redis.Nil.
- Nil = red.Nil
- blockingQueryTimeout = 5 * time.Second
- readWriteTimeout = 2 * time.Second
- defaultSlowThreshold = time.Millisecond * 100
- )
- var (
- // ErrNilNode is an error that indicates a nil redis node.
- ErrNilNode = errors.New("nil redis node")
- slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
- )
- type (
- // Option defines the method to customize a Redis.
- Option func(r *Redis)
- // A Pair is a key/pair set used in redis zset.
- Pair struct {
- Key string
- Score int64
- }
- // Redis defines a redis node/cluster. It is thread-safe.
- Redis struct {
- Addr string
- Type string
- Pass string
- tls bool
- brk breaker.Breaker
- }
- // RedisNode interface represents a redis node.
- 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 is used to represent a geo position.
- GeoPos = red.GeoPos
- // Pipeliner is an alias of redis.Pipeliner.
- Pipeliner = red.Pipeliner
- // Z represents sorted set member.
- Z = red.Z
- // ZStore is an alias of redis.ZStore.
- ZStore = red.ZStore
- // IntCmd is an alias of redis.IntCmd.
- IntCmd = red.IntCmd
- // FloatCmd is an alias of redis.FloatCmd.
- FloatCmd = red.FloatCmd
- // StringCmd is an alias of redis.StringCmd.
- StringCmd = red.StringCmd
- )
- // New returns a Redis with given options.
- func New(addr string, opts ...Option) *Redis {
- r := &Redis{
- Addr: addr,
- Type: NodeType,
- brk: breaker.NewBreaker(),
- }
- for _, opt := range opts {
- opt(r)
- }
- return r
- }
- // BitCount is redis bitcount command implementation.
- func (s *Redis) BitCount(key string, start, end int64) (int64, error) {
- return s.BitCountCtx(context.Background(), key, start, end)
- }
- // BitCountCtx is redis bitcount command implementation.
- func (s *Redis) BitCountCtx(ctx context.Context, 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(ctx, 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) (int64, error) {
- return s.BitOpAndCtx(context.Background(), destKey, keys...)
- }
- // BitOpAndCtx is redis bit operation (and) command implementation.
- func (s *Redis) BitOpAndCtx(ctx context.Context, 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(ctx, destKey, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpNot is redis bit operation (not) command implementation.
- func (s *Redis) BitOpNot(destKey, key string) (int64, error) {
- return s.BitOpNotCtx(context.Background(), destKey, key)
- }
- // BitOpNotCtx is redis bit operation (not) command implementation.
- func (s *Redis) BitOpNotCtx(ctx context.Context, 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(ctx, destKey, key).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpOr is redis bit operation (or) command implementation.
- func (s *Redis) BitOpOr(destKey string, keys ...string) (int64, error) {
- return s.BitOpOrCtx(context.Background(), destKey, keys...)
- }
- // BitOpOrCtx is redis bit operation (or) command implementation.
- func (s *Redis) BitOpOrCtx(ctx context.Context, 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(ctx, destKey, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // BitOpXor is redis bit operation (xor) command implementation.
- func (s *Redis) BitOpXor(destKey string, keys ...string) (int64, error) {
- return s.BitOpXorCtx(context.Background(), destKey, keys...)
- }
- // BitOpXorCtx is redis bit operation (xor) command implementation.
- func (s *Redis) BitOpXorCtx(ctx context.Context, 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(ctx, destKey, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // BitPos is redis bitpos command implementation.
- func (s *Redis) BitPos(key string, bit, start, end int64) (int64, error) {
- return s.BitPosCtx(context.Background(), key, bit, start, end)
- }
- // BitPosCtx is redis bitpos command implementation.
- func (s *Redis) BitPosCtx(ctx context.Context, key string, bit, 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(ctx, 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(node RedisNode, key string) (string, error) {
- return s.BlpopCtx(context.Background(), node, key)
- }
- // BlpopCtx uses passed in redis connection to execute blocking queries.
- // Doesn't benefit from pooling redis connections of blocking queries
- func (s *Redis) BlpopCtx(ctx context.Context, node RedisNode, key string) (string, error) {
- if node == nil {
- return "", ErrNilNode
- }
- vals, err := node.BLPop(ctx, 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
- }
- // BlpopEx uses passed in redis connection to execute blpop command.
- // The difference against Blpop is that this method returns a bool to indicate success.
- func (s *Redis) BlpopEx(node RedisNode, key string) (string, bool, error) {
- return s.BlpopExCtx(context.Background(), node, key)
- }
- // BlpopExCtx uses passed in redis connection to execute blpop command.
- // The difference against Blpop is that this method returns a bool to indicate success.
- func (s *Redis) BlpopExCtx(ctx context.Context, node RedisNode, key string) (string, bool, error) {
- if node == nil {
- return "", false, ErrNilNode
- }
- vals, err := node.BLPop(ctx, 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
- }
- // BlpopWithTimeout uses passed in redis connection to execute blpop command.
- // Control blocking query timeout
- func (s *Redis) BlpopWithTimeout(node RedisNode, timeout time.Duration, key string) (string, error) {
- return s.BlpopWithTimeoutCtx(context.Background(), node, timeout, key)
- }
- // BlpopWithTimeoutCtx uses passed in redis connection to execute blpop command.
- // Control blocking query timeout
- func (s *Redis) BlpopWithTimeoutCtx(ctx context.Context, node RedisNode, timeout time.Duration, key string) (string, error) {
- if node == nil {
- return "", ErrNilNode
- }
- vals, err := node.BLPop(ctx, timeout, key).Result()
- if err != nil {
- return "", err
- }
- if len(vals) < 2 {
- return "", fmt.Errorf("no value on key: %s", key)
- }
- return vals[1], nil
- }
- // Decr is the implementation of redis decr command.
- func (s *Redis) Decr(key string) (int64, error) {
- return s.DecrCtx(context.Background(), key)
- }
- // DecrCtx is the implementation of redis decr command.
- func (s *Redis) DecrCtx(ctx context.Context, key string) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.Decr(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Decrby is the implementation of redis decrby command.
- func (s *Redis) Decrby(key string, decrement int64) (int64, error) {
- return s.DecrbyCtx(context.Background(), key, decrement)
- }
- // DecrbyCtx is the implementation of redis decrby command.
- func (s *Redis) DecrbyCtx(ctx context.Context, key string, decrement int64) (val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.DecrBy(ctx, key, decrement).Result()
- return err
- }, acceptable)
- return
- }
- // Del deletes keys.
- func (s *Redis) Del(keys ...string) (int, error) {
- return s.DelCtx(context.Background(), keys...)
- }
- // DelCtx deletes keys.
- func (s *Redis) DelCtx(ctx context.Context, 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(ctx, keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Eval is the implementation of redis eval command.
- func (s *Redis) Eval(script string, keys []string, args ...interface{}) (interface{}, error) {
- return s.EvalCtx(context.Background(), script, keys, args...)
- }
- // EvalCtx is the implementation of redis eval command.
- func (s *Redis) EvalCtx(ctx context.Context, 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(ctx, script, keys, args...).Result()
- return err
- }, acceptable)
- return
- }
- // EvalSha is the implementation of redis evalsha command.
- func (s *Redis) EvalSha(sha string, keys []string, args ...interface{}) (interface{}, error) {
- return s.EvalShaCtx(context.Background(), sha, keys, args...)
- }
- // EvalShaCtx is the implementation of redis evalsha command.
- func (s *Redis) EvalShaCtx(ctx context.Context, sha 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.EvalSha(ctx, sha, keys, args...).Result()
- return err
- }, acceptable)
- return
- }
- // Exists is the implementation of redis exists command.
- func (s *Redis) Exists(key string) (bool, error) {
- return s.ExistsCtx(context.Background(), key)
- }
- // ExistsCtx is the implementation of redis exists command.
- func (s *Redis) ExistsCtx(ctx context.Context, 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(ctx, key).Result()
- if err != nil {
- return err
- }
- val = v == 1
- return nil
- }, acceptable)
- return
- }
- // Expire is the implementation of redis expire command.
- func (s *Redis) Expire(key string, seconds int) error {
- return s.ExpireCtx(context.Background(), key, seconds)
- }
- // ExpireCtx is the implementation of redis expire command.
- func (s *Redis) ExpireCtx(ctx context.Context, key string, seconds int) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.Expire(ctx, key, time.Duration(seconds)*time.Second).Err()
- }, acceptable)
- }
- // Expireat is the implementation of redis expireat command.
- func (s *Redis) Expireat(key string, expireTime int64) error {
- return s.ExpireatCtx(context.Background(), key, expireTime)
- }
- // ExpireatCtx is the implementation of redis expireat command.
- func (s *Redis) ExpireatCtx(ctx context.Context, key string, expireTime int64) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.ExpireAt(ctx, key, time.Unix(expireTime, 0)).Err()
- }, acceptable)
- }
- // GeoAdd is the implementation of redis geoadd command.
- func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (int64, error) {
- return s.GeoAddCtx(context.Background(), key, geoLocation...)
- }
- // GeoAddCtx is the implementation of redis geoadd command.
- func (s *Redis) GeoAddCtx(ctx context.Context, 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(ctx, key, geoLocation...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // GeoDist is the implementation of redis geodist command.
- func (s *Redis) GeoDist(key, member1, member2, unit string) (float64, error) {
- return s.GeoDistCtx(context.Background(), key, member1, member2, unit)
- }
- // GeoDistCtx is the implementation of redis geodist command.
- func (s *Redis) GeoDistCtx(ctx context.Context, key, 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(ctx, key, member1, member2, unit).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // GeoHash is the implementation of redis geohash command.
- func (s *Redis) GeoHash(key string, members ...string) ([]string, error) {
- return s.GeoHashCtx(context.Background(), key, members...)
- }
- // GeoHashCtx is the implementation of redis geohash command.
- func (s *Redis) GeoHashCtx(ctx context.Context, 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(ctx, key, members...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // GeoRadius is the implementation of redis georadius command.
- func (s *Redis) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) (
- []GeoLocation, error) {
- return s.GeoRadiusCtx(context.Background(), key, longitude, latitude, query)
- }
- // GeoRadiusCtx is the implementation of redis georadius command.
- func (s *Redis) GeoRadiusCtx(ctx context.Context, 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(ctx, key, longitude, latitude, query).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // GeoRadiusByMember is the implementation of redis georadiusbymember command.
- func (s *Redis) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) ([]GeoLocation, error) {
- return s.GeoRadiusByMemberCtx(context.Background(), key, member, query)
- }
- // GeoRadiusByMemberCtx is the implementation of redis georadiusbymember command.
- func (s *Redis) GeoRadiusByMemberCtx(ctx context.Context, 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(ctx, key, member, query).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // GeoPos is the implementation of redis geopos command.
- func (s *Redis) GeoPos(key string, members ...string) ([]*GeoPos, error) {
- return s.GeoPosCtx(context.Background(), key, members...)
- }
- // GeoPosCtx is the implementation of redis geopos command.
- func (s *Redis) GeoPosCtx(ctx context.Context, 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(ctx, key, members...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // Get is the implementation of redis get command.
- func (s *Redis) Get(key string) (string, error) {
- return s.GetCtx(context.Background(), key)
- }
- // GetCtx is the implementation of redis get command.
- func (s *Redis) GetCtx(ctx context.Context, 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(ctx, key).Result(); err == red.Nil {
- return nil
- } else if err != nil {
- return err
- } else {
- return nil
- }
- }, acceptable)
- return
- }
- // GetBit is the implementation of redis getbit command.
- func (s *Redis) GetBit(key string, offset int64) (int, error) {
- return s.GetBitCtx(context.Background(), key, offset)
- }
- // GetBitCtx is the implementation of redis getbit command.
- func (s *Redis) GetBitCtx(ctx context.Context, 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(ctx, key, offset).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // GetSet is the implementation of redis getset command.
- func (s *Redis) GetSet(key, value string) (string, error) {
- return s.GetSetCtx(context.Background(), key, value)
- }
- // GetSetCtx is the implementation of redis getset command.
- func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
- return nil
- }
- return err
- }, acceptable)
- return
- }
- // Hdel is the implementation of redis hdel command.
- func (s *Redis) Hdel(key string, fields ...string) (bool, error) {
- return s.HdelCtx(context.Background(), key, fields...)
- }
- // HdelCtx is the implementation of redis hdel command.
- func (s *Redis) HdelCtx(ctx context.Context, key string, fields ...string) (val bool, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.HDel(ctx, key, fields...).Result()
- if err != nil {
- return err
- }
- val = v >= 1
- return nil
- }, acceptable)
- return
- }
- // Hexists is the implementation of redis hexists command.
- func (s *Redis) Hexists(key, field string) (bool, error) {
- return s.HexistsCtx(context.Background(), key, field)
- }
- // HexistsCtx is the implementation of redis hexists command.
- func (s *Redis) HexistsCtx(ctx context.Context, 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(ctx, key, field).Result()
- return err
- }, acceptable)
- return
- }
- // Hget is the implementation of redis hget command.
- func (s *Redis) Hget(key, field string) (string, error) {
- return s.HgetCtx(context.Background(), key, field)
- }
- // HgetCtx is the implementation of redis hget command.
- func (s *Redis) HgetCtx(ctx context.Context, 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(ctx, key, field).Result()
- return err
- }, acceptable)
- return
- }
- // Hgetall is the implementation of redis hgetall command.
- func (s *Redis) Hgetall(key string) (map[string]string, error) {
- return s.HgetallCtx(context.Background(), key)
- }
- // HgetallCtx is the implementation of redis hgetall command.
- func (s *Redis) HgetallCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Hincrby is the implementation of redis hincrby command.
- func (s *Redis) Hincrby(key, field string, increment int) (int, error) {
- return s.HincrbyCtx(context.Background(), key, field, increment)
- }
- // HincrbyCtx is the implementation of redis hincrby command.
- func (s *Redis) HincrbyCtx(ctx context.Context, 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(ctx, key, field, int64(increment)).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Hkeys is the implementation of redis hkeys command.
- func (s *Redis) Hkeys(key string) ([]string, error) {
- return s.HkeysCtx(context.Background(), key)
- }
- // HkeysCtx is the implementation of redis hkeys command.
- func (s *Redis) HkeysCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Hlen is the implementation of redis hlen command.
- func (s *Redis) Hlen(key string) (int, error) {
- return s.HlenCtx(context.Background(), key)
- }
- // HlenCtx is the implementation of redis hlen command.
- func (s *Redis) HlenCtx(ctx context.Context, 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(ctx, key).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Hmget is the implementation of redis hmget command.
- func (s *Redis) Hmget(key string, fields ...string) ([]string, error) {
- return s.HmgetCtx(context.Background(), key, fields...)
- }
- // HmgetCtx is the implementation of redis hmget command.
- func (s *Redis) HmgetCtx(ctx context.Context, 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(ctx, key, fields...).Result()
- if err != nil {
- return err
- }
- val = toStrings(v)
- return nil
- }, acceptable)
- return
- }
- // Hset is the implementation of redis hset command.
- func (s *Redis) Hset(key, field, value string) error {
- return s.HsetCtx(context.Background(), key, field, value)
- }
- // HsetCtx is the implementation of redis hset command.
- func (s *Redis) HsetCtx(ctx context.Context, key, field, value string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.HSet(ctx, key, field, value).Err()
- }, acceptable)
- }
- // Hsetnx is the implementation of redis hsetnx command.
- func (s *Redis) Hsetnx(key, field, value string) (bool, error) {
- return s.HsetnxCtx(context.Background(), key, field, value)
- }
- // HsetnxCtx is the implementation of redis hsetnx command.
- func (s *Redis) HsetnxCtx(ctx context.Context, 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(ctx, key, field, value).Result()
- return err
- }, acceptable)
- return
- }
- // Hmset is the implementation of redis hmset command.
- func (s *Redis) Hmset(key string, fieldsAndValues map[string]string) error {
- return s.HmsetCtx(context.Background(), key, fieldsAndValues)
- }
- // HmsetCtx is the implementation of redis hmset command.
- func (s *Redis) HmsetCtx(ctx context.Context, 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(ctx, key, vals).Err()
- }, acceptable)
- }
- // Hscan is the implementation of redis hscan command.
- func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) (
- keys []string, cur uint64, err error) {
- return s.HscanCtx(context.Background(), key, cursor, match, count)
- }
- // HscanCtx is the implementation of redis hscan command.
- func (s *Redis) HscanCtx(ctx context.Context, 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(ctx, key, cursor, match, count).Result()
- return err
- }, acceptable)
- return
- }
- // Hvals is the implementation of redis hvals command.
- func (s *Redis) Hvals(key string) ([]string, error) {
- return s.HvalsCtx(context.Background(), key)
- }
- // HvalsCtx is the implementation of redis hvals command.
- func (s *Redis) HvalsCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Incr is the implementation of redis incr command.
- func (s *Redis) Incr(key string) (int64, error) {
- return s.IncrCtx(context.Background(), key)
- }
- // IncrCtx is the implementation of redis incr command.
- func (s *Redis) IncrCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Incrby is the implementation of redis incrby command.
- func (s *Redis) Incrby(key string, increment int64) (int64, error) {
- return s.IncrbyCtx(context.Background(), key, increment)
- }
- // IncrbyCtx is the implementation of redis incrby command.
- func (s *Redis) IncrbyCtx(ctx context.Context, 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(ctx, key, increment).Result()
- return err
- }, acceptable)
- return
- }
- // Keys is the implementation of redis keys command.
- func (s *Redis) Keys(pattern string) ([]string, error) {
- return s.KeysCtx(context.Background(), pattern)
- }
- // KeysCtx is the implementation of redis keys command.
- func (s *Redis) KeysCtx(ctx context.Context, 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(ctx, pattern).Result()
- return err
- }, acceptable)
- return
- }
- // Llen is the implementation of redis llen command.
- func (s *Redis) Llen(key string) (int, error) {
- return s.LlenCtx(context.Background(), key)
- }
- // LlenCtx is the implementation of redis llen command.
- func (s *Redis) LlenCtx(ctx context.Context, 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(ctx, key).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Lindex is the implementation of redis lindex command.
- func (s *Redis) Lindex(key string, index int64) (string, error) {
- return s.LindexCtx(context.Background(), key, index)
- }
- // LindexCtx is the implementation of redis lindex command.
- func (s *Redis) LindexCtx(ctx context.Context, key string, index int64) (val string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.LIndex(ctx, key, index).Result()
- return err
- }, acceptable)
- return
- }
- // Lpop is the implementation of redis lpop command.
- func (s *Redis) Lpop(key string) (string, error) {
- return s.LpopCtx(context.Background(), key)
- }
- // LpopCtx is the implementation of redis lpop command.
- func (s *Redis) LpopCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Lpush is the implementation of redis lpush command.
- func (s *Redis) Lpush(key string, values ...interface{}) (int, error) {
- return s.LpushCtx(context.Background(), key, values...)
- }
- // LpushCtx is the implementation of redis lpush command.
- func (s *Redis) LpushCtx(ctx context.Context, 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(ctx, key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Lrange is the implementation of redis lrange command.
- func (s *Redis) Lrange(key string, start, stop int) ([]string, error) {
- return s.LrangeCtx(context.Background(), key, start, stop)
- }
- // LrangeCtx is the implementation of redis lrange command.
- func (s *Redis) LrangeCtx(ctx context.Context, key string, start, 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(ctx, key, int64(start), int64(stop)).Result()
- return err
- }, acceptable)
- return
- }
- // Lrem is the implementation of redis lrem command.
- func (s *Redis) Lrem(key string, count int, value string) (int, error) {
- return s.LremCtx(context.Background(), key, count, value)
- }
- // LremCtx is the implementation of redis lrem command.
- func (s *Redis) LremCtx(ctx context.Context, 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(ctx, key, int64(count), value).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Ltrim is the implementation of redis ltrim command.
- func (s *Redis) Ltrim(key string, start, stop int64) error {
- return s.LtrimCtx(context.Background(), key, start, stop)
- }
- // LtrimCtx is the implementation of redis ltrim command.
- func (s *Redis) LtrimCtx(ctx context.Context, key string, start, stop int64) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.LTrim(ctx, key, start, stop).Err()
- }, acceptable)
- }
- // Mget is the implementation of redis mget command.
- func (s *Redis) Mget(keys ...string) ([]string, error) {
- return s.MgetCtx(context.Background(), keys...)
- }
- // MgetCtx is the implementation of redis mget command.
- func (s *Redis) MgetCtx(ctx context.Context, 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(ctx, keys...).Result()
- if err != nil {
- return err
- }
- val = toStrings(v)
- return nil
- }, acceptable)
- return
- }
- // Persist is the implementation of redis persist command.
- func (s *Redis) Persist(key string) (bool, error) {
- return s.PersistCtx(context.Background(), key)
- }
- // PersistCtx is the implementation of redis persist command.
- func (s *Redis) PersistCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Pfadd is the implementation of redis pfadd command.
- func (s *Redis) Pfadd(key string, values ...interface{}) (bool, error) {
- return s.PfaddCtx(context.Background(), key, values...)
- }
- // PfaddCtx is the implementation of redis pfadd command.
- func (s *Redis) PfaddCtx(ctx context.Context, 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(ctx, key, values...).Result()
- if err != nil {
- return err
- }
- val = v >= 1
- return nil
- }, acceptable)
- return
- }
- // Pfcount is the implementation of redis pfcount command.
- func (s *Redis) Pfcount(key string) (int64, error) {
- return s.PfcountCtx(context.Background(), key)
- }
- // PfcountCtx is the implementation of redis pfcount command.
- func (s *Redis) PfcountCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Pfmerge is the implementation of redis pfmerge command.
- func (s *Redis) Pfmerge(dest string, keys ...string) error {
- return s.PfmergeCtx(context.Background(), dest, keys...)
- }
- // PfmergeCtx is the implementation of redis pfmerge command.
- func (s *Redis) PfmergeCtx(ctx context.Context, dest string, keys ...string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- _, err = conn.PFMerge(ctx, dest, keys...).Result()
- return err
- }, acceptable)
- }
- // Ping is the implementation of redis ping command.
- func (s *Redis) Ping() bool {
- return s.PingCtx(context.Background())
- }
- // PingCtx is the implementation of redis ping command.
- func (s *Redis) PingCtx(ctx context.Context) (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(ctx).Result()
- if err != nil {
- val = false
- return nil
- }
- val = v == "PONG"
- return nil
- }, acceptable)
- return
- }
- // Pipelined lets fn execute pipelined commands.
- func (s *Redis) Pipelined(fn func(Pipeliner) error) error {
- return s.PipelinedCtx(context.Background(), fn)
- }
- // PipelinedCtx lets fn execute pipelined commands.
- // Results need to be retrieved by calling Pipeline.Exec()
- func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- _, err = conn.Pipelined(ctx, fn)
- return err
- }, acceptable)
- }
- // Rpop is the implementation of redis rpop command.
- func (s *Redis) Rpop(key string) (string, error) {
- return s.RpopCtx(context.Background(), key)
- }
- // RpopCtx is the implementation of redis rpop command.
- func (s *Redis) RpopCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Rpush is the implementation of redis rpush command.
- func (s *Redis) Rpush(key string, values ...interface{}) (int, error) {
- return s.RpushCtx(context.Background(), key, values...)
- }
- // RpushCtx is the implementation of redis rpush command.
- func (s *Redis) RpushCtx(ctx context.Context, 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(ctx, key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Sadd is the implementation of redis sadd command.
- func (s *Redis) Sadd(key string, values ...interface{}) (int, error) {
- return s.SaddCtx(context.Background(), key, values...)
- }
- // SaddCtx is the implementation of redis sadd command.
- func (s *Redis) SaddCtx(ctx context.Context, 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(ctx, key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Scan is the implementation of redis scan command.
- func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
- return s.ScanCtx(context.Background(), cursor, match, count)
- }
- // ScanCtx is the implementation of redis scan command.
- func (s *Redis) ScanCtx(ctx context.Context, 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(ctx, cursor, match, count).Result()
- return err
- }, acceptable)
- return
- }
- // SetBit is the implementation of redis setbit command.
- func (s *Redis) SetBit(key string, offset int64, value int) (int, error) {
- return s.SetBitCtx(context.Background(), key, offset, value)
- }
- // SetBitCtx is the implementation of redis setbit command.
- func (s *Redis) SetBitCtx(ctx context.Context, key string, offset int64, value int) (val int, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- v, err := conn.SetBit(ctx, key, offset, value).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Sscan is the implementation of redis sscan command.
- func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) (
- keys []string, cur uint64, err error) {
- return s.SscanCtx(context.Background(), key, cursor, match, count)
- }
- // SscanCtx is the implementation of redis sscan command.
- func (s *Redis) SscanCtx(ctx context.Context, 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(ctx, key, cursor, match, count).Result()
- return err
- }, acceptable)
- return
- }
- // Scard is the implementation of redis scard command.
- func (s *Redis) Scard(key string) (int64, error) {
- return s.ScardCtx(context.Background(), key)
- }
- // ScardCtx is the implementation of redis scard command.
- func (s *Redis) ScardCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // ScriptLoad is the implementation of redis script load command.
- func (s *Redis) ScriptLoad(script string) (string, error) {
- return s.ScriptLoadCtx(context.Background(), script)
- }
- // ScriptLoadCtx is the implementation of redis script load command.
- func (s *Redis) ScriptLoadCtx(ctx context.Context, script string) (string, error) {
- conn, err := getRedis(s)
- if err != nil {
- return "", err
- }
- return conn.ScriptLoad(ctx, script).Result()
- }
- // Set is the implementation of redis set command.
- func (s *Redis) Set(key, value string) error {
- return s.SetCtx(context.Background(), key, value)
- }
- // SetCtx is the implementation of redis set command.
- func (s *Redis) SetCtx(ctx context.Context, key, value string) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.Set(ctx, key, value, 0).Err()
- }, acceptable)
- }
- // Setex is the implementation of redis setex command.
- func (s *Redis) Setex(key, value string, seconds int) error {
- return s.SetexCtx(context.Background(), key, value, seconds)
- }
- // SetexCtx is the implementation of redis setex command.
- func (s *Redis) SetexCtx(ctx context.Context, key, value string, seconds int) error {
- return s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- return conn.Set(ctx, key, value, time.Duration(seconds)*time.Second).Err()
- }, acceptable)
- }
- // Setnx is the implementation of redis setnx command.
- func (s *Redis) Setnx(key, value string) (bool, error) {
- return s.SetnxCtx(context.Background(), key, value)
- }
- // SetnxCtx is the implementation of redis setnx command.
- func (s *Redis) SetnxCtx(ctx context.Context, 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(ctx, key, value, 0).Result()
- return err
- }, acceptable)
- return
- }
- // SetnxEx is the implementation of redis setnx command with expire.
- func (s *Redis) SetnxEx(key, value string, seconds int) (bool, error) {
- return s.SetnxExCtx(context.Background(), key, value, seconds)
- }
- // SetnxExCtx is the implementation of redis setnx command with expire.
- func (s *Redis) SetnxExCtx(ctx context.Context, 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(ctx, key, value, time.Duration(seconds)*time.Second).Result()
- return err
- }, acceptable)
- return
- }
- // Sismember is the implementation of redis sismember command.
- func (s *Redis) Sismember(key string, value interface{}) (bool, error) {
- return s.SismemberCtx(context.Background(), key, value)
- }
- // SismemberCtx is the implementation of redis sismember command.
- func (s *Redis) SismemberCtx(ctx context.Context, 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(ctx, key, value).Result()
- return err
- }, acceptable)
- return
- }
- // Smembers is the implementation of redis smembers command.
- func (s *Redis) Smembers(key string) ([]string, error) {
- return s.SmembersCtx(context.Background(), key)
- }
- // SmembersCtx is the implementation of redis smembers command.
- func (s *Redis) SmembersCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Spop is the implementation of redis spop command.
- func (s *Redis) Spop(key string) (string, error) {
- return s.SpopCtx(context.Background(), key)
- }
- // SpopCtx is the implementation of redis spop command.
- func (s *Redis) SpopCtx(ctx context.Context, 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(ctx, key).Result()
- return err
- }, acceptable)
- return
- }
- // Srandmember is the implementation of redis srandmember command.
- func (s *Redis) Srandmember(key string, count int) ([]string, error) {
- return s.SrandmemberCtx(context.Background(), key, count)
- }
- // SrandmemberCtx is the implementation of redis srandmember command.
- func (s *Redis) SrandmemberCtx(ctx context.Context, 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(ctx, key, int64(count)).Result()
- return err
- }, acceptable)
- return
- }
- // Srem is the implementation of redis srem command.
- func (s *Redis) Srem(key string, values ...interface{}) (int, error) {
- return s.SremCtx(context.Background(), key, values...)
- }
- // SremCtx is the implementation of redis srem command.
- func (s *Redis) SremCtx(ctx context.Context, 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(ctx, key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // String returns the string representation of s.
- func (s *Redis) String() string {
- return s.Addr
- }
- // Sunion is the implementation of redis sunion command.
- func (s *Redis) Sunion(keys ...string) ([]string, error) {
- return s.SunionCtx(context.Background(), keys...)
- }
- // SunionCtx is the implementation of redis sunion command.
- func (s *Redis) SunionCtx(ctx context.Context, 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(ctx, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // Sunionstore is the implementation of redis sunionstore command.
- func (s *Redis) Sunionstore(destination string, keys ...string) (int, error) {
- return s.SunionstoreCtx(context.Background(), destination, keys...)
- }
- // SunionstoreCtx is the implementation of redis sunionstore command.
- func (s *Redis) SunionstoreCtx(ctx context.Context, 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(ctx, destination, keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Sdiff is the implementation of redis sdiff command.
- func (s *Redis) Sdiff(keys ...string) ([]string, error) {
- return s.SdiffCtx(context.Background(), keys...)
- }
- // SdiffCtx is the implementation of redis sdiff command.
- func (s *Redis) SdiffCtx(ctx context.Context, 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(ctx, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // Sdiffstore is the implementation of redis sdiffstore command.
- func (s *Redis) Sdiffstore(destination string, keys ...string) (int, error) {
- return s.SdiffstoreCtx(context.Background(), destination, keys...)
- }
- // SdiffstoreCtx is the implementation of redis sdiffstore command.
- func (s *Redis) SdiffstoreCtx(ctx context.Context, 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(ctx, destination, keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Sinter is the implementation of redis sinter command.
- func (s *Redis) Sinter(keys ...string) ([]string, error) {
- return s.SinterCtx(context.Background(), keys...)
- }
- // SinterCtx is the implementation of redis sinter command.
- func (s *Redis) SinterCtx(ctx context.Context, keys ...string) (val []string, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.SInter(ctx, keys...).Result()
- return err
- }, acceptable)
- return
- }
- // Sinterstore is the implementation of redis sinterstore command.
- func (s *Redis) Sinterstore(destination string, keys ...string) (int, error) {
- return s.SinterstoreCtx(context.Background(), destination, keys...)
- }
- // SinterstoreCtx is the implementation of redis sinterstore command.
- func (s *Redis) SinterstoreCtx(ctx context.Context, 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.SInterStore(ctx, destination, keys...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Ttl is the implementation of redis ttl command.
- func (s *Redis) Ttl(key string) (int, error) {
- return s.TtlCtx(context.Background(), key)
- }
- // TtlCtx is the implementation of redis ttl command.
- func (s *Redis) TtlCtx(ctx context.Context, 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(ctx, key).Result()
- if err != nil {
- return err
- }
- val = int(duration / time.Second)
- return nil
- }, acceptable)
- return
- }
- // Zadd is the implementation of redis zadd command.
- func (s *Redis) Zadd(key string, score int64, value string) (bool, error) {
- return s.ZaddCtx(context.Background(), key, score, value)
- }
- // ZaddFloat is the implementation of redis zadd command.
- func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) {
- return s.ZaddFloatCtx(context.Background(), key, score, value)
- }
- // ZaddCtx is the implementation of redis zadd command.
- func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
- val bool, err error) {
- return s.ZaddFloatCtx(ctx, key, float64(score), value)
- }
- // ZaddFloatCtx is the implementation of redis zadd command.
- func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, 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(ctx, key, &red.Z{
- Score: score,
- Member: value,
- }).Result()
- if err != nil {
- return err
- }
- val = v == 1
- return nil
- }, acceptable)
- return
- }
- // Zadds is the implementation of redis zadds command.
- func (s *Redis) Zadds(key string, ps ...Pair) (int64, error) {
- return s.ZaddsCtx(context.Background(), key, ps...)
- }
- // ZaddsCtx is the implementation of redis zadds command.
- func (s *Redis) ZaddsCtx(ctx context.Context, 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(ctx, key, zs...).Result()
- if err != nil {
- return err
- }
- val = v
- return nil
- }, acceptable)
- return
- }
- // Zcard is the implementation of redis zcard command.
- func (s *Redis) Zcard(key string) (int, error) {
- return s.ZcardCtx(context.Background(), key)
- }
- // ZcardCtx is the implementation of redis zcard command.
- func (s *Redis) ZcardCtx(ctx context.Context, 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(ctx, key).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Zcount is the implementation of redis zcount command.
- func (s *Redis) Zcount(key string, start, stop int64) (int, error) {
- return s.ZcountCtx(context.Background(), key, start, stop)
- }
- // ZcountCtx is the implementation of redis zcount command.
- func (s *Redis) ZcountCtx(ctx context.Context, 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(ctx, key, strconv.FormatInt(start, 10),
- strconv.FormatInt(stop, 10)).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Zincrby is the implementation of redis zincrby command.
- func (s *Redis) Zincrby(key string, increment int64, field string) (int64, error) {
- return s.ZincrbyCtx(context.Background(), key, increment, field)
- }
- // ZincrbyCtx is the implementation of redis zincrby command.
- func (s *Redis) ZincrbyCtx(ctx context.Context, 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(ctx, key, float64(increment), field).Result()
- if err != nil {
- return err
- }
- val = int64(v)
- return nil
- }, acceptable)
- return
- }
- // Zscore is the implementation of redis zscore command.
- func (s *Redis) Zscore(key, value string) (int64, error) {
- return s.ZscoreCtx(context.Background(), key, value)
- }
- // ZscoreCtx is the implementation of redis zscore command.
- func (s *Redis) ZscoreCtx(ctx context.Context, key, 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(ctx, key, value).Result()
- if err != nil {
- return err
- }
- val = int64(v)
- return nil
- }, acceptable)
- return
- }
- // Zrank is the implementation of redis zrank command.
- func (s *Redis) Zrank(key, field string) (int64, error) {
- return s.ZrankCtx(context.Background(), key, field)
- }
- // ZrankCtx is the implementation of redis zrank command.
- func (s *Redis) ZrankCtx(ctx context.Context, 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(ctx, key, field).Result()
- return err
- }, acceptable)
- return
- }
- // Zrem is the implementation of redis zrem command.
- func (s *Redis) Zrem(key string, values ...interface{}) (int, error) {
- return s.ZremCtx(context.Background(), key, values...)
- }
- // ZremCtx is the implementation of redis zrem command.
- func (s *Redis) ZremCtx(ctx context.Context, 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(ctx, key, values...).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Zremrangebyscore is the implementation of redis zremrangebyscore command.
- func (s *Redis) Zremrangebyscore(key string, start, stop int64) (int, error) {
- return s.ZremrangebyscoreCtx(context.Background(), key, start, stop)
- }
- // ZremrangebyscoreCtx is the implementation of redis zremrangebyscore command.
- func (s *Redis) ZremrangebyscoreCtx(ctx context.Context, 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(ctx, key, strconv.FormatInt(start, 10),
- strconv.FormatInt(stop, 10)).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Zremrangebyrank is the implementation of redis zremrangebyrank command.
- func (s *Redis) Zremrangebyrank(key string, start, stop int64) (int, error) {
- return s.ZremrangebyrankCtx(context.Background(), key, start, stop)
- }
- // ZremrangebyrankCtx is the implementation of redis zremrangebyrank command.
- func (s *Redis) ZremrangebyrankCtx(ctx context.Context, 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(ctx, key, start, stop).Result()
- if err != nil {
- return err
- }
- val = int(v)
- return nil
- }, acceptable)
- return
- }
- // Zrange is the implementation of redis zrange command.
- func (s *Redis) Zrange(key string, start, stop int64) ([]string, error) {
- return s.ZrangeCtx(context.Background(), key, start, stop)
- }
- // ZrangeCtx is the implementation of redis zrange command.
- func (s *Redis) ZrangeCtx(ctx context.Context, 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(ctx, key, start, stop).Result()
- return err
- }, acceptable)
- return
- }
- // ZrangeWithScores is the implementation of redis zrange command with scores.
- func (s *Redis) ZrangeWithScores(key string, start, stop int64) ([]Pair, error) {
- return s.ZrangeWithScoresCtx(context.Background(), key, start, stop)
- }
- // ZrangeWithScoresCtx is the implementation of redis zrange command with scores.
- func (s *Redis) ZrangeWithScoresCtx(ctx context.Context, 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(ctx, key, start, stop).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- // ZRevRangeWithScores is the implementation of redis zrevrange command with scores.
- func (s *Redis) ZRevRangeWithScores(key string, start, stop int64) ([]Pair, error) {
- return s.ZRevRangeWithScoresCtx(context.Background(), key, start, stop)
- }
- // ZRevRangeWithScoresCtx is the implementation of redis zrevrange command with scores.
- func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, 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(ctx, key, start, stop).Result()
- if err != nil {
- return err
- }
- val = toPairs(v)
- return nil
- }, acceptable)
- return
- }
- // ZrangebyscoreWithScores is the implementation of redis zrangebyscore command with scores.
- func (s *Redis) ZrangebyscoreWithScores(key string, start, stop int64) ([]Pair, error) {
- return s.ZrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
- }
- // ZrangebyscoreWithScoresCtx is the implementation of redis zrangebyscore command with scores.
- func (s *Redis) ZrangebyscoreWithScoresCtx(ctx context.Context, 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(ctx, 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
- }
- // ZrangebyscoreWithScoresAndLimit is the implementation of redis zrangebyscore command
- // with scores and limit.
- func (s *Redis) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64,
- page, size int) ([]Pair, error) {
- return s.ZrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
- }
- // ZrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrangebyscore command
- // with scores and limit.
- func (s *Redis) ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, 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(ctx, 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
- }
- // Zrevrange is the implementation of redis zrevrange command.
- func (s *Redis) Zrevrange(key string, start, stop int64) ([]string, error) {
- return s.ZrevrangeCtx(context.Background(), key, start, stop)
- }
- // ZrevrangeCtx is the implementation of redis zrevrange command.
- func (s *Redis) ZrevrangeCtx(ctx context.Context, 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(ctx, key, start, stop).Result()
- return err
- }, acceptable)
- return
- }
- // ZrevrangebyscoreWithScores is the implementation of redis zrevrangebyscore command with scores.
- func (s *Redis) ZrevrangebyscoreWithScores(key string, start, stop int64) ([]Pair, error) {
- return s.ZrevrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
- }
- // ZrevrangebyscoreWithScoresCtx is the implementation of redis zrevrangebyscore command with scores.
- func (s *Redis) ZrevrangebyscoreWithScoresCtx(ctx context.Context, 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(ctx, 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
- }
- // ZrevrangebyscoreWithScoresAndLimit is the implementation of redis zrevrangebyscore command
- // with scores and limit.
- func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64,
- page, size int) ([]Pair, error) {
- return s.ZrevrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
- }
- // ZrevrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrevrangebyscore command
- // with scores and limit.
- func (s *Redis) ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, 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(ctx, 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
- }
- // Zrevrank is the implementation of redis zrevrank command.
- func (s *Redis) Zrevrank(key, field string) (int64, error) {
- return s.ZrevrankCtx(context.Background(), key, field)
- }
- // ZrevrankCtx is the implementation of redis zrevrank command.
- func (s *Redis) ZrevrankCtx(ctx context.Context, 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.ZRevRank(ctx, key, field).Result()
- return err
- }, acceptable)
- return
- }
- // Zunionstore is the implementation of redis zunionstore command.
- func (s *Redis) Zunionstore(dest string, store *ZStore) (int64, error) {
- return s.ZunionstoreCtx(context.Background(), dest, store)
- }
- // ZunionstoreCtx is the implementation of redis zunionstore command.
- func (s *Redis) ZunionstoreCtx(ctx context.Context, dest string, store *ZStore) (
- val int64, err error) {
- err = s.brk.DoWithAcceptable(func() error {
- conn, err := getRedis(s)
- if err != nil {
- return err
- }
- val, err = conn.ZUnionStore(ctx, dest, store).Result()
- return err
- }, acceptable)
- return
- }
- // Cluster customizes the given Redis as a cluster.
- func Cluster() Option {
- return func(r *Redis) {
- r.Type = ClusterType
- }
- }
- // SetSlowThreshold sets the slow threshold.
- func SetSlowThreshold(threshold time.Duration) {
- slowThreshold.Set(threshold)
- }
- // WithPass customizes the given Redis with given password.
- func WithPass(pass string) Option {
- return func(r *Redis) {
- r.Pass = pass
- }
- }
- // WithTLS customizes the given Redis with TLS enabled.
- func WithTLS() Option {
- return func(r *Redis) {
- r.tls = true
- }
- }
- func acceptable(err error) bool {
- return err == nil || err == red.Nil || err == context.Canceled
- }
- func getRedis(r *Redis) (RedisNode, error) {
- switch r.Type {
- case ClusterType:
- return getCluster(r)
- case NodeType:
- return getClient(r)
- 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
- }
|