redis.go 75 KB

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