redis.go 62 KB

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