redis.go 71 KB

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