redis.go 61 KB

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