redis.go 74 KB

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