redis.go 62 KB

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