redis.go 63 KB

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