redis.go 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  1. package redis
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "strconv"
  8. "time"
  9. red "github.com/go-redis/redis/v8"
  10. "github.com/zeromicro/go-zero/core/breaker"
  11. "github.com/zeromicro/go-zero/core/mapping"
  12. "github.com/zeromicro/go-zero/core/syncx"
  13. )
  14. const (
  15. // ClusterType means redis cluster.
  16. ClusterType = "cluster"
  17. // NodeType means redis node.
  18. NodeType = "node"
  19. // Nil is an alias of redis.Nil.
  20. Nil = red.Nil
  21. blockingQueryTimeout = 5 * time.Second
  22. readWriteTimeout = 2 * time.Second
  23. defaultSlowThreshold = time.Millisecond * 100
  24. )
  25. var (
  26. // ErrNilNode is an error that indicates a nil redis node.
  27. ErrNilNode = errors.New("nil redis node")
  28. slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  29. )
  30. type (
  31. // Option defines the method to customize a Redis.
  32. Option func(r *Redis)
  33. // A Pair is a key/pair set used in redis zset.
  34. Pair struct {
  35. Key string
  36. Score int64
  37. }
  38. // A FloatPair is a key/pair for float set used in redis zet.
  39. FloatPair struct {
  40. Key string
  41. Score float64
  42. }
  43. // Redis defines a redis node/cluster. It is thread-safe.
  44. Redis struct {
  45. Addr string
  46. Type string
  47. Pass string
  48. tls bool
  49. brk breaker.Breaker
  50. }
  51. // RedisNode interface represents a redis node.
  52. RedisNode interface {
  53. red.Cmdable
  54. }
  55. // GeoLocation is used with GeoAdd to add geospatial location.
  56. GeoLocation = red.GeoLocation
  57. // GeoRadiusQuery is used with GeoRadius to query geospatial index.
  58. GeoRadiusQuery = red.GeoRadiusQuery
  59. // GeoPos is used to represent a geo position.
  60. GeoPos = red.GeoPos
  61. // Pipeliner is an alias of redis.Pipeliner.
  62. Pipeliner = red.Pipeliner
  63. // Z represents sorted set member.
  64. Z = red.Z
  65. // ZStore is an alias of redis.ZStore.
  66. ZStore = red.ZStore
  67. // IntCmd is an alias of redis.IntCmd.
  68. IntCmd = red.IntCmd
  69. // FloatCmd is an alias of redis.FloatCmd.
  70. FloatCmd = red.FloatCmd
  71. // StringCmd is an alias of redis.StringCmd.
  72. StringCmd = red.StringCmd
  73. )
  74. // New returns a Redis with given options.
  75. // Deprecated: use MustNewRedis or NewRedis instead.
  76. func New(addr string, opts ...Option) *Redis {
  77. return newRedis(addr, opts...)
  78. }
  79. // MustNewRedis returns a Redis with given options.
  80. func MustNewRedis(conf RedisConf, opts ...Option) *Redis {
  81. rds, err := NewRedis(conf, opts...)
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. return rds
  86. }
  87. // NewRedis returns a Redis with given options.
  88. func NewRedis(conf RedisConf, opts ...Option) (*Redis, error) {
  89. if err := conf.Validate(); err != nil {
  90. return nil, err
  91. }
  92. if conf.Type == ClusterType {
  93. opts = append([]Option{Cluster()}, opts...)
  94. }
  95. if len(conf.Pass) > 0 {
  96. opts = append([]Option{WithPass(conf.Pass)}, opts...)
  97. }
  98. if conf.Tls {
  99. opts = append([]Option{WithTLS()}, opts...)
  100. }
  101. rds := newRedis(conf.Host, opts...)
  102. if !rds.Ping() {
  103. return nil, ErrPing
  104. }
  105. return rds, nil
  106. }
  107. func newRedis(addr string, opts ...Option) *Redis {
  108. r := &Redis{
  109. Addr: addr,
  110. Type: NodeType,
  111. brk: breaker.NewBreaker(),
  112. }
  113. for _, opt := range opts {
  114. opt(r)
  115. }
  116. return r
  117. }
  118. // BitCount is redis bitcount command implementation.
  119. func (s *Redis) BitCount(key string, start, end int64) (int64, error) {
  120. return s.BitCountCtx(context.Background(), key, start, end)
  121. }
  122. // BitCountCtx is redis bitcount command implementation.
  123. func (s *Redis) BitCountCtx(ctx context.Context, key string, start, end int64) (val int64, err error) {
  124. err = s.brk.DoWithAcceptable(func() error {
  125. conn, err := getRedis(s)
  126. if err != nil {
  127. return err
  128. }
  129. val, err = conn.BitCount(ctx, key, &red.BitCount{
  130. Start: start,
  131. End: end,
  132. }).Result()
  133. return err
  134. }, acceptable)
  135. return
  136. }
  137. // BitOpAnd is redis bit operation (and) command implementation.
  138. func (s *Redis) BitOpAnd(destKey string, keys ...string) (int64, error) {
  139. return s.BitOpAndCtx(context.Background(), destKey, keys...)
  140. }
  141. // BitOpAndCtx is redis bit operation (and) command implementation.
  142. func (s *Redis) BitOpAndCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
  143. err = s.brk.DoWithAcceptable(func() error {
  144. conn, err := getRedis(s)
  145. if err != nil {
  146. return err
  147. }
  148. val, err = conn.BitOpAnd(ctx, destKey, keys...).Result()
  149. return err
  150. }, acceptable)
  151. return
  152. }
  153. // BitOpNot is redis bit operation (not) command implementation.
  154. func (s *Redis) BitOpNot(destKey, key string) (int64, error) {
  155. return s.BitOpNotCtx(context.Background(), destKey, key)
  156. }
  157. // BitOpNotCtx is redis bit operation (not) command implementation.
  158. func (s *Redis) BitOpNotCtx(ctx context.Context, destKey, key string) (val int64, err error) {
  159. err = s.brk.DoWithAcceptable(func() error {
  160. conn, err := getRedis(s)
  161. if err != nil {
  162. return err
  163. }
  164. val, err = conn.BitOpNot(ctx, destKey, key).Result()
  165. return err
  166. }, acceptable)
  167. return
  168. }
  169. // BitOpOr is redis bit operation (or) command implementation.
  170. func (s *Redis) BitOpOr(destKey string, keys ...string) (int64, error) {
  171. return s.BitOpOrCtx(context.Background(), destKey, keys...)
  172. }
  173. // BitOpOrCtx is redis bit operation (or) command implementation.
  174. func (s *Redis) BitOpOrCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
  175. err = s.brk.DoWithAcceptable(func() error {
  176. conn, err := getRedis(s)
  177. if err != nil {
  178. return err
  179. }
  180. val, err = conn.BitOpOr(ctx, destKey, keys...).Result()
  181. return err
  182. }, acceptable)
  183. return
  184. }
  185. // BitOpXor is redis bit operation (xor) command implementation.
  186. func (s *Redis) BitOpXor(destKey string, keys ...string) (int64, error) {
  187. return s.BitOpXorCtx(context.Background(), destKey, keys...)
  188. }
  189. // BitOpXorCtx is redis bit operation (xor) command implementation.
  190. func (s *Redis) BitOpXorCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
  191. err = s.brk.DoWithAcceptable(func() error {
  192. conn, err := getRedis(s)
  193. if err != nil {
  194. return err
  195. }
  196. val, err = conn.BitOpXor(ctx, destKey, keys...).Result()
  197. return err
  198. }, acceptable)
  199. return
  200. }
  201. // BitPos is redis bitpos command implementation.
  202. func (s *Redis) BitPos(key string, bit, start, end int64) (int64, error) {
  203. return s.BitPosCtx(context.Background(), key, bit, start, end)
  204. }
  205. // BitPosCtx is redis bitpos command implementation.
  206. func (s *Redis) BitPosCtx(ctx context.Context, key string, bit, start, end int64) (val int64, err error) {
  207. err = s.brk.DoWithAcceptable(func() error {
  208. conn, err := getRedis(s)
  209. if err != nil {
  210. return err
  211. }
  212. val, err = conn.BitPos(ctx, key, bit, start, end).Result()
  213. return err
  214. }, acceptable)
  215. return
  216. }
  217. // Blpop uses passed in redis connection to execute blocking queries.
  218. // Doesn't benefit from pooling redis connections of blocking queries
  219. func (s *Redis) Blpop(node RedisNode, key string) (string, error) {
  220. return s.BlpopCtx(context.Background(), node, key)
  221. }
  222. // BlpopCtx uses passed in redis connection to execute blocking queries.
  223. // Doesn't benefit from pooling redis connections of blocking queries
  224. func (s *Redis) BlpopCtx(ctx context.Context, node RedisNode, key string) (string, error) {
  225. return s.BlpopWithTimeoutCtx(ctx, node, blockingQueryTimeout, key)
  226. }
  227. // BlpopEx uses passed in redis connection to execute blpop command.
  228. // The difference against Blpop is that this method returns a bool to indicate success.
  229. func (s *Redis) BlpopEx(node RedisNode, key string) (string, bool, error) {
  230. return s.BlpopExCtx(context.Background(), node, key)
  231. }
  232. // BlpopExCtx uses passed in redis connection to execute blpop command.
  233. // The difference against Blpop is that this method returns a bool to indicate success.
  234. func (s *Redis) BlpopExCtx(ctx context.Context, node RedisNode, key string) (string, bool, error) {
  235. if node == nil {
  236. return "", false, ErrNilNode
  237. }
  238. vals, err := node.BLPop(ctx, blockingQueryTimeout, key).Result()
  239. if err != nil {
  240. return "", false, err
  241. }
  242. if len(vals) < 2 {
  243. return "", false, fmt.Errorf("no value on key: %s", key)
  244. }
  245. return vals[1], true, nil
  246. }
  247. // BlpopWithTimeout uses passed in redis connection to execute blpop command.
  248. // Control blocking query timeout
  249. func (s *Redis) BlpopWithTimeout(node RedisNode, timeout time.Duration, key string) (string, error) {
  250. return s.BlpopWithTimeoutCtx(context.Background(), node, timeout, key)
  251. }
  252. // BlpopWithTimeoutCtx uses passed in redis connection to execute blpop command.
  253. // Control blocking query timeout
  254. func (s *Redis) BlpopWithTimeoutCtx(ctx context.Context, node RedisNode, timeout time.Duration,
  255. key string) (string, error) {
  256. if node == nil {
  257. return "", ErrNilNode
  258. }
  259. vals, err := node.BLPop(ctx, timeout, key).Result()
  260. if err != nil {
  261. return "", err
  262. }
  263. if len(vals) < 2 {
  264. return "", fmt.Errorf("no value on key: %s", key)
  265. }
  266. return vals[1], nil
  267. }
  268. // Decr is the implementation of redis decr command.
  269. func (s *Redis) Decr(key string) (int64, error) {
  270. return s.DecrCtx(context.Background(), key)
  271. }
  272. // DecrCtx is the implementation of redis decr command.
  273. func (s *Redis) DecrCtx(ctx context.Context, key string) (val int64, err error) {
  274. err = s.brk.DoWithAcceptable(func() error {
  275. conn, err := getRedis(s)
  276. if err != nil {
  277. return err
  278. }
  279. val, err = conn.Decr(ctx, key).Result()
  280. return err
  281. }, acceptable)
  282. return
  283. }
  284. // Decrby is the implementation of redis decrby command.
  285. func (s *Redis) Decrby(key string, decrement int64) (int64, error) {
  286. return s.DecrbyCtx(context.Background(), key, decrement)
  287. }
  288. // DecrbyCtx is the implementation of redis decrby command.
  289. func (s *Redis) DecrbyCtx(ctx context.Context, key string, decrement int64) (val int64, err error) {
  290. err = s.brk.DoWithAcceptable(func() error {
  291. conn, err := getRedis(s)
  292. if err != nil {
  293. return err
  294. }
  295. val, err = conn.DecrBy(ctx, key, decrement).Result()
  296. return err
  297. }, acceptable)
  298. return
  299. }
  300. // Del deletes keys.
  301. func (s *Redis) Del(keys ...string) (int, error) {
  302. return s.DelCtx(context.Background(), keys...)
  303. }
  304. // DelCtx deletes keys.
  305. func (s *Redis) DelCtx(ctx context.Context, keys ...string) (val int, err error) {
  306. err = s.brk.DoWithAcceptable(func() error {
  307. conn, err := getRedis(s)
  308. if err != nil {
  309. return err
  310. }
  311. v, err := conn.Del(ctx, keys...).Result()
  312. if err != nil {
  313. return err
  314. }
  315. val = int(v)
  316. return nil
  317. }, acceptable)
  318. return
  319. }
  320. // Eval is the implementation of redis eval command.
  321. func (s *Redis) Eval(script string, keys []string, args ...any) (any, error) {
  322. return s.EvalCtx(context.Background(), script, keys, args...)
  323. }
  324. // EvalCtx is the implementation of redis eval command.
  325. func (s *Redis) EvalCtx(ctx context.Context, script string, keys []string,
  326. args ...any) (val any, err error) {
  327. err = s.brk.DoWithAcceptable(func() error {
  328. conn, err := getRedis(s)
  329. if err != nil {
  330. return err
  331. }
  332. val, err = conn.Eval(ctx, script, keys, args...).Result()
  333. return err
  334. }, acceptable)
  335. return
  336. }
  337. // EvalSha is the implementation of redis evalsha command.
  338. func (s *Redis) EvalSha(sha string, keys []string, args ...any) (any, error) {
  339. return s.EvalShaCtx(context.Background(), sha, keys, args...)
  340. }
  341. // EvalShaCtx is the implementation of redis evalsha command.
  342. func (s *Redis) EvalShaCtx(ctx context.Context, sha string, keys []string,
  343. args ...any) (val any, err error) {
  344. err = s.brk.DoWithAcceptable(func() error {
  345. conn, err := getRedis(s)
  346. if err != nil {
  347. return err
  348. }
  349. val, err = conn.EvalSha(ctx, sha, keys, args...).Result()
  350. return err
  351. }, acceptable)
  352. return
  353. }
  354. // Exists is the implementation of redis exists command.
  355. func (s *Redis) Exists(key string) (bool, error) {
  356. return s.ExistsCtx(context.Background(), key)
  357. }
  358. // ExistsCtx is the implementation of redis exists command.
  359. func (s *Redis) ExistsCtx(ctx context.Context, key string) (val bool, err error) {
  360. err = s.brk.DoWithAcceptable(func() error {
  361. conn, err := getRedis(s)
  362. if err != nil {
  363. return err
  364. }
  365. v, err := conn.Exists(ctx, key).Result()
  366. if err != nil {
  367. return err
  368. }
  369. val = v == 1
  370. return nil
  371. }, acceptable)
  372. return
  373. }
  374. // Expire is the implementation of redis expire command.
  375. func (s *Redis) Expire(key string, seconds int) error {
  376. return s.ExpireCtx(context.Background(), key, seconds)
  377. }
  378. // ExpireCtx is the implementation of redis expire command.
  379. func (s *Redis) ExpireCtx(ctx context.Context, key string, seconds int) error {
  380. return s.brk.DoWithAcceptable(func() error {
  381. conn, err := getRedis(s)
  382. if err != nil {
  383. return err
  384. }
  385. return conn.Expire(ctx, key, time.Duration(seconds)*time.Second).Err()
  386. }, acceptable)
  387. }
  388. // Expireat is the implementation of redis expireat command.
  389. func (s *Redis) Expireat(key string, expireTime int64) error {
  390. return s.ExpireatCtx(context.Background(), key, expireTime)
  391. }
  392. // ExpireatCtx is the implementation of redis expireat command.
  393. func (s *Redis) ExpireatCtx(ctx context.Context, key string, expireTime int64) error {
  394. return s.brk.DoWithAcceptable(func() error {
  395. conn, err := getRedis(s)
  396. if err != nil {
  397. return err
  398. }
  399. return conn.ExpireAt(ctx, key, time.Unix(expireTime, 0)).Err()
  400. }, acceptable)
  401. }
  402. // GeoAdd is the implementation of redis geoadd command.
  403. func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (int64, error) {
  404. return s.GeoAddCtx(context.Background(), key, geoLocation...)
  405. }
  406. // GeoAddCtx is the implementation of redis geoadd command.
  407. func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLocation) (
  408. val int64, err error) {
  409. err = s.brk.DoWithAcceptable(func() error {
  410. conn, err := getRedis(s)
  411. if err != nil {
  412. return err
  413. }
  414. v, err := conn.GeoAdd(ctx, key, geoLocation...).Result()
  415. if err != nil {
  416. return err
  417. }
  418. val = v
  419. return nil
  420. }, acceptable)
  421. return
  422. }
  423. // GeoDist is the implementation of redis geodist command.
  424. func (s *Redis) GeoDist(key, member1, member2, unit string) (float64, error) {
  425. return s.GeoDistCtx(context.Background(), key, member1, member2, unit)
  426. }
  427. // GeoDistCtx is the implementation of redis geodist command.
  428. func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit string) (
  429. val float64, err error) {
  430. err = s.brk.DoWithAcceptable(func() error {
  431. conn, err := getRedis(s)
  432. if err != nil {
  433. return err
  434. }
  435. v, err := conn.GeoDist(ctx, key, member1, member2, unit).Result()
  436. if err != nil {
  437. return err
  438. }
  439. val = v
  440. return nil
  441. }, acceptable)
  442. return
  443. }
  444. // GeoHash is the implementation of redis geohash command.
  445. func (s *Redis) GeoHash(key string, members ...string) ([]string, error) {
  446. return s.GeoHashCtx(context.Background(), key, members...)
  447. }
  448. // GeoHashCtx is the implementation of redis geohash command.
  449. func (s *Redis) GeoHashCtx(ctx context.Context, key string, members ...string) (
  450. val []string, err error) {
  451. err = s.brk.DoWithAcceptable(func() error {
  452. conn, err := getRedis(s)
  453. if err != nil {
  454. return err
  455. }
  456. v, err := conn.GeoHash(ctx, key, members...).Result()
  457. if err != nil {
  458. return err
  459. }
  460. val = v
  461. return nil
  462. }, acceptable)
  463. return
  464. }
  465. // GeoRadius is the implementation of redis georadius command.
  466. func (s *Redis) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) (
  467. []GeoLocation, error) {
  468. return s.GeoRadiusCtx(context.Background(), key, longitude, latitude, query)
  469. }
  470. // GeoRadiusCtx is the implementation of redis georadius command.
  471. func (s *Redis) GeoRadiusCtx(ctx context.Context, key string, longitude, latitude float64,
  472. query *GeoRadiusQuery) (val []GeoLocation, err error) {
  473. err = s.brk.DoWithAcceptable(func() error {
  474. conn, err := getRedis(s)
  475. if err != nil {
  476. return err
  477. }
  478. v, err := conn.GeoRadius(ctx, key, longitude, latitude, query).Result()
  479. if err != nil {
  480. return err
  481. }
  482. val = v
  483. return nil
  484. }, acceptable)
  485. return
  486. }
  487. // GeoRadiusByMember is the implementation of redis georadiusbymember command.
  488. func (s *Redis) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) ([]GeoLocation, error) {
  489. return s.GeoRadiusByMemberCtx(context.Background(), key, member, query)
  490. }
  491. // GeoRadiusByMemberCtx is the implementation of redis georadiusbymember command.
  492. func (s *Redis) GeoRadiusByMemberCtx(ctx context.Context, key, member string,
  493. query *GeoRadiusQuery) (val []GeoLocation, err error) {
  494. err = s.brk.DoWithAcceptable(func() error {
  495. conn, err := getRedis(s)
  496. if err != nil {
  497. return err
  498. }
  499. v, err := conn.GeoRadiusByMember(ctx, key, member, query).Result()
  500. if err != nil {
  501. return err
  502. }
  503. val = v
  504. return nil
  505. }, acceptable)
  506. return
  507. }
  508. // GeoPos is the implementation of redis geopos command.
  509. func (s *Redis) GeoPos(key string, members ...string) ([]*GeoPos, error) {
  510. return s.GeoPosCtx(context.Background(), key, members...)
  511. }
  512. // GeoPosCtx is the implementation of redis geopos command.
  513. func (s *Redis) GeoPosCtx(ctx context.Context, key string, members ...string) (
  514. val []*GeoPos, err error) {
  515. err = s.brk.DoWithAcceptable(func() error {
  516. conn, err := getRedis(s)
  517. if err != nil {
  518. return err
  519. }
  520. v, err := conn.GeoPos(ctx, key, members...).Result()
  521. if err != nil {
  522. return err
  523. }
  524. val = v
  525. return nil
  526. }, acceptable)
  527. return
  528. }
  529. // Get is the implementation of redis get command.
  530. func (s *Redis) Get(key string) (string, error) {
  531. return s.GetCtx(context.Background(), key)
  532. }
  533. // GetCtx is the implementation of redis get command.
  534. func (s *Redis) GetCtx(ctx context.Context, key string) (val string, err error) {
  535. err = s.brk.DoWithAcceptable(func() error {
  536. conn, err := getRedis(s)
  537. if err != nil {
  538. return err
  539. }
  540. if val, err = conn.Get(ctx, key).Result(); err == red.Nil {
  541. return nil
  542. } else if err != nil {
  543. return err
  544. } else {
  545. return nil
  546. }
  547. }, acceptable)
  548. return
  549. }
  550. // GetBit is the implementation of redis getbit command.
  551. func (s *Redis) GetBit(key string, offset int64) (int, error) {
  552. return s.GetBitCtx(context.Background(), key, offset)
  553. }
  554. // GetBitCtx is the implementation of redis getbit command.
  555. func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (val int, err error) {
  556. err = s.brk.DoWithAcceptable(func() error {
  557. conn, err := getRedis(s)
  558. if err != nil {
  559. return err
  560. }
  561. v, err := conn.GetBit(ctx, key, offset).Result()
  562. if err != nil {
  563. return err
  564. }
  565. val = int(v)
  566. return nil
  567. }, acceptable)
  568. return
  569. }
  570. // GetSet is the implementation of redis getset command.
  571. func (s *Redis) GetSet(key, value string) (string, error) {
  572. return s.GetSetCtx(context.Background(), key, value)
  573. }
  574. // GetSetCtx is the implementation of redis getset command.
  575. func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
  576. err = s.brk.DoWithAcceptable(func() error {
  577. conn, err := getRedis(s)
  578. if err != nil {
  579. return err
  580. }
  581. if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
  582. return nil
  583. }
  584. return err
  585. }, acceptable)
  586. return
  587. }
  588. // Hdel is the implementation of redis hdel command.
  589. func (s *Redis) Hdel(key string, fields ...string) (bool, error) {
  590. return s.HdelCtx(context.Background(), key, fields...)
  591. }
  592. // HdelCtx is the implementation of redis hdel command.
  593. func (s *Redis) HdelCtx(ctx context.Context, key string, fields ...string) (val bool, err error) {
  594. err = s.brk.DoWithAcceptable(func() error {
  595. conn, err := getRedis(s)
  596. if err != nil {
  597. return err
  598. }
  599. v, err := conn.HDel(ctx, key, fields...).Result()
  600. if err != nil {
  601. return err
  602. }
  603. val = v >= 1
  604. return nil
  605. }, acceptable)
  606. return
  607. }
  608. // Hexists is the implementation of redis hexists command.
  609. func (s *Redis) Hexists(key, field string) (bool, error) {
  610. return s.HexistsCtx(context.Background(), key, field)
  611. }
  612. // HexistsCtx is the implementation of redis hexists command.
  613. func (s *Redis) HexistsCtx(ctx context.Context, key, field string) (val bool, err error) {
  614. err = s.brk.DoWithAcceptable(func() error {
  615. conn, err := getRedis(s)
  616. if err != nil {
  617. return err
  618. }
  619. val, err = conn.HExists(ctx, key, field).Result()
  620. return err
  621. }, acceptable)
  622. return
  623. }
  624. // Hget is the implementation of redis hget command.
  625. func (s *Redis) Hget(key, field string) (string, error) {
  626. return s.HgetCtx(context.Background(), key, field)
  627. }
  628. // HgetCtx is the implementation of redis hget command.
  629. func (s *Redis) HgetCtx(ctx context.Context, key, field string) (val string, err error) {
  630. err = s.brk.DoWithAcceptable(func() error {
  631. conn, err := getRedis(s)
  632. if err != nil {
  633. return err
  634. }
  635. val, err = conn.HGet(ctx, key, field).Result()
  636. return err
  637. }, acceptable)
  638. return
  639. }
  640. // Hgetall is the implementation of redis hgetall command.
  641. func (s *Redis) Hgetall(key string) (map[string]string, error) {
  642. return s.HgetallCtx(context.Background(), key)
  643. }
  644. // HgetallCtx is the implementation of redis hgetall command.
  645. func (s *Redis) HgetallCtx(ctx context.Context, key string) (val map[string]string, err error) {
  646. err = s.brk.DoWithAcceptable(func() error {
  647. conn, err := getRedis(s)
  648. if err != nil {
  649. return err
  650. }
  651. val, err = conn.HGetAll(ctx, key).Result()
  652. return err
  653. }, acceptable)
  654. return
  655. }
  656. // Hincrby is the implementation of redis hincrby command.
  657. func (s *Redis) Hincrby(key, field string, increment int) (int, error) {
  658. return s.HincrbyCtx(context.Background(), key, field, increment)
  659. }
  660. // HincrbyCtx is the implementation of redis hincrby command.
  661. func (s *Redis) HincrbyCtx(ctx context.Context, key, field string, increment int) (val int, err error) {
  662. err = s.brk.DoWithAcceptable(func() error {
  663. conn, err := getRedis(s)
  664. if err != nil {
  665. return err
  666. }
  667. v, err := conn.HIncrBy(ctx, key, field, int64(increment)).Result()
  668. if err != nil {
  669. return err
  670. }
  671. val = int(v)
  672. return nil
  673. }, acceptable)
  674. return
  675. }
  676. // HincrbyFloat is the implementation of redis hincrbyfloat command.
  677. func (s *Redis) HincrbyFloat(key, field string, increment float64) (float64, error) {
  678. return s.HincrbyFloatCtx(context.Background(), key, field, increment)
  679. }
  680. // HincrbyFloatCtx is the implementation of redis hincrbyfloat command.
  681. func (s *Redis) HincrbyFloatCtx(ctx context.Context, key, field string, increment float64) (val float64, err error) {
  682. err = s.brk.DoWithAcceptable(func() error {
  683. conn, err := getRedis(s)
  684. if err != nil {
  685. return err
  686. }
  687. val, err = conn.HIncrByFloat(ctx, key, field, increment).Result()
  688. if err != nil {
  689. return err
  690. }
  691. return nil
  692. }, acceptable)
  693. return
  694. }
  695. // Hkeys is the implementation of redis hkeys command.
  696. func (s *Redis) Hkeys(key string) ([]string, error) {
  697. return s.HkeysCtx(context.Background(), key)
  698. }
  699. // HkeysCtx is the implementation of redis hkeys command.
  700. func (s *Redis) HkeysCtx(ctx context.Context, key string) (val []string, err error) {
  701. err = s.brk.DoWithAcceptable(func() error {
  702. conn, err := getRedis(s)
  703. if err != nil {
  704. return err
  705. }
  706. val, err = conn.HKeys(ctx, key).Result()
  707. return err
  708. }, acceptable)
  709. return
  710. }
  711. // Hlen is the implementation of redis hlen command.
  712. func (s *Redis) Hlen(key string) (int, error) {
  713. return s.HlenCtx(context.Background(), key)
  714. }
  715. // HlenCtx is the implementation of redis hlen command.
  716. func (s *Redis) HlenCtx(ctx context.Context, key string) (val int, err error) {
  717. err = s.brk.DoWithAcceptable(func() error {
  718. conn, err := getRedis(s)
  719. if err != nil {
  720. return err
  721. }
  722. v, err := conn.HLen(ctx, key).Result()
  723. if err != nil {
  724. return err
  725. }
  726. val = int(v)
  727. return nil
  728. }, acceptable)
  729. return
  730. }
  731. // Hmget is the implementation of redis hmget command.
  732. func (s *Redis) Hmget(key string, fields ...string) ([]string, error) {
  733. return s.HmgetCtx(context.Background(), key, fields...)
  734. }
  735. // HmgetCtx is the implementation of redis hmget command.
  736. func (s *Redis) HmgetCtx(ctx context.Context, key string, fields ...string) (val []string, err error) {
  737. err = s.brk.DoWithAcceptable(func() error {
  738. conn, err := getRedis(s)
  739. if err != nil {
  740. return err
  741. }
  742. v, err := conn.HMGet(ctx, key, fields...).Result()
  743. if err != nil {
  744. return err
  745. }
  746. val = toStrings(v)
  747. return nil
  748. }, acceptable)
  749. return
  750. }
  751. // Hset is the implementation of redis hset command.
  752. func (s *Redis) Hset(key, field, value string) error {
  753. return s.HsetCtx(context.Background(), key, field, value)
  754. }
  755. // HsetCtx is the implementation of redis hset command.
  756. func (s *Redis) HsetCtx(ctx context.Context, key, field, value string) error {
  757. return s.brk.DoWithAcceptable(func() error {
  758. conn, err := getRedis(s)
  759. if err != nil {
  760. return err
  761. }
  762. return conn.HSet(ctx, key, field, value).Err()
  763. }, acceptable)
  764. }
  765. // Hsetnx is the implementation of redis hsetnx command.
  766. func (s *Redis) Hsetnx(key, field, value string) (bool, error) {
  767. return s.HsetnxCtx(context.Background(), key, field, value)
  768. }
  769. // HsetnxCtx is the implementation of redis hsetnx command.
  770. func (s *Redis) HsetnxCtx(ctx context.Context, key, field, value string) (val bool, err error) {
  771. err = s.brk.DoWithAcceptable(func() error {
  772. conn, err := getRedis(s)
  773. if err != nil {
  774. return err
  775. }
  776. val, err = conn.HSetNX(ctx, key, field, value).Result()
  777. return err
  778. }, acceptable)
  779. return
  780. }
  781. // Hmset is the implementation of redis hmset command.
  782. func (s *Redis) Hmset(key string, fieldsAndValues map[string]string) error {
  783. return s.HmsetCtx(context.Background(), key, fieldsAndValues)
  784. }
  785. // HmsetCtx is the implementation of redis hmset command.
  786. func (s *Redis) HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error {
  787. return s.brk.DoWithAcceptable(func() error {
  788. conn, err := getRedis(s)
  789. if err != nil {
  790. return err
  791. }
  792. vals := make(map[string]any, len(fieldsAndValues))
  793. for k, v := range fieldsAndValues {
  794. vals[k] = v
  795. }
  796. return conn.HMSet(ctx, key, vals).Err()
  797. }, acceptable)
  798. }
  799. // Hscan is the implementation of redis hscan command.
  800. func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) (
  801. keys []string, cur uint64, err error) {
  802. return s.HscanCtx(context.Background(), key, cursor, match, count)
  803. }
  804. // HscanCtx is the implementation of redis hscan command.
  805. func (s *Redis) HscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  806. keys []string, cur uint64, err error) {
  807. err = s.brk.DoWithAcceptable(func() error {
  808. conn, err := getRedis(s)
  809. if err != nil {
  810. return err
  811. }
  812. keys, cur, err = conn.HScan(ctx, key, cursor, match, count).Result()
  813. return err
  814. }, acceptable)
  815. return
  816. }
  817. // Hvals is the implementation of redis hvals command.
  818. func (s *Redis) Hvals(key string) ([]string, error) {
  819. return s.HvalsCtx(context.Background(), key)
  820. }
  821. // HvalsCtx is the implementation of redis hvals command.
  822. func (s *Redis) HvalsCtx(ctx context.Context, key string) (val []string, err error) {
  823. err = s.brk.DoWithAcceptable(func() error {
  824. conn, err := getRedis(s)
  825. if err != nil {
  826. return err
  827. }
  828. val, err = conn.HVals(ctx, key).Result()
  829. return err
  830. }, acceptable)
  831. return
  832. }
  833. // Incr is the implementation of redis incr command.
  834. func (s *Redis) Incr(key string) (int64, error) {
  835. return s.IncrCtx(context.Background(), key)
  836. }
  837. // IncrCtx is the implementation of redis incr command.
  838. func (s *Redis) IncrCtx(ctx context.Context, key string) (val int64, err error) {
  839. err = s.brk.DoWithAcceptable(func() error {
  840. conn, err := getRedis(s)
  841. if err != nil {
  842. return err
  843. }
  844. val, err = conn.Incr(ctx, key).Result()
  845. return err
  846. }, acceptable)
  847. return
  848. }
  849. // Incrby is the implementation of redis incrby command.
  850. func (s *Redis) Incrby(key string, increment int64) (int64, error) {
  851. return s.IncrbyCtx(context.Background(), key, increment)
  852. }
  853. // IncrbyCtx is the implementation of redis incrby command.
  854. func (s *Redis) IncrbyCtx(ctx context.Context, key string, increment int64) (val int64, err error) {
  855. err = s.brk.DoWithAcceptable(func() error {
  856. conn, err := getRedis(s)
  857. if err != nil {
  858. return err
  859. }
  860. val, err = conn.IncrBy(ctx, key, increment).Result()
  861. return err
  862. }, acceptable)
  863. return
  864. }
  865. // IncrbyFloat is the implementation of redis hincrbyfloat command.
  866. func (s *Redis) IncrbyFloat(key string, increment float64) (float64, error) {
  867. return s.IncrbyFloatCtx(context.Background(), key, increment)
  868. }
  869. // IncrbyFloatCtx is the implementation of redis hincrbyfloat command.
  870. func (s *Redis) IncrbyFloatCtx(ctx context.Context, key string, increment float64) (val float64, err error) {
  871. err = s.brk.DoWithAcceptable(func() error {
  872. conn, err := getRedis(s)
  873. if err != nil {
  874. return err
  875. }
  876. val, err = conn.IncrByFloat(ctx, key, increment).Result()
  877. return err
  878. }, acceptable)
  879. return
  880. }
  881. // Keys is the implementation of redis keys command.
  882. func (s *Redis) Keys(pattern string) ([]string, error) {
  883. return s.KeysCtx(context.Background(), pattern)
  884. }
  885. // KeysCtx is the implementation of redis keys command.
  886. func (s *Redis) KeysCtx(ctx context.Context, pattern string) (val []string, err error) {
  887. err = s.brk.DoWithAcceptable(func() error {
  888. conn, err := getRedis(s)
  889. if err != nil {
  890. return err
  891. }
  892. val, err = conn.Keys(ctx, pattern).Result()
  893. return err
  894. }, acceptable)
  895. return
  896. }
  897. // Llen is the implementation of redis llen command.
  898. func (s *Redis) Llen(key string) (int, error) {
  899. return s.LlenCtx(context.Background(), key)
  900. }
  901. // LlenCtx is the implementation of redis llen command.
  902. func (s *Redis) LlenCtx(ctx context.Context, key string) (val int, err error) {
  903. err = s.brk.DoWithAcceptable(func() error {
  904. conn, err := getRedis(s)
  905. if err != nil {
  906. return err
  907. }
  908. v, err := conn.LLen(ctx, key).Result()
  909. if err != nil {
  910. return err
  911. }
  912. val = int(v)
  913. return nil
  914. }, acceptable)
  915. return
  916. }
  917. // Lindex is the implementation of redis lindex command.
  918. func (s *Redis) Lindex(key string, index int64) (string, error) {
  919. return s.LindexCtx(context.Background(), key, index)
  920. }
  921. // LindexCtx is the implementation of redis lindex command.
  922. func (s *Redis) LindexCtx(ctx context.Context, key string, index int64) (val string, err error) {
  923. err = s.brk.DoWithAcceptable(func() error {
  924. conn, err := getRedis(s)
  925. if err != nil {
  926. return err
  927. }
  928. val, err = conn.LIndex(ctx, key, index).Result()
  929. return err
  930. }, acceptable)
  931. return
  932. }
  933. // Lpop is the implementation of redis lpop command.
  934. func (s *Redis) Lpop(key string) (string, error) {
  935. return s.LpopCtx(context.Background(), key)
  936. }
  937. // LpopCtx is the implementation of redis lpop command.
  938. func (s *Redis) LpopCtx(ctx context.Context, key string) (val string, err error) {
  939. err = s.brk.DoWithAcceptable(func() error {
  940. conn, err := getRedis(s)
  941. if err != nil {
  942. return err
  943. }
  944. val, err = conn.LPop(ctx, key).Result()
  945. return err
  946. }, acceptable)
  947. return
  948. }
  949. // LpopCount is the implementation of redis lpopCount command.
  950. func (s *Redis) LpopCount(key string, count int) ([]string, error) {
  951. return s.LpopCountCtx(context.Background(), key, count)
  952. }
  953. // LpopCountCtx is the implementation of redis lpopCount command.
  954. func (s *Redis) LpopCountCtx(ctx context.Context, key string, count int) (val []string, err error) {
  955. err = s.brk.DoWithAcceptable(func() error {
  956. conn, err := getRedis(s)
  957. if err != nil {
  958. return err
  959. }
  960. val, err = conn.LPopCount(ctx, key, count).Result()
  961. return err
  962. }, acceptable)
  963. return
  964. }
  965. // Lpush is the implementation of redis lpush command.
  966. func (s *Redis) Lpush(key string, values ...any) (int, error) {
  967. return s.LpushCtx(context.Background(), key, values...)
  968. }
  969. // LpushCtx is the implementation of redis lpush command.
  970. func (s *Redis) LpushCtx(ctx context.Context, key string, values ...any) (val int, err error) {
  971. err = s.brk.DoWithAcceptable(func() error {
  972. conn, err := getRedis(s)
  973. if err != nil {
  974. return err
  975. }
  976. v, err := conn.LPush(ctx, key, values...).Result()
  977. if err != nil {
  978. return err
  979. }
  980. val = int(v)
  981. return nil
  982. }, acceptable)
  983. return
  984. }
  985. // Lrange is the implementation of redis lrange command.
  986. func (s *Redis) Lrange(key string, start, stop int) ([]string, error) {
  987. return s.LrangeCtx(context.Background(), key, start, stop)
  988. }
  989. // LrangeCtx is the implementation of redis lrange command.
  990. func (s *Redis) LrangeCtx(ctx context.Context, key string, start, stop int) (val []string, err error) {
  991. err = s.brk.DoWithAcceptable(func() error {
  992. conn, err := getRedis(s)
  993. if err != nil {
  994. return err
  995. }
  996. val, err = conn.LRange(ctx, key, int64(start), int64(stop)).Result()
  997. return err
  998. }, acceptable)
  999. return
  1000. }
  1001. // Lrem is the implementation of redis lrem command.
  1002. func (s *Redis) Lrem(key string, count int, value string) (int, error) {
  1003. return s.LremCtx(context.Background(), key, count, value)
  1004. }
  1005. // LremCtx is the implementation of redis lrem command.
  1006. func (s *Redis) LremCtx(ctx context.Context, key string, count int, value string) (val int, err error) {
  1007. err = s.brk.DoWithAcceptable(func() error {
  1008. conn, err := getRedis(s)
  1009. if err != nil {
  1010. return err
  1011. }
  1012. v, err := conn.LRem(ctx, key, int64(count), value).Result()
  1013. if err != nil {
  1014. return err
  1015. }
  1016. val = int(v)
  1017. return nil
  1018. }, acceptable)
  1019. return
  1020. }
  1021. // Ltrim is the implementation of redis ltrim command.
  1022. func (s *Redis) Ltrim(key string, start, stop int64) error {
  1023. return s.LtrimCtx(context.Background(), key, start, stop)
  1024. }
  1025. // LtrimCtx is the implementation of redis ltrim command.
  1026. func (s *Redis) LtrimCtx(ctx context.Context, key string, start, stop int64) error {
  1027. return s.brk.DoWithAcceptable(func() error {
  1028. conn, err := getRedis(s)
  1029. if err != nil {
  1030. return err
  1031. }
  1032. return conn.LTrim(ctx, key, start, stop).Err()
  1033. }, acceptable)
  1034. }
  1035. // Mget is the implementation of redis mget command.
  1036. func (s *Redis) Mget(keys ...string) ([]string, error) {
  1037. return s.MgetCtx(context.Background(), keys...)
  1038. }
  1039. // MgetCtx is the implementation of redis mget command.
  1040. func (s *Redis) MgetCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1041. err = s.brk.DoWithAcceptable(func() error {
  1042. conn, err := getRedis(s)
  1043. if err != nil {
  1044. return err
  1045. }
  1046. v, err := conn.MGet(ctx, keys...).Result()
  1047. if err != nil {
  1048. return err
  1049. }
  1050. val = toStrings(v)
  1051. return nil
  1052. }, acceptable)
  1053. return
  1054. }
  1055. // Persist is the implementation of redis persist command.
  1056. func (s *Redis) Persist(key string) (bool, error) {
  1057. return s.PersistCtx(context.Background(), key)
  1058. }
  1059. // PersistCtx is the implementation of redis persist command.
  1060. func (s *Redis) PersistCtx(ctx context.Context, key string) (val bool, err error) {
  1061. err = s.brk.DoWithAcceptable(func() error {
  1062. conn, err := getRedis(s)
  1063. if err != nil {
  1064. return err
  1065. }
  1066. val, err = conn.Persist(ctx, key).Result()
  1067. return err
  1068. }, acceptable)
  1069. return
  1070. }
  1071. // Pfadd is the implementation of redis pfadd command.
  1072. func (s *Redis) Pfadd(key string, values ...any) (bool, error) {
  1073. return s.PfaddCtx(context.Background(), key, values...)
  1074. }
  1075. // PfaddCtx is the implementation of redis pfadd command.
  1076. func (s *Redis) PfaddCtx(ctx context.Context, key string, values ...any) (val bool, err error) {
  1077. err = s.brk.DoWithAcceptable(func() error {
  1078. conn, err := getRedis(s)
  1079. if err != nil {
  1080. return err
  1081. }
  1082. v, err := conn.PFAdd(ctx, key, values...).Result()
  1083. if err != nil {
  1084. return err
  1085. }
  1086. val = v >= 1
  1087. return nil
  1088. }, acceptable)
  1089. return
  1090. }
  1091. // Pfcount is the implementation of redis pfcount command.
  1092. func (s *Redis) Pfcount(key string) (int64, error) {
  1093. return s.PfcountCtx(context.Background(), key)
  1094. }
  1095. // PfcountCtx is the implementation of redis pfcount command.
  1096. func (s *Redis) PfcountCtx(ctx context.Context, key string) (val int64, err error) {
  1097. err = s.brk.DoWithAcceptable(func() error {
  1098. conn, err := getRedis(s)
  1099. if err != nil {
  1100. return err
  1101. }
  1102. val, err = conn.PFCount(ctx, key).Result()
  1103. return err
  1104. }, acceptable)
  1105. return
  1106. }
  1107. // Pfmerge is the implementation of redis pfmerge command.
  1108. func (s *Redis) Pfmerge(dest string, keys ...string) error {
  1109. return s.PfmergeCtx(context.Background(), dest, keys...)
  1110. }
  1111. // PfmergeCtx is the implementation of redis pfmerge command.
  1112. func (s *Redis) PfmergeCtx(ctx context.Context, dest string, keys ...string) error {
  1113. return s.brk.DoWithAcceptable(func() error {
  1114. conn, err := getRedis(s)
  1115. if err != nil {
  1116. return err
  1117. }
  1118. _, err = conn.PFMerge(ctx, dest, keys...).Result()
  1119. return err
  1120. }, acceptable)
  1121. }
  1122. // Ping is the implementation of redis ping command.
  1123. func (s *Redis) Ping() bool {
  1124. return s.PingCtx(context.Background())
  1125. }
  1126. // PingCtx is the implementation of redis ping command.
  1127. func (s *Redis) PingCtx(ctx context.Context) (val bool) {
  1128. // ignore error, error means false
  1129. _ = s.brk.DoWithAcceptable(func() error {
  1130. conn, err := getRedis(s)
  1131. if err != nil {
  1132. val = false
  1133. return nil
  1134. }
  1135. v, err := conn.Ping(ctx).Result()
  1136. if err != nil {
  1137. val = false
  1138. return nil
  1139. }
  1140. val = v == "PONG"
  1141. return nil
  1142. }, acceptable)
  1143. return
  1144. }
  1145. // Pipelined lets fn execute pipelined commands.
  1146. func (s *Redis) Pipelined(fn func(Pipeliner) error) error {
  1147. return s.PipelinedCtx(context.Background(), fn)
  1148. }
  1149. // PipelinedCtx lets fn execute pipelined commands.
  1150. // Results need to be retrieved by calling Pipeline.Exec()
  1151. func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) error {
  1152. return s.brk.DoWithAcceptable(func() error {
  1153. conn, err := getRedis(s)
  1154. if err != nil {
  1155. return err
  1156. }
  1157. _, err = conn.Pipelined(ctx, fn)
  1158. return err
  1159. }, acceptable)
  1160. }
  1161. // Rpop is the implementation of redis rpop command.
  1162. func (s *Redis) Rpop(key string) (string, error) {
  1163. return s.RpopCtx(context.Background(), key)
  1164. }
  1165. // RpopCtx is the implementation of redis rpop command.
  1166. func (s *Redis) RpopCtx(ctx context.Context, key string) (val string, err error) {
  1167. err = s.brk.DoWithAcceptable(func() error {
  1168. conn, err := getRedis(s)
  1169. if err != nil {
  1170. return err
  1171. }
  1172. val, err = conn.RPop(ctx, key).Result()
  1173. return err
  1174. }, acceptable)
  1175. return
  1176. }
  1177. // RpopCount is the implementation of redis rpopCount command.
  1178. func (s *Redis) RpopCount(key string, count int) ([]string, error) {
  1179. return s.RpopCountCtx(context.Background(), key, count)
  1180. }
  1181. // RpopCountCtx is the implementation of redis rpopCount command.
  1182. func (s *Redis) RpopCountCtx(ctx context.Context, key string, count int) (val []string, err error) {
  1183. err = s.brk.DoWithAcceptable(func() error {
  1184. conn, err := getRedis(s)
  1185. if err != nil {
  1186. return err
  1187. }
  1188. val, err = conn.RPopCount(ctx, key, count).Result()
  1189. return err
  1190. }, acceptable)
  1191. return
  1192. }
  1193. // Rpush is the implementation of redis rpush command.
  1194. func (s *Redis) Rpush(key string, values ...any) (int, error) {
  1195. return s.RpushCtx(context.Background(), key, values...)
  1196. }
  1197. // RpushCtx is the implementation of redis rpush command.
  1198. func (s *Redis) RpushCtx(ctx context.Context, key string, values ...any) (val int, err error) {
  1199. err = s.brk.DoWithAcceptable(func() error {
  1200. conn, err := getRedis(s)
  1201. if err != nil {
  1202. return err
  1203. }
  1204. v, err := conn.RPush(ctx, key, values...).Result()
  1205. if err != nil {
  1206. return err
  1207. }
  1208. val = int(v)
  1209. return nil
  1210. }, acceptable)
  1211. return
  1212. }
  1213. // Sadd is the implementation of redis sadd command.
  1214. func (s *Redis) Sadd(key string, values ...any) (int, error) {
  1215. return s.SaddCtx(context.Background(), key, values...)
  1216. }
  1217. // SaddCtx is the implementation of redis sadd command.
  1218. func (s *Redis) SaddCtx(ctx context.Context, key string, values ...any) (val int, err error) {
  1219. err = s.brk.DoWithAcceptable(func() error {
  1220. conn, err := getRedis(s)
  1221. if err != nil {
  1222. return err
  1223. }
  1224. v, err := conn.SAdd(ctx, key, values...).Result()
  1225. if err != nil {
  1226. return err
  1227. }
  1228. val = int(v)
  1229. return nil
  1230. }, acceptable)
  1231. return
  1232. }
  1233. // Scan is the implementation of redis scan command.
  1234. func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
  1235. return s.ScanCtx(context.Background(), cursor, match, count)
  1236. }
  1237. // ScanCtx is the implementation of redis scan command.
  1238. func (s *Redis) ScanCtx(ctx context.Context, cursor uint64, match string, count int64) (
  1239. keys []string, cur uint64, err error) {
  1240. err = s.brk.DoWithAcceptable(func() error {
  1241. conn, err := getRedis(s)
  1242. if err != nil {
  1243. return err
  1244. }
  1245. keys, cur, err = conn.Scan(ctx, cursor, match, count).Result()
  1246. return err
  1247. }, acceptable)
  1248. return
  1249. }
  1250. // SetBit is the implementation of redis setbit command.
  1251. func (s *Redis) SetBit(key string, offset int64, value int) (int, error) {
  1252. return s.SetBitCtx(context.Background(), key, offset, value)
  1253. }
  1254. // SetBitCtx is the implementation of redis setbit command.
  1255. func (s *Redis) SetBitCtx(ctx context.Context, key string, offset int64, value int) (val int, err error) {
  1256. err = s.brk.DoWithAcceptable(func() error {
  1257. conn, err := getRedis(s)
  1258. if err != nil {
  1259. return err
  1260. }
  1261. v, err := conn.SetBit(ctx, key, offset, value).Result()
  1262. if err != nil {
  1263. return err
  1264. }
  1265. val = int(v)
  1266. return nil
  1267. }, acceptable)
  1268. return
  1269. }
  1270. // Sscan is the implementation of redis sscan command.
  1271. func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) (
  1272. keys []string, cur uint64, err error) {
  1273. return s.SscanCtx(context.Background(), key, cursor, match, count)
  1274. }
  1275. // SscanCtx is the implementation of redis sscan command.
  1276. func (s *Redis) SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  1277. keys []string, cur uint64, err error) {
  1278. err = s.brk.DoWithAcceptable(func() error {
  1279. conn, err := getRedis(s)
  1280. if err != nil {
  1281. return err
  1282. }
  1283. keys, cur, err = conn.SScan(ctx, key, cursor, match, count).Result()
  1284. return err
  1285. }, acceptable)
  1286. return
  1287. }
  1288. // Scard is the implementation of redis scard command.
  1289. func (s *Redis) Scard(key string) (int64, error) {
  1290. return s.ScardCtx(context.Background(), key)
  1291. }
  1292. // ScardCtx is the implementation of redis scard command.
  1293. func (s *Redis) ScardCtx(ctx context.Context, key string) (val int64, err error) {
  1294. err = s.brk.DoWithAcceptable(func() error {
  1295. conn, err := getRedis(s)
  1296. if err != nil {
  1297. return err
  1298. }
  1299. val, err = conn.SCard(ctx, key).Result()
  1300. return err
  1301. }, acceptable)
  1302. return
  1303. }
  1304. // ScriptLoad is the implementation of redis script load command.
  1305. func (s *Redis) ScriptLoad(script string) (string, error) {
  1306. return s.ScriptLoadCtx(context.Background(), script)
  1307. }
  1308. // ScriptLoadCtx is the implementation of redis script load command.
  1309. func (s *Redis) ScriptLoadCtx(ctx context.Context, script string) (string, error) {
  1310. conn, err := getRedis(s)
  1311. if err != nil {
  1312. return "", err
  1313. }
  1314. return conn.ScriptLoad(ctx, script).Result()
  1315. }
  1316. // Set is the implementation of redis set command.
  1317. func (s *Redis) Set(key, value string) error {
  1318. return s.SetCtx(context.Background(), key, value)
  1319. }
  1320. // SetCtx is the implementation of redis set command.
  1321. func (s *Redis) SetCtx(ctx context.Context, key, value string) error {
  1322. return s.brk.DoWithAcceptable(func() error {
  1323. conn, err := getRedis(s)
  1324. if err != nil {
  1325. return err
  1326. }
  1327. return conn.Set(ctx, key, value, 0).Err()
  1328. }, acceptable)
  1329. }
  1330. // Setex is the implementation of redis setex command.
  1331. func (s *Redis) Setex(key, value string, seconds int) error {
  1332. return s.SetexCtx(context.Background(), key, value, seconds)
  1333. }
  1334. // SetexCtx is the implementation of redis setex command.
  1335. func (s *Redis) SetexCtx(ctx context.Context, key, value string, seconds int) error {
  1336. return s.brk.DoWithAcceptable(func() error {
  1337. conn, err := getRedis(s)
  1338. if err != nil {
  1339. return err
  1340. }
  1341. return conn.Set(ctx, key, value, time.Duration(seconds)*time.Second).Err()
  1342. }, acceptable)
  1343. }
  1344. // Setnx is the implementation of redis setnx command.
  1345. func (s *Redis) Setnx(key, value string) (bool, error) {
  1346. return s.SetnxCtx(context.Background(), key, value)
  1347. }
  1348. // SetnxCtx is the implementation of redis setnx command.
  1349. func (s *Redis) SetnxCtx(ctx context.Context, key, value string) (val bool, err error) {
  1350. err = s.brk.DoWithAcceptable(func() error {
  1351. conn, err := getRedis(s)
  1352. if err != nil {
  1353. return err
  1354. }
  1355. val, err = conn.SetNX(ctx, key, value, 0).Result()
  1356. return err
  1357. }, acceptable)
  1358. return
  1359. }
  1360. // SetnxEx is the implementation of redis setnx command with expire.
  1361. func (s *Redis) SetnxEx(key, value string, seconds int) (bool, error) {
  1362. return s.SetnxExCtx(context.Background(), key, value, seconds)
  1363. }
  1364. // SetnxExCtx is the implementation of redis setnx command with expire.
  1365. func (s *Redis) SetnxExCtx(ctx context.Context, key, value string, seconds int) (val bool, err error) {
  1366. err = s.brk.DoWithAcceptable(func() error {
  1367. conn, err := getRedis(s)
  1368. if err != nil {
  1369. return err
  1370. }
  1371. val, err = conn.SetNX(ctx, key, value, time.Duration(seconds)*time.Second).Result()
  1372. return err
  1373. }, acceptable)
  1374. return
  1375. }
  1376. // Sismember is the implementation of redis sismember command.
  1377. func (s *Redis) Sismember(key string, value any) (bool, error) {
  1378. return s.SismemberCtx(context.Background(), key, value)
  1379. }
  1380. // SismemberCtx is the implementation of redis sismember command.
  1381. func (s *Redis) SismemberCtx(ctx context.Context, key string, value any) (val bool, err error) {
  1382. err = s.brk.DoWithAcceptable(func() error {
  1383. conn, err := getRedis(s)
  1384. if err != nil {
  1385. return err
  1386. }
  1387. val, err = conn.SIsMember(ctx, key, value).Result()
  1388. return err
  1389. }, acceptable)
  1390. return
  1391. }
  1392. // Smembers is the implementation of redis smembers command.
  1393. func (s *Redis) Smembers(key string) ([]string, error) {
  1394. return s.SmembersCtx(context.Background(), key)
  1395. }
  1396. // SmembersCtx is the implementation of redis smembers command.
  1397. func (s *Redis) SmembersCtx(ctx context.Context, key string) (val []string, err error) {
  1398. err = s.brk.DoWithAcceptable(func() error {
  1399. conn, err := getRedis(s)
  1400. if err != nil {
  1401. return err
  1402. }
  1403. val, err = conn.SMembers(ctx, key).Result()
  1404. return err
  1405. }, acceptable)
  1406. return
  1407. }
  1408. // Spop is the implementation of redis spop command.
  1409. func (s *Redis) Spop(key string) (string, error) {
  1410. return s.SpopCtx(context.Background(), key)
  1411. }
  1412. // SpopCtx is the implementation of redis spop command.
  1413. func (s *Redis) SpopCtx(ctx context.Context, key string) (val string, err error) {
  1414. err = s.brk.DoWithAcceptable(func() error {
  1415. conn, err := getRedis(s)
  1416. if err != nil {
  1417. return err
  1418. }
  1419. val, err = conn.SPop(ctx, key).Result()
  1420. return err
  1421. }, acceptable)
  1422. return
  1423. }
  1424. // Srandmember is the implementation of redis srandmember command.
  1425. func (s *Redis) Srandmember(key string, count int) ([]string, error) {
  1426. return s.SrandmemberCtx(context.Background(), key, count)
  1427. }
  1428. // SrandmemberCtx is the implementation of redis srandmember command.
  1429. func (s *Redis) SrandmemberCtx(ctx context.Context, key string, count int) (val []string, err error) {
  1430. err = s.brk.DoWithAcceptable(func() error {
  1431. conn, err := getRedis(s)
  1432. if err != nil {
  1433. return err
  1434. }
  1435. val, err = conn.SRandMemberN(ctx, key, int64(count)).Result()
  1436. return err
  1437. }, acceptable)
  1438. return
  1439. }
  1440. // Srem is the implementation of redis srem command.
  1441. func (s *Redis) Srem(key string, values ...any) (int, error) {
  1442. return s.SremCtx(context.Background(), key, values...)
  1443. }
  1444. // SremCtx is the implementation of redis srem command.
  1445. func (s *Redis) SremCtx(ctx context.Context, key string, values ...any) (val int, err error) {
  1446. err = s.brk.DoWithAcceptable(func() error {
  1447. conn, err := getRedis(s)
  1448. if err != nil {
  1449. return err
  1450. }
  1451. v, err := conn.SRem(ctx, key, values...).Result()
  1452. if err != nil {
  1453. return err
  1454. }
  1455. val = int(v)
  1456. return nil
  1457. }, acceptable)
  1458. return
  1459. }
  1460. // String returns the string representation of s.
  1461. func (s *Redis) String() string {
  1462. return s.Addr
  1463. }
  1464. // Sunion is the implementation of redis sunion command.
  1465. func (s *Redis) Sunion(keys ...string) ([]string, error) {
  1466. return s.SunionCtx(context.Background(), keys...)
  1467. }
  1468. // SunionCtx is the implementation of redis sunion command.
  1469. func (s *Redis) SunionCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1470. err = s.brk.DoWithAcceptable(func() error {
  1471. conn, err := getRedis(s)
  1472. if err != nil {
  1473. return err
  1474. }
  1475. val, err = conn.SUnion(ctx, keys...).Result()
  1476. return err
  1477. }, acceptable)
  1478. return
  1479. }
  1480. // Sunionstore is the implementation of redis sunionstore command.
  1481. func (s *Redis) Sunionstore(destination string, keys ...string) (int, error) {
  1482. return s.SunionstoreCtx(context.Background(), destination, keys...)
  1483. }
  1484. // SunionstoreCtx is the implementation of redis sunionstore command.
  1485. func (s *Redis) SunionstoreCtx(ctx context.Context, destination string, keys ...string) (
  1486. val int, err error) {
  1487. err = s.brk.DoWithAcceptable(func() error {
  1488. conn, err := getRedis(s)
  1489. if err != nil {
  1490. return err
  1491. }
  1492. v, err := conn.SUnionStore(ctx, destination, keys...).Result()
  1493. if err != nil {
  1494. return err
  1495. }
  1496. val = int(v)
  1497. return nil
  1498. }, acceptable)
  1499. return
  1500. }
  1501. // Sdiff is the implementation of redis sdiff command.
  1502. func (s *Redis) Sdiff(keys ...string) ([]string, error) {
  1503. return s.SdiffCtx(context.Background(), keys...)
  1504. }
  1505. // SdiffCtx is the implementation of redis sdiff command.
  1506. func (s *Redis) SdiffCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1507. err = s.brk.DoWithAcceptable(func() error {
  1508. conn, err := getRedis(s)
  1509. if err != nil {
  1510. return err
  1511. }
  1512. val, err = conn.SDiff(ctx, keys...).Result()
  1513. return err
  1514. }, acceptable)
  1515. return
  1516. }
  1517. // Sdiffstore is the implementation of redis sdiffstore command.
  1518. func (s *Redis) Sdiffstore(destination string, keys ...string) (int, error) {
  1519. return s.SdiffstoreCtx(context.Background(), destination, keys...)
  1520. }
  1521. // SdiffstoreCtx is the implementation of redis sdiffstore command.
  1522. func (s *Redis) SdiffstoreCtx(ctx context.Context, destination string, keys ...string) (
  1523. val int, err error) {
  1524. err = s.brk.DoWithAcceptable(func() error {
  1525. conn, err := getRedis(s)
  1526. if err != nil {
  1527. return err
  1528. }
  1529. v, err := conn.SDiffStore(ctx, destination, keys...).Result()
  1530. if err != nil {
  1531. return err
  1532. }
  1533. val = int(v)
  1534. return nil
  1535. }, acceptable)
  1536. return
  1537. }
  1538. // Sinter is the implementation of redis sinter command.
  1539. func (s *Redis) Sinter(keys ...string) ([]string, error) {
  1540. return s.SinterCtx(context.Background(), keys...)
  1541. }
  1542. // SinterCtx is the implementation of redis sinter command.
  1543. func (s *Redis) SinterCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1544. err = s.brk.DoWithAcceptable(func() error {
  1545. conn, err := getRedis(s)
  1546. if err != nil {
  1547. return err
  1548. }
  1549. val, err = conn.SInter(ctx, keys...).Result()
  1550. return err
  1551. }, acceptable)
  1552. return
  1553. }
  1554. // Sinterstore is the implementation of redis sinterstore command.
  1555. func (s *Redis) Sinterstore(destination string, keys ...string) (int, error) {
  1556. return s.SinterstoreCtx(context.Background(), destination, keys...)
  1557. }
  1558. // SinterstoreCtx is the implementation of redis sinterstore command.
  1559. func (s *Redis) SinterstoreCtx(ctx context.Context, destination string, keys ...string) (
  1560. val int, err error) {
  1561. err = s.brk.DoWithAcceptable(func() error {
  1562. conn, err := getRedis(s)
  1563. if err != nil {
  1564. return err
  1565. }
  1566. v, err := conn.SInterStore(ctx, destination, keys...).Result()
  1567. if err != nil {
  1568. return err
  1569. }
  1570. val = int(v)
  1571. return nil
  1572. }, acceptable)
  1573. return
  1574. }
  1575. // Ttl is the implementation of redis ttl command.
  1576. func (s *Redis) Ttl(key string) (int, error) {
  1577. return s.TtlCtx(context.Background(), key)
  1578. }
  1579. // TtlCtx is the implementation of redis ttl command.
  1580. func (s *Redis) TtlCtx(ctx context.Context, key string) (val int, err error) {
  1581. err = s.brk.DoWithAcceptable(func() error {
  1582. conn, err := getRedis(s)
  1583. if err != nil {
  1584. return err
  1585. }
  1586. duration, err := conn.TTL(ctx, key).Result()
  1587. if err != nil {
  1588. return err
  1589. }
  1590. val = int(duration / time.Second)
  1591. return nil
  1592. }, acceptable)
  1593. return
  1594. }
  1595. // Zadd is the implementation of redis zadd command.
  1596. func (s *Redis) Zadd(key string, score int64, value string) (bool, error) {
  1597. return s.ZaddCtx(context.Background(), key, score, value)
  1598. }
  1599. // ZaddCtx is the implementation of redis zadd command.
  1600. func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
  1601. val bool, err error) {
  1602. return s.ZaddFloatCtx(ctx, key, float64(score), value)
  1603. }
  1604. // ZaddFloat is the implementation of redis zadd command.
  1605. func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) {
  1606. return s.ZaddFloatCtx(context.Background(), key, score, value)
  1607. }
  1608. // ZaddFloatCtx is the implementation of redis zadd command.
  1609. func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (
  1610. val bool, err error) {
  1611. err = s.brk.DoWithAcceptable(func() error {
  1612. conn, err := getRedis(s)
  1613. if err != nil {
  1614. return err
  1615. }
  1616. v, err := conn.ZAdd(ctx, key, &red.Z{
  1617. Score: score,
  1618. Member: value,
  1619. }).Result()
  1620. if err != nil {
  1621. return err
  1622. }
  1623. val = v == 1
  1624. return nil
  1625. }, acceptable)
  1626. return
  1627. }
  1628. // Zadds is the implementation of redis zadds command.
  1629. func (s *Redis) Zadds(key string, ps ...Pair) (int64, error) {
  1630. return s.ZaddsCtx(context.Background(), key, ps...)
  1631. }
  1632. // ZaddsCtx is the implementation of redis zadds command.
  1633. func (s *Redis) ZaddsCtx(ctx context.Context, key string, ps ...Pair) (val int64, err error) {
  1634. err = s.brk.DoWithAcceptable(func() error {
  1635. conn, err := getRedis(s)
  1636. if err != nil {
  1637. return err
  1638. }
  1639. var zs []*red.Z
  1640. for _, p := range ps {
  1641. z := &red.Z{Score: float64(p.Score), Member: p.Key}
  1642. zs = append(zs, z)
  1643. }
  1644. v, err := conn.ZAdd(ctx, key, zs...).Result()
  1645. if err != nil {
  1646. return err
  1647. }
  1648. val = v
  1649. return nil
  1650. }, acceptable)
  1651. return
  1652. }
  1653. // Zcard is the implementation of redis zcard command.
  1654. func (s *Redis) Zcard(key string) (int, error) {
  1655. return s.ZcardCtx(context.Background(), key)
  1656. }
  1657. // ZcardCtx is the implementation of redis zcard command.
  1658. func (s *Redis) ZcardCtx(ctx context.Context, key string) (val int, err error) {
  1659. err = s.brk.DoWithAcceptable(func() error {
  1660. conn, err := getRedis(s)
  1661. if err != nil {
  1662. return err
  1663. }
  1664. v, err := conn.ZCard(ctx, key).Result()
  1665. if err != nil {
  1666. return err
  1667. }
  1668. val = int(v)
  1669. return nil
  1670. }, acceptable)
  1671. return
  1672. }
  1673. // Zcount is the implementation of redis zcount command.
  1674. func (s *Redis) Zcount(key string, start, stop int64) (int, error) {
  1675. return s.ZcountCtx(context.Background(), key, start, stop)
  1676. }
  1677. // ZcountCtx is the implementation of redis zcount command.
  1678. func (s *Redis) ZcountCtx(ctx context.Context, key string, start, stop int64) (val int, err error) {
  1679. err = s.brk.DoWithAcceptable(func() error {
  1680. conn, err := getRedis(s)
  1681. if err != nil {
  1682. return err
  1683. }
  1684. v, err := conn.ZCount(ctx, key, strconv.FormatInt(start, 10),
  1685. strconv.FormatInt(stop, 10)).Result()
  1686. if err != nil {
  1687. return err
  1688. }
  1689. val = int(v)
  1690. return nil
  1691. }, acceptable)
  1692. return
  1693. }
  1694. // Zincrby is the implementation of redis zincrby command.
  1695. func (s *Redis) Zincrby(key string, increment int64, field string) (int64, error) {
  1696. return s.ZincrbyCtx(context.Background(), key, increment, field)
  1697. }
  1698. // ZincrbyCtx is the implementation of redis zincrby command.
  1699. func (s *Redis) ZincrbyCtx(ctx context.Context, key string, increment int64, field string) (
  1700. val int64, err error) {
  1701. err = s.brk.DoWithAcceptable(func() error {
  1702. conn, err := getRedis(s)
  1703. if err != nil {
  1704. return err
  1705. }
  1706. v, err := conn.ZIncrBy(ctx, key, float64(increment), field).Result()
  1707. if err != nil {
  1708. return err
  1709. }
  1710. val = int64(v)
  1711. return nil
  1712. }, acceptable)
  1713. return
  1714. }
  1715. // Zscore is the implementation of redis zscore command.
  1716. func (s *Redis) Zscore(key, value string) (int64, error) {
  1717. return s.ZscoreCtx(context.Background(), key, value)
  1718. }
  1719. // ZscoreCtx is the implementation of redis zscore command.
  1720. func (s *Redis) ZscoreCtx(ctx context.Context, key, value string) (val int64, err error) {
  1721. err = s.brk.DoWithAcceptable(func() error {
  1722. conn, err := getRedis(s)
  1723. if err != nil {
  1724. return err
  1725. }
  1726. v, err := conn.ZScore(ctx, key, value).Result()
  1727. if err != nil {
  1728. return err
  1729. }
  1730. val = int64(v)
  1731. return nil
  1732. }, acceptable)
  1733. return
  1734. }
  1735. // ZscoreByFloat is the implementation of redis zscore command score by float.
  1736. func (s *Redis) ZscoreByFloat(key, value string) (float64, error) {
  1737. return s.ZscoreByFloatCtx(context.Background(), key, value)
  1738. }
  1739. // ZscoreByFloatCtx is the implementation of redis zscore command score by float.
  1740. func (s *Redis) ZscoreByFloatCtx(ctx context.Context, key, value string) (val float64, err error) {
  1741. err = s.brk.DoWithAcceptable(func() error {
  1742. conn, err := getRedis(s)
  1743. if err != nil {
  1744. return err
  1745. }
  1746. val, err = conn.ZScore(ctx, key, value).Result()
  1747. return err
  1748. }, acceptable)
  1749. return
  1750. }
  1751. // Zscan is the implementation of redis zscan command.
  1752. func (s *Redis) Zscan(key string, cursor uint64, match string, count int64) (
  1753. keys []string, cur uint64, err error) {
  1754. return s.ZscanCtx(context.Background(), key, cursor, match, count)
  1755. }
  1756. // ZscanCtx is the implementation of redis zscan command.
  1757. func (s *Redis) ZscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  1758. keys []string, cur uint64, err error) {
  1759. err = s.brk.DoWithAcceptable(func() error {
  1760. conn, err := getRedis(s)
  1761. if err != nil {
  1762. return err
  1763. }
  1764. keys, cur, err = conn.ZScan(ctx, key, cursor, match, count).Result()
  1765. return err
  1766. }, acceptable)
  1767. return
  1768. }
  1769. // Zrank is the implementation of redis zrank command.
  1770. func (s *Redis) Zrank(key, field string) (int64, error) {
  1771. return s.ZrankCtx(context.Background(), key, field)
  1772. }
  1773. // ZrankCtx is the implementation of redis zrank command.
  1774. func (s *Redis) ZrankCtx(ctx context.Context, key, field string) (val int64, err error) {
  1775. err = s.brk.DoWithAcceptable(func() error {
  1776. conn, err := getRedis(s)
  1777. if err != nil {
  1778. return err
  1779. }
  1780. val, err = conn.ZRank(ctx, key, field).Result()
  1781. return err
  1782. }, acceptable)
  1783. return
  1784. }
  1785. // Zrem is the implementation of redis zrem command.
  1786. func (s *Redis) Zrem(key string, values ...any) (int, error) {
  1787. return s.ZremCtx(context.Background(), key, values...)
  1788. }
  1789. // ZremCtx is the implementation of redis zrem command.
  1790. func (s *Redis) ZremCtx(ctx context.Context, key string, values ...any) (val int, err error) {
  1791. err = s.brk.DoWithAcceptable(func() error {
  1792. conn, err := getRedis(s)
  1793. if err != nil {
  1794. return err
  1795. }
  1796. v, err := conn.ZRem(ctx, key, values...).Result()
  1797. if err != nil {
  1798. return err
  1799. }
  1800. val = int(v)
  1801. return nil
  1802. }, acceptable)
  1803. return
  1804. }
  1805. // Zremrangebyscore is the implementation of redis zremrangebyscore command.
  1806. func (s *Redis) Zremrangebyscore(key string, start, stop int64) (int, error) {
  1807. return s.ZremrangebyscoreCtx(context.Background(), key, start, stop)
  1808. }
  1809. // ZremrangebyscoreCtx is the implementation of redis zremrangebyscore command.
  1810. func (s *Redis) ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) (
  1811. val int, err error) {
  1812. err = s.brk.DoWithAcceptable(func() error {
  1813. conn, err := getRedis(s)
  1814. if err != nil {
  1815. return err
  1816. }
  1817. v, err := conn.ZRemRangeByScore(ctx, key, strconv.FormatInt(start, 10),
  1818. strconv.FormatInt(stop, 10)).Result()
  1819. if err != nil {
  1820. return err
  1821. }
  1822. val = int(v)
  1823. return nil
  1824. }, acceptable)
  1825. return
  1826. }
  1827. // Zremrangebyrank is the implementation of redis zremrangebyrank command.
  1828. func (s *Redis) Zremrangebyrank(key string, start, stop int64) (int, error) {
  1829. return s.ZremrangebyrankCtx(context.Background(), key, start, stop)
  1830. }
  1831. // ZremrangebyrankCtx is the implementation of redis zremrangebyrank command.
  1832. func (s *Redis) ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) (
  1833. val int, err error) {
  1834. err = s.brk.DoWithAcceptable(func() error {
  1835. conn, err := getRedis(s)
  1836. if err != nil {
  1837. return err
  1838. }
  1839. v, err := conn.ZRemRangeByRank(ctx, key, start, stop).Result()
  1840. if err != nil {
  1841. return err
  1842. }
  1843. val = int(v)
  1844. return nil
  1845. }, acceptable)
  1846. return
  1847. }
  1848. // Zrange is the implementation of redis zrange command.
  1849. func (s *Redis) Zrange(key string, start, stop int64) ([]string, error) {
  1850. return s.ZrangeCtx(context.Background(), key, start, stop)
  1851. }
  1852. // ZrangeCtx is the implementation of redis zrange command.
  1853. func (s *Redis) ZrangeCtx(ctx context.Context, key string, start, stop int64) (
  1854. val []string, err error) {
  1855. err = s.brk.DoWithAcceptable(func() error {
  1856. conn, err := getRedis(s)
  1857. if err != nil {
  1858. return err
  1859. }
  1860. val, err = conn.ZRange(ctx, key, start, stop).Result()
  1861. return err
  1862. }, acceptable)
  1863. return
  1864. }
  1865. // ZrangeWithScores is the implementation of redis zrange command with scores.
  1866. func (s *Redis) ZrangeWithScores(key string, start, stop int64) ([]Pair, error) {
  1867. return s.ZrangeWithScoresCtx(context.Background(), key, start, stop)
  1868. }
  1869. // ZrangeWithScoresCtx is the implementation of redis zrange command with scores.
  1870. func (s *Redis) ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1871. val []Pair, err error) {
  1872. err = s.brk.DoWithAcceptable(func() error {
  1873. conn, err := getRedis(s)
  1874. if err != nil {
  1875. return err
  1876. }
  1877. v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result()
  1878. if err != nil {
  1879. return err
  1880. }
  1881. val = toPairs(v)
  1882. return nil
  1883. }, acceptable)
  1884. return
  1885. }
  1886. // ZrangeWithScoresByFloat is the implementation of redis zrange command with scores by float64.
  1887. func (s *Redis) ZrangeWithScoresByFloat(key string, start, stop int64) ([]FloatPair, error) {
  1888. return s.ZrangeWithScoresByFloatCtx(context.Background(), key, start, stop)
  1889. }
  1890. // ZrangeWithScoresByFloatCtx is the implementation of redis zrange command with scores by float64.
  1891. func (s *Redis) ZrangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) (
  1892. val []FloatPair, err error) {
  1893. err = s.brk.DoWithAcceptable(func() error {
  1894. conn, err := getRedis(s)
  1895. if err != nil {
  1896. return err
  1897. }
  1898. v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result()
  1899. if err != nil {
  1900. return err
  1901. }
  1902. val = toFloatPairs(v)
  1903. return nil
  1904. }, acceptable)
  1905. return
  1906. }
  1907. // ZRevRangeWithScores is the implementation of redis zrevrange command with scores.
  1908. // Deprecated: use ZrevrangeWithScores instead.
  1909. func (s *Redis) ZRevRangeWithScores(key string, start, stop int64) ([]Pair, error) {
  1910. return s.ZrevrangeWithScoresCtx(context.Background(), key, start, stop)
  1911. }
  1912. // ZrevrangeWithScores is the implementation of redis zrevrange command with scores.
  1913. func (s *Redis) ZrevrangeWithScores(key string, start, stop int64) ([]Pair, error) {
  1914. return s.ZrevrangeWithScoresCtx(context.Background(), key, start, stop)
  1915. }
  1916. // ZRevRangeWithScoresCtx is the implementation of redis zrevrange command with scores.
  1917. // Deprecated: use ZrevrangeWithScoresCtx instead.
  1918. func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1919. val []Pair, err error) {
  1920. return s.ZrevrangeWithScoresCtx(ctx, key, start, stop)
  1921. }
  1922. // ZrevrangeWithScoresCtx is the implementation of redis zrevrange command with scores.
  1923. func (s *Redis) ZrevrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1924. val []Pair, err error) {
  1925. err = s.brk.DoWithAcceptable(func() error {
  1926. conn, err := getRedis(s)
  1927. if err != nil {
  1928. return err
  1929. }
  1930. v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result()
  1931. if err != nil {
  1932. return err
  1933. }
  1934. val = toPairs(v)
  1935. return nil
  1936. }, acceptable)
  1937. return
  1938. }
  1939. // ZRevRangeWithScoresByFloat is the implementation of redis zrevrange command with scores by float.
  1940. // Deprecated: use ZrevrangeWithScoresByFloat instead.
  1941. func (s *Redis) ZRevRangeWithScoresByFloat(key string, start, stop int64) ([]FloatPair, error) {
  1942. return s.ZrevrangeWithScoresByFloatCtx(context.Background(), key, start, stop)
  1943. }
  1944. // ZrevrangeWithScoresByFloat is the implementation of redis zrevrange command with scores by float.
  1945. func (s *Redis) ZrevrangeWithScoresByFloat(key string, start, stop int64) ([]FloatPair, error) {
  1946. return s.ZrevrangeWithScoresByFloatCtx(context.Background(), key, start, stop)
  1947. }
  1948. // ZRevRangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float.
  1949. // Deprecated: use ZrevrangeWithScoresByFloatCtx instead.
  1950. func (s *Redis) ZRevRangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) (
  1951. val []FloatPair, err error) {
  1952. return s.ZrevrangeWithScoresByFloatCtx(ctx, key, start, stop)
  1953. }
  1954. // ZrevrangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float.
  1955. func (s *Redis) ZrevrangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) (
  1956. val []FloatPair, err error) {
  1957. err = s.brk.DoWithAcceptable(func() error {
  1958. conn, err := getRedis(s)
  1959. if err != nil {
  1960. return err
  1961. }
  1962. v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result()
  1963. if err != nil {
  1964. return err
  1965. }
  1966. val = toFloatPairs(v)
  1967. return nil
  1968. }, acceptable)
  1969. return
  1970. }
  1971. // ZrangebyscoreWithScores is the implementation of redis zrangebyscore command with scores.
  1972. func (s *Redis) ZrangebyscoreWithScores(key string, start, stop int64) ([]Pair, error) {
  1973. return s.ZrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
  1974. }
  1975. // ZrangebyscoreWithScoresCtx is the implementation of redis zrangebyscore command with scores.
  1976. func (s *Redis) ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1977. val []Pair, err error) {
  1978. err = s.brk.DoWithAcceptable(func() error {
  1979. conn, err := getRedis(s)
  1980. if err != nil {
  1981. return err
  1982. }
  1983. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  1984. Min: strconv.FormatInt(start, 10),
  1985. Max: strconv.FormatInt(stop, 10),
  1986. }).Result()
  1987. if err != nil {
  1988. return err
  1989. }
  1990. val = toPairs(v)
  1991. return nil
  1992. }, acceptable)
  1993. return
  1994. }
  1995. // ZrangebyscoreWithScoresByFloat is the implementation of redis zrangebyscore command with scores by float.
  1996. func (s *Redis) ZrangebyscoreWithScoresByFloat(key string, start, stop float64) ([]FloatPair, error) {
  1997. return s.ZrangebyscoreWithScoresByFloatCtx(context.Background(), key, start, stop)
  1998. }
  1999. // ZrangebyscoreWithScoresByFloatCtx is the implementation of redis zrangebyscore command with scores by float.
  2000. func (s *Redis) ZrangebyscoreWithScoresByFloatCtx(ctx context.Context, key string, start, stop float64) (
  2001. val []FloatPair, err error) {
  2002. err = s.brk.DoWithAcceptable(func() error {
  2003. conn, err := getRedis(s)
  2004. if err != nil {
  2005. return err
  2006. }
  2007. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2008. Min: strconv.FormatFloat(start, 'f', -1, 64),
  2009. Max: strconv.FormatFloat(stop, 'f', -1, 64),
  2010. }).Result()
  2011. if err != nil {
  2012. return err
  2013. }
  2014. val = toFloatPairs(v)
  2015. return nil
  2016. }, acceptable)
  2017. return
  2018. }
  2019. // ZrangebyscoreWithScoresAndLimit is the implementation of redis zrangebyscore command
  2020. // with scores and limit.
  2021. func (s *Redis) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64,
  2022. page, size int) ([]Pair, error) {
  2023. return s.ZrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
  2024. }
  2025. // ZrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrangebyscore command
  2026. // with scores and limit.
  2027. func (s *Redis) ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start,
  2028. stop int64, page, size int) (val []Pair, err error) {
  2029. err = s.brk.DoWithAcceptable(func() error {
  2030. if size <= 0 {
  2031. return nil
  2032. }
  2033. conn, err := getRedis(s)
  2034. if err != nil {
  2035. return err
  2036. }
  2037. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2038. Min: strconv.FormatInt(start, 10),
  2039. Max: strconv.FormatInt(stop, 10),
  2040. Offset: int64(page * size),
  2041. Count: int64(size),
  2042. }).Result()
  2043. if err != nil {
  2044. return err
  2045. }
  2046. val = toPairs(v)
  2047. return nil
  2048. }, acceptable)
  2049. return
  2050. }
  2051. // ZrangebyscoreWithScoresByFloatAndLimit is the implementation of redis zrangebyscore command
  2052. // with scores by float and limit.
  2053. func (s *Redis) ZrangebyscoreWithScoresByFloatAndLimit(key string, start, stop float64,
  2054. page, size int) ([]FloatPair, error) {
  2055. return s.ZrangebyscoreWithScoresByFloatAndLimitCtx(context.Background(),
  2056. key, start, stop, page, size)
  2057. }
  2058. // ZrangebyscoreWithScoresByFloatAndLimitCtx is the implementation of redis zrangebyscore command
  2059. // with scores by float and limit.
  2060. func (s *Redis) ZrangebyscoreWithScoresByFloatAndLimitCtx(ctx context.Context, key string, start,
  2061. stop float64, page, size int) (val []FloatPair, err error) {
  2062. err = s.brk.DoWithAcceptable(func() error {
  2063. if size <= 0 {
  2064. return nil
  2065. }
  2066. conn, err := getRedis(s)
  2067. if err != nil {
  2068. return err
  2069. }
  2070. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2071. Min: strconv.FormatFloat(start, 'f', -1, 64),
  2072. Max: strconv.FormatFloat(stop, 'f', -1, 64),
  2073. Offset: int64(page * size),
  2074. Count: int64(size),
  2075. }).Result()
  2076. if err != nil {
  2077. return err
  2078. }
  2079. val = toFloatPairs(v)
  2080. return nil
  2081. }, acceptable)
  2082. return
  2083. }
  2084. // Zrevrange is the implementation of redis zrevrange command.
  2085. func (s *Redis) Zrevrange(key string, start, stop int64) ([]string, error) {
  2086. return s.ZrevrangeCtx(context.Background(), key, start, stop)
  2087. }
  2088. // ZrevrangeCtx is the implementation of redis zrevrange command.
  2089. func (s *Redis) ZrevrangeCtx(ctx context.Context, key string, start, stop int64) (
  2090. val []string, err error) {
  2091. err = s.brk.DoWithAcceptable(func() error {
  2092. conn, err := getRedis(s)
  2093. if err != nil {
  2094. return err
  2095. }
  2096. val, err = conn.ZRevRange(ctx, key, start, stop).Result()
  2097. return err
  2098. }, acceptable)
  2099. return
  2100. }
  2101. // ZrevrangebyscoreWithScores is the implementation of redis zrevrangebyscore command with scores.
  2102. func (s *Redis) ZrevrangebyscoreWithScores(key string, start, stop int64) ([]Pair, error) {
  2103. return s.ZrevrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
  2104. }
  2105. // ZrevrangebyscoreWithScoresCtx is the implementation of redis zrevrangebyscore command with scores.
  2106. func (s *Redis) ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  2107. val []Pair, err error) {
  2108. err = s.brk.DoWithAcceptable(func() error {
  2109. conn, err := getRedis(s)
  2110. if err != nil {
  2111. return err
  2112. }
  2113. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2114. Min: strconv.FormatInt(start, 10),
  2115. Max: strconv.FormatInt(stop, 10),
  2116. }).Result()
  2117. if err != nil {
  2118. return err
  2119. }
  2120. val = toPairs(v)
  2121. return nil
  2122. }, acceptable)
  2123. return
  2124. }
  2125. // ZrevrangebyscoreWithScoresByFloat is the implementation of redis zrevrangebyscore command with scores by float.
  2126. func (s *Redis) ZrevrangebyscoreWithScoresByFloat(key string, start, stop float64) (
  2127. []FloatPair, error) {
  2128. return s.ZrevrangebyscoreWithScoresByFloatCtx(context.Background(), key, start, stop)
  2129. }
  2130. // ZrevrangebyscoreWithScoresByFloatCtx is the implementation of redis zrevrangebyscore command with scores by float.
  2131. func (s *Redis) ZrevrangebyscoreWithScoresByFloatCtx(ctx context.Context, key string,
  2132. start, stop float64) (val []FloatPair, err error) {
  2133. err = s.brk.DoWithAcceptable(func() error {
  2134. conn, err := getRedis(s)
  2135. if err != nil {
  2136. return err
  2137. }
  2138. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2139. Min: strconv.FormatFloat(start, 'f', -1, 64),
  2140. Max: strconv.FormatFloat(stop, 'f', -1, 64),
  2141. }).Result()
  2142. if err != nil {
  2143. return err
  2144. }
  2145. val = toFloatPairs(v)
  2146. return nil
  2147. }, acceptable)
  2148. return
  2149. }
  2150. // ZrevrangebyscoreWithScoresAndLimit is the implementation of redis zrevrangebyscore command
  2151. // with scores and limit.
  2152. func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64,
  2153. page, size int) ([]Pair, error) {
  2154. return s.ZrevrangebyscoreWithScoresAndLimitCtx(context.Background(),
  2155. key, start, stop, page, size)
  2156. }
  2157. // ZrevrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrevrangebyscore command
  2158. // with scores and limit.
  2159. func (s *Redis) ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string,
  2160. start, stop int64, page, size int) (val []Pair, err error) {
  2161. err = s.brk.DoWithAcceptable(func() error {
  2162. if size <= 0 {
  2163. return nil
  2164. }
  2165. conn, err := getRedis(s)
  2166. if err != nil {
  2167. return err
  2168. }
  2169. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2170. Min: strconv.FormatInt(start, 10),
  2171. Max: strconv.FormatInt(stop, 10),
  2172. Offset: int64(page * size),
  2173. Count: int64(size),
  2174. }).Result()
  2175. if err != nil {
  2176. return err
  2177. }
  2178. val = toPairs(v)
  2179. return nil
  2180. }, acceptable)
  2181. return
  2182. }
  2183. // ZrevrangebyscoreWithScoresByFloatAndLimit is the implementation of redis zrevrangebyscore command
  2184. // with scores by float and limit.
  2185. func (s *Redis) ZrevrangebyscoreWithScoresByFloatAndLimit(key string, start, stop float64,
  2186. page, size int) ([]FloatPair, error) {
  2187. return s.ZrevrangebyscoreWithScoresByFloatAndLimitCtx(context.Background(),
  2188. key, start, stop, page, size)
  2189. }
  2190. // ZrevrangebyscoreWithScoresByFloatAndLimitCtx is the implementation of redis zrevrangebyscore command
  2191. // with scores by float and limit.
  2192. func (s *Redis) ZrevrangebyscoreWithScoresByFloatAndLimitCtx(ctx context.Context, key string,
  2193. start, stop float64, page, size int) (val []FloatPair, err error) {
  2194. err = s.brk.DoWithAcceptable(func() error {
  2195. if size <= 0 {
  2196. return nil
  2197. }
  2198. conn, err := getRedis(s)
  2199. if err != nil {
  2200. return err
  2201. }
  2202. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2203. Min: strconv.FormatFloat(start, 'f', -1, 64),
  2204. Max: strconv.FormatFloat(stop, 'f', -1, 64),
  2205. Offset: int64(page * size),
  2206. Count: int64(size),
  2207. }).Result()
  2208. if err != nil {
  2209. return err
  2210. }
  2211. val = toFloatPairs(v)
  2212. return nil
  2213. }, acceptable)
  2214. return
  2215. }
  2216. // Zrevrank is the implementation of redis zrevrank command.
  2217. func (s *Redis) Zrevrank(key, field string) (int64, error) {
  2218. return s.ZrevrankCtx(context.Background(), key, field)
  2219. }
  2220. // ZrevrankCtx is the implementation of redis zrevrank command.
  2221. func (s *Redis) ZrevrankCtx(ctx context.Context, key, field string) (val int64, err error) {
  2222. err = s.brk.DoWithAcceptable(func() error {
  2223. conn, err := getRedis(s)
  2224. if err != nil {
  2225. return err
  2226. }
  2227. val, err = conn.ZRevRank(ctx, key, field).Result()
  2228. return err
  2229. }, acceptable)
  2230. return
  2231. }
  2232. // Zunionstore is the implementation of redis zunionstore command.
  2233. func (s *Redis) Zunionstore(dest string, store *ZStore) (int64, error) {
  2234. return s.ZunionstoreCtx(context.Background(), dest, store)
  2235. }
  2236. // ZunionstoreCtx is the implementation of redis zunionstore command.
  2237. func (s *Redis) ZunionstoreCtx(ctx context.Context, dest string, store *ZStore) (
  2238. val int64, err error) {
  2239. err = s.brk.DoWithAcceptable(func() error {
  2240. conn, err := getRedis(s)
  2241. if err != nil {
  2242. return err
  2243. }
  2244. val, err = conn.ZUnionStore(ctx, dest, store).Result()
  2245. return err
  2246. }, acceptable)
  2247. return
  2248. }
  2249. // Cluster customizes the given Redis as a cluster.
  2250. func Cluster() Option {
  2251. return func(r *Redis) {
  2252. r.Type = ClusterType
  2253. }
  2254. }
  2255. // SetSlowThreshold sets the slow threshold.
  2256. func SetSlowThreshold(threshold time.Duration) {
  2257. slowThreshold.Set(threshold)
  2258. }
  2259. // WithPass customizes the given Redis with given password.
  2260. func WithPass(pass string) Option {
  2261. return func(r *Redis) {
  2262. r.Pass = pass
  2263. }
  2264. }
  2265. // WithTLS customizes the given Redis with TLS enabled.
  2266. func WithTLS() Option {
  2267. return func(r *Redis) {
  2268. r.tls = true
  2269. }
  2270. }
  2271. func acceptable(err error) bool {
  2272. return err == nil || err == red.Nil || err == context.Canceled
  2273. }
  2274. func getRedis(r *Redis) (RedisNode, error) {
  2275. switch r.Type {
  2276. case ClusterType:
  2277. return getCluster(r)
  2278. case NodeType:
  2279. return getClient(r)
  2280. default:
  2281. return nil, fmt.Errorf("redis type '%s' is not supported", r.Type)
  2282. }
  2283. }
  2284. func toPairs(vals []red.Z) []Pair {
  2285. pairs := make([]Pair, len(vals))
  2286. for i, val := range vals {
  2287. switch member := val.Member.(type) {
  2288. case string:
  2289. pairs[i] = Pair{
  2290. Key: member,
  2291. Score: int64(val.Score),
  2292. }
  2293. default:
  2294. pairs[i] = Pair{
  2295. Key: mapping.Repr(val.Member),
  2296. Score: int64(val.Score),
  2297. }
  2298. }
  2299. }
  2300. return pairs
  2301. }
  2302. func toFloatPairs(vals []red.Z) []FloatPair {
  2303. pairs := make([]FloatPair, len(vals))
  2304. for i, val := range vals {
  2305. switch member := val.Member.(type) {
  2306. case string:
  2307. pairs[i] = FloatPair{
  2308. Key: member,
  2309. Score: val.Score,
  2310. }
  2311. default:
  2312. pairs[i] = FloatPair{
  2313. Key: mapping.Repr(val.Member),
  2314. Score: val.Score,
  2315. }
  2316. }
  2317. }
  2318. return pairs
  2319. }
  2320. func toStrings(vals []any) []string {
  2321. ret := make([]string, len(vals))
  2322. for i, val := range vals {
  2323. if val == nil {
  2324. ret[i] = ""
  2325. continue
  2326. }
  2327. switch val := val.(type) {
  2328. case string:
  2329. ret[i] = val
  2330. default:
  2331. ret[i] = mapping.Repr(val)
  2332. }
  2333. }
  2334. return ret
  2335. }