redis.go 75 KB

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