redis.go 72 KB

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