redis.go 61 KB

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