redis.go 60 KB

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