redis.go 73 KB

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