redis.go 73 KB

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