redis.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. package redis
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. red "github.com/go-redis/redis"
  8. "github.com/tal-tech/go-zero/core/breaker"
  9. "github.com/tal-tech/go-zero/core/mapping"
  10. )
  11. const (
  12. // ClusterType means redis cluster.
  13. ClusterType = "cluster"
  14. // NodeType means redis node.
  15. NodeType = "node"
  16. // Nil is an alias of redis.Nil.
  17. Nil = red.Nil
  18. blockingQueryTimeout = 5 * time.Second
  19. readWriteTimeout = 2 * time.Second
  20. slowThreshold = time.Millisecond * 100
  21. )
  22. // ErrNilNode is an error that indicates a nil redis node.
  23. var ErrNilNode = errors.New("nil redis node")
  24. type (
  25. // Option defines the method to customize a Redis.
  26. Option func(r *Redis)
  27. // A Pair is a key/pair set used in redis zset.
  28. Pair struct {
  29. Key string
  30. Score int64
  31. }
  32. // Redis defines a redis node/cluster. It is thread-safe.
  33. Redis struct {
  34. Addr string
  35. Type string
  36. Pass string
  37. tls bool
  38. brk breaker.Breaker
  39. }
  40. // RedisNode interface represents a redis node.
  41. RedisNode interface {
  42. red.Cmdable
  43. }
  44. // GeoLocation is used with GeoAdd to add geospatial location.
  45. GeoLocation = red.GeoLocation
  46. // GeoRadiusQuery is used with GeoRadius to query geospatial index.
  47. GeoRadiusQuery = red.GeoRadiusQuery
  48. // GeoPos is used to represent a geo position.
  49. GeoPos = red.GeoPos
  50. // Pipeliner is an alias of redis.Pipeliner.
  51. Pipeliner = red.Pipeliner
  52. // Z represents sorted set member.
  53. Z = red.Z
  54. // ZStore is an alias of redis.ZStore.
  55. ZStore = red.ZStore
  56. // IntCmd is an alias of redis.IntCmd.
  57. IntCmd = red.IntCmd
  58. // FloatCmd is an alias of redis.FloatCmd.
  59. FloatCmd = red.FloatCmd
  60. )
  61. // New returns a Redis with given options.
  62. func New(addr string, opts ...Option) *Redis {
  63. r := &Redis{
  64. Addr: addr,
  65. Type: NodeType,
  66. brk: breaker.NewBreaker(),
  67. }
  68. for _, opt := range opts {
  69. opt(r)
  70. }
  71. return r
  72. }
  73. // NewRedis returns a Redis.
  74. func NewRedis(redisAddr, redisType string, redisPass ...string) *Redis {
  75. var opts []Option
  76. if redisType == ClusterType {
  77. opts = append(opts, Cluster())
  78. }
  79. for _, v := range redisPass {
  80. opts = append(opts, WithPass(v))
  81. }
  82. return New(redisAddr, opts...)
  83. }
  84. // BitCount is redis bitcount command implementation.
  85. func (s *Redis) BitCount(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(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) (val int64, err error) {
  101. err = s.brk.DoWithAcceptable(func() error {
  102. conn, err := getRedis(s)
  103. if err != nil {
  104. return err
  105. }
  106. val, err = conn.BitOpAnd(destKey, keys...).Result()
  107. return err
  108. }, acceptable)
  109. return
  110. }
  111. // BitOpNot is redis bit operation (not) command implementation.
  112. func (s *Redis) BitOpNot(destKey, key string) (val int64, err error) {
  113. err = s.brk.DoWithAcceptable(func() error {
  114. conn, err := getRedis(s)
  115. if err != nil {
  116. return err
  117. }
  118. val, err = conn.BitOpNot(destKey, key).Result()
  119. return err
  120. }, acceptable)
  121. return
  122. }
  123. // BitOpOr is redis bit operation (or) command implementation.
  124. func (s *Redis) BitOpOr(destKey string, keys ...string) (val int64, err error) {
  125. err = s.brk.DoWithAcceptable(func() error {
  126. conn, err := getRedis(s)
  127. if err != nil {
  128. return err
  129. }
  130. val, err = conn.BitOpOr(destKey, keys...).Result()
  131. return err
  132. }, acceptable)
  133. return
  134. }
  135. // BitOpXor is redis bit operation (xor) command implementation.
  136. func (s *Redis) BitOpXor(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.BitOpXor(destKey, keys...).Result()
  143. return err
  144. }, acceptable)
  145. return
  146. }
  147. // BitPos is redis bitpos command implementation.
  148. func (s *Redis) BitPos(key string, bit, start, end int64) (val int64, err error) {
  149. err = s.brk.DoWithAcceptable(func() error {
  150. conn, err := getRedis(s)
  151. if err != nil {
  152. return err
  153. }
  154. val, err = conn.BitPos(key, bit, start, end).Result()
  155. return err
  156. }, acceptable)
  157. return
  158. }
  159. // Blpop uses passed in redis connection to execute blocking queries.
  160. // Doesn't benefit from pooling redis connections of blocking queries
  161. func (s *Redis) Blpop(redisNode RedisNode, key string) (string, error) {
  162. if redisNode == nil {
  163. return "", ErrNilNode
  164. }
  165. vals, err := redisNode.BLPop(blockingQueryTimeout, key).Result()
  166. if err != nil {
  167. return "", err
  168. }
  169. if len(vals) < 2 {
  170. return "", fmt.Errorf("no value on key: %s", key)
  171. }
  172. return vals[1], nil
  173. }
  174. // BlpopEx uses passed in redis connection to execute blpop command.
  175. // The difference against Blpop is that this method returns a bool to indicate success.
  176. func (s *Redis) BlpopEx(redisNode RedisNode, key string) (string, bool, error) {
  177. if redisNode == nil {
  178. return "", false, ErrNilNode
  179. }
  180. vals, err := redisNode.BLPop(blockingQueryTimeout, key).Result()
  181. if err != nil {
  182. return "", false, err
  183. }
  184. if len(vals) < 2 {
  185. return "", false, fmt.Errorf("no value on key: %s", key)
  186. }
  187. return vals[1], true, nil
  188. }
  189. // Del deletes keys.
  190. func (s *Redis) Del(keys ...string) (val int, err error) {
  191. err = s.brk.DoWithAcceptable(func() error {
  192. conn, err := getRedis(s)
  193. if err != nil {
  194. return err
  195. }
  196. v, err := conn.Del(keys...).Result()
  197. if err != nil {
  198. return err
  199. }
  200. val = int(v)
  201. return nil
  202. }, acceptable)
  203. return
  204. }
  205. // Eval is the implementation of redis eval command.
  206. func (s *Redis) Eval(script string, keys []string, args ...interface{}) (val interface{}, err error) {
  207. err = s.brk.DoWithAcceptable(func() error {
  208. conn, err := getRedis(s)
  209. if err != nil {
  210. return err
  211. }
  212. val, err = conn.Eval(script, keys, args...).Result()
  213. return err
  214. }, acceptable)
  215. return
  216. }
  217. // EvalSha is the implementation of redis evalsha command.
  218. func (s *Redis) EvalSha(sha string, keys []string, args ...interface{}) (val interface{}, err error) {
  219. err = s.brk.DoWithAcceptable(func() error {
  220. conn, err := getRedis(s)
  221. if err != nil {
  222. return err
  223. }
  224. val, err = conn.EvalSha(sha, keys, args...).Result()
  225. return err
  226. }, acceptable)
  227. return
  228. }
  229. // Exists is the implementation of redis exists command.
  230. func (s *Redis) Exists(key string) (val bool, err error) {
  231. err = s.brk.DoWithAcceptable(func() error {
  232. conn, err := getRedis(s)
  233. if err != nil {
  234. return err
  235. }
  236. v, err := conn.Exists(key).Result()
  237. if err != nil {
  238. return err
  239. }
  240. val = v == 1
  241. return nil
  242. }, acceptable)
  243. return
  244. }
  245. // Expire is the implementation of redis expire command.
  246. func (s *Redis) Expire(key string, seconds int) error {
  247. return s.brk.DoWithAcceptable(func() error {
  248. conn, err := getRedis(s)
  249. if err != nil {
  250. return err
  251. }
  252. return conn.Expire(key, time.Duration(seconds)*time.Second).Err()
  253. }, acceptable)
  254. }
  255. // Expireat is the implementation of redis expireat command.
  256. func (s *Redis) Expireat(key string, expireTime int64) error {
  257. return s.brk.DoWithAcceptable(func() error {
  258. conn, err := getRedis(s)
  259. if err != nil {
  260. return err
  261. }
  262. return conn.ExpireAt(key, time.Unix(expireTime, 0)).Err()
  263. }, acceptable)
  264. }
  265. // GeoAdd is the implementation of redis geoadd command.
  266. func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (val int64, err error) {
  267. err = s.brk.DoWithAcceptable(func() error {
  268. conn, err := getRedis(s)
  269. if err != nil {
  270. return err
  271. }
  272. v, err := conn.GeoAdd(key, geoLocation...).Result()
  273. if err != nil {
  274. return err
  275. }
  276. val = v
  277. return nil
  278. }, acceptable)
  279. return
  280. }
  281. // GeoDist is the implementation of redis geodist command.
  282. func (s *Redis) GeoDist(key, member1, member2, unit string) (val float64, err error) {
  283. err = s.brk.DoWithAcceptable(func() error {
  284. conn, err := getRedis(s)
  285. if err != nil {
  286. return err
  287. }
  288. v, err := conn.GeoDist(key, member1, member2, unit).Result()
  289. if err != nil {
  290. return err
  291. }
  292. val = v
  293. return nil
  294. }, acceptable)
  295. return
  296. }
  297. // GeoHash is the implementation of redis geohash command.
  298. func (s *Redis) GeoHash(key string, members ...string) (val []string, err error) {
  299. err = s.brk.DoWithAcceptable(func() error {
  300. conn, err := getRedis(s)
  301. if err != nil {
  302. return err
  303. }
  304. v, err := conn.GeoHash(key, members...).Result()
  305. if err != nil {
  306. return err
  307. }
  308. val = v
  309. return nil
  310. }, acceptable)
  311. return
  312. }
  313. // GeoRadius is the implementation of redis georadius command.
  314. func (s *Redis) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) (val []GeoLocation, err error) {
  315. err = s.brk.DoWithAcceptable(func() error {
  316. conn, err := getRedis(s)
  317. if err != nil {
  318. return err
  319. }
  320. v, err := conn.GeoRadius(key, longitude, latitude, query).Result()
  321. if err != nil {
  322. return err
  323. }
  324. val = v
  325. return nil
  326. }, acceptable)
  327. return
  328. }
  329. // GeoRadiusByMember is the implementation of redis georadiusbymember command.
  330. func (s *Redis) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) (val []GeoLocation, err error) {
  331. err = s.brk.DoWithAcceptable(func() error {
  332. conn, err := getRedis(s)
  333. if err != nil {
  334. return err
  335. }
  336. v, err := conn.GeoRadiusByMember(key, member, query).Result()
  337. if err != nil {
  338. return err
  339. }
  340. val = v
  341. return nil
  342. }, acceptable)
  343. return
  344. }
  345. // GeoPos is the implementation of redis geopos command.
  346. func (s *Redis) GeoPos(key string, members ...string) (val []*GeoPos, err error) {
  347. err = s.brk.DoWithAcceptable(func() error {
  348. conn, err := getRedis(s)
  349. if err != nil {
  350. return err
  351. }
  352. v, err := conn.GeoPos(key, members...).Result()
  353. if err != nil {
  354. return err
  355. }
  356. val = v
  357. return nil
  358. }, acceptable)
  359. return
  360. }
  361. // Get is the implementation of redis get command.
  362. func (s *Redis) Get(key string) (val string, err error) {
  363. err = s.brk.DoWithAcceptable(func() error {
  364. conn, err := getRedis(s)
  365. if err != nil {
  366. return err
  367. }
  368. if val, err = conn.Get(key).Result(); err == red.Nil {
  369. return nil
  370. } else if err != nil {
  371. return err
  372. } else {
  373. return nil
  374. }
  375. }, acceptable)
  376. return
  377. }
  378. // GetBit is the implementation of redis getbit command.
  379. func (s *Redis) GetBit(key string, offset int64) (val int, err error) {
  380. err = s.brk.DoWithAcceptable(func() error {
  381. conn, err := getRedis(s)
  382. if err != nil {
  383. return err
  384. }
  385. v, err := conn.GetBit(key, offset).Result()
  386. if err != nil {
  387. return err
  388. }
  389. val = int(v)
  390. return nil
  391. }, acceptable)
  392. return
  393. }
  394. // Hdel is the implementation of redis hdel command.
  395. func (s *Redis) Hdel(key string, fields ...string) (val bool, err error) {
  396. err = s.brk.DoWithAcceptable(func() error {
  397. conn, err := getRedis(s)
  398. if err != nil {
  399. return err
  400. }
  401. v, err := conn.HDel(key, fields...).Result()
  402. if err != nil {
  403. return err
  404. }
  405. val = v == 1
  406. return nil
  407. }, acceptable)
  408. return
  409. }
  410. // Hexists is the implementation of redis hexists command.
  411. func (s *Redis) Hexists(key, field string) (val bool, err error) {
  412. err = s.brk.DoWithAcceptable(func() error {
  413. conn, err := getRedis(s)
  414. if err != nil {
  415. return err
  416. }
  417. val, err = conn.HExists(key, field).Result()
  418. return err
  419. }, acceptable)
  420. return
  421. }
  422. // Hget is the implementation of redis hget command.
  423. func (s *Redis) Hget(key, field string) (val string, err error) {
  424. err = s.brk.DoWithAcceptable(func() error {
  425. conn, err := getRedis(s)
  426. if err != nil {
  427. return err
  428. }
  429. val, err = conn.HGet(key, field).Result()
  430. return err
  431. }, acceptable)
  432. return
  433. }
  434. // Hgetall is the implementation of redis hgetall command.
  435. func (s *Redis) Hgetall(key string) (val map[string]string, err error) {
  436. err = s.brk.DoWithAcceptable(func() error {
  437. conn, err := getRedis(s)
  438. if err != nil {
  439. return err
  440. }
  441. val, err = conn.HGetAll(key).Result()
  442. return err
  443. }, acceptable)
  444. return
  445. }
  446. // Hincrby is the implementation of redis hincrby command.
  447. func (s *Redis) Hincrby(key, field string, increment int) (val int, err error) {
  448. err = s.brk.DoWithAcceptable(func() error {
  449. conn, err := getRedis(s)
  450. if err != nil {
  451. return err
  452. }
  453. v, err := conn.HIncrBy(key, field, int64(increment)).Result()
  454. if err != nil {
  455. return err
  456. }
  457. val = int(v)
  458. return nil
  459. }, acceptable)
  460. return
  461. }
  462. // Hkeys is the implementation of redis hkeys command.
  463. func (s *Redis) Hkeys(key string) (val []string, err error) {
  464. err = s.brk.DoWithAcceptable(func() error {
  465. conn, err := getRedis(s)
  466. if err != nil {
  467. return err
  468. }
  469. val, err = conn.HKeys(key).Result()
  470. return err
  471. }, acceptable)
  472. return
  473. }
  474. // Hlen is the implementation of redis hlen command.
  475. func (s *Redis) Hlen(key string) (val int, err error) {
  476. err = s.brk.DoWithAcceptable(func() error {
  477. conn, err := getRedis(s)
  478. if err != nil {
  479. return err
  480. }
  481. v, err := conn.HLen(key).Result()
  482. if err != nil {
  483. return err
  484. }
  485. val = int(v)
  486. return nil
  487. }, acceptable)
  488. return
  489. }
  490. // Hmget is the implementation of redis hmget command.
  491. func (s *Redis) Hmget(key string, fields ...string) (val []string, err error) {
  492. err = s.brk.DoWithAcceptable(func() error {
  493. conn, err := getRedis(s)
  494. if err != nil {
  495. return err
  496. }
  497. v, err := conn.HMGet(key, fields...).Result()
  498. if err != nil {
  499. return err
  500. }
  501. val = toStrings(v)
  502. return nil
  503. }, acceptable)
  504. return
  505. }
  506. // Hset is the implementation of redis hset command.
  507. func (s *Redis) Hset(key, field, value string) error {
  508. return s.brk.DoWithAcceptable(func() error {
  509. conn, err := getRedis(s)
  510. if err != nil {
  511. return err
  512. }
  513. return conn.HSet(key, field, value).Err()
  514. }, acceptable)
  515. }
  516. // Hsetnx is the implementation of redis hsetnx command.
  517. func (s *Redis) Hsetnx(key, field, value string) (val bool, err error) {
  518. err = s.brk.DoWithAcceptable(func() error {
  519. conn, err := getRedis(s)
  520. if err != nil {
  521. return err
  522. }
  523. val, err = conn.HSetNX(key, field, value).Result()
  524. return err
  525. }, acceptable)
  526. return
  527. }
  528. // Hmset is the implementation of redis hmset command.
  529. func (s *Redis) Hmset(key string, fieldsAndValues map[string]string) error {
  530. return s.brk.DoWithAcceptable(func() error {
  531. conn, err := getRedis(s)
  532. if err != nil {
  533. return err
  534. }
  535. vals := make(map[string]interface{}, len(fieldsAndValues))
  536. for k, v := range fieldsAndValues {
  537. vals[k] = v
  538. }
  539. return conn.HMSet(key, vals).Err()
  540. }, acceptable)
  541. }
  542. // Hscan is the implementation of redis hscan command.
  543. func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
  544. err = s.brk.DoWithAcceptable(func() error {
  545. conn, err := getRedis(s)
  546. if err != nil {
  547. return err
  548. }
  549. keys, cur, err = conn.HScan(key, cursor, match, count).Result()
  550. return err
  551. }, acceptable)
  552. return
  553. }
  554. // Hvals is the implementation of redis hvals command.
  555. func (s *Redis) Hvals(key string) (val []string, err error) {
  556. err = s.brk.DoWithAcceptable(func() error {
  557. conn, err := getRedis(s)
  558. if err != nil {
  559. return err
  560. }
  561. val, err = conn.HVals(key).Result()
  562. return err
  563. }, acceptable)
  564. return
  565. }
  566. // Incr is the implementation of redis incr command.
  567. func (s *Redis) Incr(key string) (val int64, err error) {
  568. err = s.brk.DoWithAcceptable(func() error {
  569. conn, err := getRedis(s)
  570. if err != nil {
  571. return err
  572. }
  573. val, err = conn.Incr(key).Result()
  574. return err
  575. }, acceptable)
  576. return
  577. }
  578. // Incrby is the implementation of redis incrby command.
  579. func (s *Redis) Incrby(key string, increment int64) (val int64, err error) {
  580. err = s.brk.DoWithAcceptable(func() error {
  581. conn, err := getRedis(s)
  582. if err != nil {
  583. return err
  584. }
  585. val, err = conn.IncrBy(key, int64(increment)).Result()
  586. return err
  587. }, acceptable)
  588. return
  589. }
  590. // Keys is the implementation of redis keys command.
  591. func (s *Redis) Keys(pattern 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.Keys(pattern).Result()
  598. return err
  599. }, acceptable)
  600. return
  601. }
  602. // Llen is the implementation of redis llen command.
  603. func (s *Redis) Llen(key string) (val int, err error) {
  604. err = s.brk.DoWithAcceptable(func() error {
  605. conn, err := getRedis(s)
  606. if err != nil {
  607. return err
  608. }
  609. v, err := conn.LLen(key).Result()
  610. if err != nil {
  611. return err
  612. }
  613. val = int(v)
  614. return nil
  615. }, acceptable)
  616. return
  617. }
  618. // Lpop is the implementation of redis lpop command.
  619. func (s *Redis) Lpop(key string) (val string, err error) {
  620. err = s.brk.DoWithAcceptable(func() error {
  621. conn, err := getRedis(s)
  622. if err != nil {
  623. return err
  624. }
  625. val, err = conn.LPop(key).Result()
  626. return err
  627. }, acceptable)
  628. return
  629. }
  630. // Lpush is the implementation of redis lpush command.
  631. func (s *Redis) Lpush(key string, values ...interface{}) (val int, err error) {
  632. err = s.brk.DoWithAcceptable(func() error {
  633. conn, err := getRedis(s)
  634. if err != nil {
  635. return err
  636. }
  637. v, err := conn.LPush(key, values...).Result()
  638. if err != nil {
  639. return err
  640. }
  641. val = int(v)
  642. return nil
  643. }, acceptable)
  644. return
  645. }
  646. // Lrange is the implementation of redis lrange command.
  647. func (s *Redis) Lrange(key string, start, stop int) (val []string, err error) {
  648. err = s.brk.DoWithAcceptable(func() error {
  649. conn, err := getRedis(s)
  650. if err != nil {
  651. return err
  652. }
  653. val, err = conn.LRange(key, int64(start), int64(stop)).Result()
  654. return err
  655. }, acceptable)
  656. return
  657. }
  658. // Lrem is the implementation of redis lrem command.
  659. func (s *Redis) Lrem(key string, count int, value 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.LRem(key, int64(count), value).Result()
  666. if err != nil {
  667. return err
  668. }
  669. val = int(v)
  670. return nil
  671. }, acceptable)
  672. return
  673. }
  674. // Mget is the implementation of redis mget command.
  675. func (s *Redis) Mget(keys ...string) (val []string, err error) {
  676. err = s.brk.DoWithAcceptable(func() error {
  677. conn, err := getRedis(s)
  678. if err != nil {
  679. return err
  680. }
  681. v, err := conn.MGet(keys...).Result()
  682. if err != nil {
  683. return err
  684. }
  685. val = toStrings(v)
  686. return nil
  687. }, acceptable)
  688. return
  689. }
  690. // Persist is the implementation of redis persist command.
  691. func (s *Redis) Persist(key string) (val bool, err error) {
  692. err = s.brk.DoWithAcceptable(func() error {
  693. conn, err := getRedis(s)
  694. if err != nil {
  695. return err
  696. }
  697. val, err = conn.Persist(key).Result()
  698. return err
  699. }, acceptable)
  700. return
  701. }
  702. // Pfadd is the implementation of redis pfadd command.
  703. func (s *Redis) Pfadd(key string, values ...interface{}) (val bool, err error) {
  704. err = s.brk.DoWithAcceptable(func() error {
  705. conn, err := getRedis(s)
  706. if err != nil {
  707. return err
  708. }
  709. v, err := conn.PFAdd(key, values...).Result()
  710. if err != nil {
  711. return err
  712. }
  713. val = v == 1
  714. return nil
  715. }, acceptable)
  716. return
  717. }
  718. // Pfcount is the implementation of redis pfcount command.
  719. func (s *Redis) Pfcount(key string) (val int64, err error) {
  720. err = s.brk.DoWithAcceptable(func() error {
  721. conn, err := getRedis(s)
  722. if err != nil {
  723. return err
  724. }
  725. val, err = conn.PFCount(key).Result()
  726. return err
  727. }, acceptable)
  728. return
  729. }
  730. // Pfmerge is the implementation of redis pfmerge command.
  731. func (s *Redis) Pfmerge(dest string, keys ...string) error {
  732. return s.brk.DoWithAcceptable(func() error {
  733. conn, err := getRedis(s)
  734. if err != nil {
  735. return err
  736. }
  737. _, err = conn.PFMerge(dest, keys...).Result()
  738. return err
  739. }, acceptable)
  740. }
  741. // Ping is the implementation of redis ping command.
  742. func (s *Redis) Ping() (val bool) {
  743. // ignore error, error means false
  744. _ = s.brk.DoWithAcceptable(func() error {
  745. conn, err := getRedis(s)
  746. if err != nil {
  747. val = false
  748. return nil
  749. }
  750. v, err := conn.Ping().Result()
  751. if err != nil {
  752. val = false
  753. return nil
  754. }
  755. val = v == "PONG"
  756. return nil
  757. }, acceptable)
  758. return
  759. }
  760. // Pipelined lets fn to execute pipelined commands.
  761. func (s *Redis) Pipelined(fn func(Pipeliner) error) (err error) {
  762. err = s.brk.DoWithAcceptable(func() error {
  763. conn, err := getRedis(s)
  764. if err != nil {
  765. return err
  766. }
  767. _, err = conn.Pipelined(fn)
  768. return err
  769. }, acceptable)
  770. return
  771. }
  772. // Rpop is the implementation of redis rpop command.
  773. func (s *Redis) Rpop(key string) (val string, err error) {
  774. err = s.brk.DoWithAcceptable(func() error {
  775. conn, err := getRedis(s)
  776. if err != nil {
  777. return err
  778. }
  779. val, err = conn.RPop(key).Result()
  780. return err
  781. }, acceptable)
  782. return
  783. }
  784. // Rpush is the implementation of redis rpush command.
  785. func (s *Redis) Rpush(key string, values ...interface{}) (val int, err error) {
  786. err = s.brk.DoWithAcceptable(func() error {
  787. conn, err := getRedis(s)
  788. if err != nil {
  789. return err
  790. }
  791. v, err := conn.RPush(key, values...).Result()
  792. if err != nil {
  793. return err
  794. }
  795. val = int(v)
  796. return nil
  797. }, acceptable)
  798. return
  799. }
  800. // Sadd is the implementation of redis sadd command.
  801. func (s *Redis) Sadd(key string, values ...interface{}) (val int, err error) {
  802. err = s.brk.DoWithAcceptable(func() error {
  803. conn, err := getRedis(s)
  804. if err != nil {
  805. return err
  806. }
  807. v, err := conn.SAdd(key, values...).Result()
  808. if err != nil {
  809. return err
  810. }
  811. val = int(v)
  812. return nil
  813. }, acceptable)
  814. return
  815. }
  816. // Scan is the implementation of redis scan command.
  817. func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
  818. err = s.brk.DoWithAcceptable(func() error {
  819. conn, err := getRedis(s)
  820. if err != nil {
  821. return err
  822. }
  823. keys, cur, err = conn.Scan(cursor, match, count).Result()
  824. return err
  825. }, acceptable)
  826. return
  827. }
  828. // SetBit is the implementation of redis setbit command.
  829. func (s *Redis) SetBit(key string, offset int64, value int) error {
  830. return s.brk.DoWithAcceptable(func() error {
  831. conn, err := getRedis(s)
  832. if err != nil {
  833. return err
  834. }
  835. _, err = conn.SetBit(key, offset, value).Result()
  836. return err
  837. }, acceptable)
  838. }
  839. // Sscan is the implementation of redis sscan command.
  840. func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
  841. err = s.brk.DoWithAcceptable(func() error {
  842. conn, err := getRedis(s)
  843. if err != nil {
  844. return err
  845. }
  846. keys, cur, err = conn.SScan(key, cursor, match, count).Result()
  847. return err
  848. }, acceptable)
  849. return
  850. }
  851. // Scard is the implementation of redis scard command.
  852. func (s *Redis) Scard(key string) (val int64, err error) {
  853. err = s.brk.DoWithAcceptable(func() error {
  854. conn, err := getRedis(s)
  855. if err != nil {
  856. return err
  857. }
  858. val, err = conn.SCard(key).Result()
  859. return err
  860. }, acceptable)
  861. return
  862. }
  863. // ScriptLoad is the implementation of redis script load command.
  864. func (s *Redis) ScriptLoad(script string) (string, error) {
  865. conn, err := getRedis(s)
  866. if err != nil {
  867. return "", err
  868. }
  869. return conn.ScriptLoad(script).Result()
  870. }
  871. // Set is the implementation of redis set command.
  872. func (s *Redis) Set(key, value string) error {
  873. return s.brk.DoWithAcceptable(func() error {
  874. conn, err := getRedis(s)
  875. if err != nil {
  876. return err
  877. }
  878. return conn.Set(key, value, 0).Err()
  879. }, acceptable)
  880. }
  881. // Setex is the implementation of redis setex command.
  882. func (s *Redis) Setex(key, value string, seconds int) error {
  883. return s.brk.DoWithAcceptable(func() error {
  884. conn, err := getRedis(s)
  885. if err != nil {
  886. return err
  887. }
  888. return conn.Set(key, value, time.Duration(seconds)*time.Second).Err()
  889. }, acceptable)
  890. }
  891. // Setnx is the implementation of redis setnx command.
  892. func (s *Redis) Setnx(key, value string) (val bool, err error) {
  893. err = s.brk.DoWithAcceptable(func() error {
  894. conn, err := getRedis(s)
  895. if err != nil {
  896. return err
  897. }
  898. val, err = conn.SetNX(key, value, 0).Result()
  899. return err
  900. }, acceptable)
  901. return
  902. }
  903. // SetnxEx is the implementation of redis setnx command with expire.
  904. func (s *Redis) SetnxEx(key, value string, seconds int) (val bool, err error) {
  905. err = s.brk.DoWithAcceptable(func() error {
  906. conn, err := getRedis(s)
  907. if err != nil {
  908. return err
  909. }
  910. val, err = conn.SetNX(key, value, time.Duration(seconds)*time.Second).Result()
  911. return err
  912. }, acceptable)
  913. return
  914. }
  915. // Sismember is the implementation of redis sismember command.
  916. func (s *Redis) Sismember(key string, value interface{}) (val bool, err error) {
  917. err = s.brk.DoWithAcceptable(func() error {
  918. conn, err := getRedis(s)
  919. if err != nil {
  920. return err
  921. }
  922. val, err = conn.SIsMember(key, value).Result()
  923. return err
  924. }, acceptable)
  925. return
  926. }
  927. // Smembers is the implementation of redis smembers command.
  928. func (s *Redis) Smembers(key string) (val []string, err error) {
  929. err = s.brk.DoWithAcceptable(func() error {
  930. conn, err := getRedis(s)
  931. if err != nil {
  932. return err
  933. }
  934. val, err = conn.SMembers(key).Result()
  935. return err
  936. }, acceptable)
  937. return
  938. }
  939. // Spop is the implementation of redis spop command.
  940. func (s *Redis) Spop(key 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. val, err = conn.SPop(key).Result()
  947. return err
  948. }, acceptable)
  949. return
  950. }
  951. // Srandmember is the implementation of redis srandmember command.
  952. func (s *Redis) Srandmember(key string, count int) (val []string, err error) {
  953. err = s.brk.DoWithAcceptable(func() error {
  954. conn, err := getRedis(s)
  955. if err != nil {
  956. return err
  957. }
  958. val, err = conn.SRandMemberN(key, int64(count)).Result()
  959. return err
  960. }, acceptable)
  961. return
  962. }
  963. // Srem is the implementation of redis srem command.
  964. func (s *Redis) Srem(key string, values ...interface{}) (val int, err error) {
  965. err = s.brk.DoWithAcceptable(func() error {
  966. conn, err := getRedis(s)
  967. if err != nil {
  968. return err
  969. }
  970. v, err := conn.SRem(key, values...).Result()
  971. if err != nil {
  972. return err
  973. }
  974. val = int(v)
  975. return nil
  976. }, acceptable)
  977. return
  978. }
  979. // String returns the string representation of s.
  980. func (s *Redis) String() string {
  981. return s.Addr
  982. }
  983. // Sunion is the implementation of redis sunion command.
  984. func (s *Redis) Sunion(keys ...string) (val []string, err error) {
  985. err = s.brk.DoWithAcceptable(func() error {
  986. conn, err := getRedis(s)
  987. if err != nil {
  988. return err
  989. }
  990. val, err = conn.SUnion(keys...).Result()
  991. return err
  992. }, acceptable)
  993. return
  994. }
  995. // Sunionstore is the implementation of redis sunionstore command.
  996. func (s *Redis) Sunionstore(destination string, keys ...string) (val int, err error) {
  997. err = s.brk.DoWithAcceptable(func() error {
  998. conn, err := getRedis(s)
  999. if err != nil {
  1000. return err
  1001. }
  1002. v, err := conn.SUnionStore(destination, keys...).Result()
  1003. if err != nil {
  1004. return err
  1005. }
  1006. val = int(v)
  1007. return nil
  1008. }, acceptable)
  1009. return
  1010. }
  1011. // Sdiff is the implementation of redis sdiff command.
  1012. func (s *Redis) Sdiff(keys ...string) (val []string, err error) {
  1013. err = s.brk.DoWithAcceptable(func() error {
  1014. conn, err := getRedis(s)
  1015. if err != nil {
  1016. return err
  1017. }
  1018. val, err = conn.SDiff(keys...).Result()
  1019. return err
  1020. }, acceptable)
  1021. return
  1022. }
  1023. // Sdiffstore is the implementation of redis sdiffstore command.
  1024. func (s *Redis) Sdiffstore(destination string, keys ...string) (val int, err error) {
  1025. err = s.brk.DoWithAcceptable(func() error {
  1026. conn, err := getRedis(s)
  1027. if err != nil {
  1028. return err
  1029. }
  1030. v, err := conn.SDiffStore(destination, keys...).Result()
  1031. if err != nil {
  1032. return err
  1033. }
  1034. val = int(v)
  1035. return nil
  1036. }, acceptable)
  1037. return
  1038. }
  1039. // Sinter is the implementation of redis sinter command.
  1040. func (s *Redis) Sinter(keys ...string) (val []string, err error) {
  1041. err = s.brk.DoWithAcceptable(func() error {
  1042. conn, err := getRedis(s)
  1043. if err != nil {
  1044. return err
  1045. }
  1046. val, err = conn.SInter(keys...).Result()
  1047. return err
  1048. }, acceptable)
  1049. return
  1050. }
  1051. // Sinterstore is the implementation of redis sinterstore command.
  1052. func (s *Redis) Sinterstore(destination string, keys ...string) (val int, err error) {
  1053. err = s.brk.DoWithAcceptable(func() error {
  1054. conn, err := getRedis(s)
  1055. if err != nil {
  1056. return err
  1057. }
  1058. v, err := conn.SInterStore(destination, keys...).Result()
  1059. if err != nil {
  1060. return err
  1061. }
  1062. val = int(v)
  1063. return nil
  1064. }, acceptable)
  1065. return
  1066. }
  1067. // Ttl is the implementation of redis ttl command.
  1068. func (s *Redis) Ttl(key string) (val int, err error) {
  1069. err = s.brk.DoWithAcceptable(func() error {
  1070. conn, err := getRedis(s)
  1071. if err != nil {
  1072. return err
  1073. }
  1074. duration, err := conn.TTL(key).Result()
  1075. if err != nil {
  1076. return err
  1077. }
  1078. val = int(duration / time.Second)
  1079. return nil
  1080. }, acceptable)
  1081. return
  1082. }
  1083. // Zadd is the implementation of redis zadd command.
  1084. func (s *Redis) Zadd(key string, score int64, value string) (val bool, err error) {
  1085. err = s.brk.DoWithAcceptable(func() error {
  1086. conn, err := getRedis(s)
  1087. if err != nil {
  1088. return err
  1089. }
  1090. v, err := conn.ZAdd(key, red.Z{
  1091. Score: float64(score),
  1092. Member: value,
  1093. }).Result()
  1094. if err != nil {
  1095. return err
  1096. }
  1097. val = v == 1
  1098. return nil
  1099. }, acceptable)
  1100. return
  1101. }
  1102. // Zadds is the implementation of redis zadds command.
  1103. func (s *Redis) Zadds(key string, ps ...Pair) (val int64, err error) {
  1104. err = s.brk.DoWithAcceptable(func() error {
  1105. conn, err := getRedis(s)
  1106. if err != nil {
  1107. return err
  1108. }
  1109. var zs []red.Z
  1110. for _, p := range ps {
  1111. z := red.Z{Score: float64(p.Score), Member: p.Key}
  1112. zs = append(zs, z)
  1113. }
  1114. v, err := conn.ZAdd(key, zs...).Result()
  1115. if err != nil {
  1116. return err
  1117. }
  1118. val = v
  1119. return nil
  1120. }, acceptable)
  1121. return
  1122. }
  1123. // Zcard is the implementation of redis zcard command.
  1124. func (s *Redis) Zcard(key string) (val int, err error) {
  1125. err = s.brk.DoWithAcceptable(func() error {
  1126. conn, err := getRedis(s)
  1127. if err != nil {
  1128. return err
  1129. }
  1130. v, err := conn.ZCard(key).Result()
  1131. if err != nil {
  1132. return err
  1133. }
  1134. val = int(v)
  1135. return nil
  1136. }, acceptable)
  1137. return
  1138. }
  1139. // Zcount is the implementation of redis zcount command.
  1140. func (s *Redis) Zcount(key string, start, stop int64) (val int, err error) {
  1141. err = s.brk.DoWithAcceptable(func() error {
  1142. conn, err := getRedis(s)
  1143. if err != nil {
  1144. return err
  1145. }
  1146. v, err := conn.ZCount(key, strconv.FormatInt(start, 10), strconv.FormatInt(stop, 10)).Result()
  1147. if err != nil {
  1148. return err
  1149. }
  1150. val = int(v)
  1151. return nil
  1152. }, acceptable)
  1153. return
  1154. }
  1155. // Zincrby is the implementation of redis zincrby command.
  1156. func (s *Redis) Zincrby(key string, increment int64, field string) (val int64, err error) {
  1157. err = s.brk.DoWithAcceptable(func() error {
  1158. conn, err := getRedis(s)
  1159. if err != nil {
  1160. return err
  1161. }
  1162. v, err := conn.ZIncrBy(key, float64(increment), field).Result()
  1163. if err != nil {
  1164. return err
  1165. }
  1166. val = int64(v)
  1167. return nil
  1168. }, acceptable)
  1169. return
  1170. }
  1171. // Zscore is the implementation of redis zscore command.
  1172. func (s *Redis) Zscore(key, value string) (val int64, err error) {
  1173. err = s.brk.DoWithAcceptable(func() error {
  1174. conn, err := getRedis(s)
  1175. if err != nil {
  1176. return err
  1177. }
  1178. v, err := conn.ZScore(key, value).Result()
  1179. if err != nil {
  1180. return err
  1181. }
  1182. val = int64(v)
  1183. return nil
  1184. }, acceptable)
  1185. return
  1186. }
  1187. // Zrank is the implementation of redis zrank command.
  1188. func (s *Redis) Zrank(key, field 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.ZRank(key, field).Result()
  1195. return err
  1196. }, acceptable)
  1197. return
  1198. }
  1199. // Zrem is the implementation of redis zrem command.
  1200. func (s *Redis) Zrem(key string, values ...interface{}) (val int, err error) {
  1201. err = s.brk.DoWithAcceptable(func() error {
  1202. conn, err := getRedis(s)
  1203. if err != nil {
  1204. return err
  1205. }
  1206. v, err := conn.ZRem(key, values...).Result()
  1207. if err != nil {
  1208. return err
  1209. }
  1210. val = int(v)
  1211. return nil
  1212. }, acceptable)
  1213. return
  1214. }
  1215. // Zremrangebyscore is the implementation of redis zremrangebyscore command.
  1216. func (s *Redis) Zremrangebyscore(key string, start, stop int64) (val int, err error) {
  1217. err = s.brk.DoWithAcceptable(func() error {
  1218. conn, err := getRedis(s)
  1219. if err != nil {
  1220. return err
  1221. }
  1222. v, err := conn.ZRemRangeByScore(key, strconv.FormatInt(start, 10),
  1223. strconv.FormatInt(stop, 10)).Result()
  1224. if err != nil {
  1225. return err
  1226. }
  1227. val = int(v)
  1228. return nil
  1229. }, acceptable)
  1230. return
  1231. }
  1232. // Zremrangebyrank is the implementation of redis zremrangebyrank command.
  1233. func (s *Redis) Zremrangebyrank(key string, start, stop int64) (val int, err error) {
  1234. err = s.brk.DoWithAcceptable(func() error {
  1235. conn, err := getRedis(s)
  1236. if err != nil {
  1237. return err
  1238. }
  1239. v, err := conn.ZRemRangeByRank(key, start, stop).Result()
  1240. if err != nil {
  1241. return err
  1242. }
  1243. val = int(v)
  1244. return nil
  1245. }, acceptable)
  1246. return
  1247. }
  1248. // Zrange is the implementation of redis zrange command.
  1249. func (s *Redis) Zrange(key string, start, stop int64) (val []string, 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.ZRange(key, start, stop).Result()
  1256. return err
  1257. }, acceptable)
  1258. return
  1259. }
  1260. // ZrangeWithScores is the implementation of redis zrange command with scores.
  1261. func (s *Redis) ZrangeWithScores(key string, start, stop int64) (val []Pair, err error) {
  1262. err = s.brk.DoWithAcceptable(func() error {
  1263. conn, err := getRedis(s)
  1264. if err != nil {
  1265. return err
  1266. }
  1267. v, err := conn.ZRangeWithScores(key, start, stop).Result()
  1268. if err != nil {
  1269. return err
  1270. }
  1271. val = toPairs(v)
  1272. return nil
  1273. }, acceptable)
  1274. return
  1275. }
  1276. // ZRevRangeWithScores is the implementation of redis zrevrange command with scores.
  1277. func (s *Redis) ZRevRangeWithScores(key string, start, stop int64) (val []Pair, err error) {
  1278. err = s.brk.DoWithAcceptable(func() error {
  1279. conn, err := getRedis(s)
  1280. if err != nil {
  1281. return err
  1282. }
  1283. v, err := conn.ZRevRangeWithScores(key, start, stop).Result()
  1284. if err != nil {
  1285. return err
  1286. }
  1287. val = toPairs(v)
  1288. return nil
  1289. }, acceptable)
  1290. return
  1291. }
  1292. // ZrangebyscoreWithScores is the implementation of redis zrangebyscore command with scores.
  1293. func (s *Redis) ZrangebyscoreWithScores(key string, start, stop int64) (val []Pair, err error) {
  1294. err = s.brk.DoWithAcceptable(func() error {
  1295. conn, err := getRedis(s)
  1296. if err != nil {
  1297. return err
  1298. }
  1299. v, err := conn.ZRangeByScoreWithScores(key, red.ZRangeBy{
  1300. Min: strconv.FormatInt(start, 10),
  1301. Max: strconv.FormatInt(stop, 10),
  1302. }).Result()
  1303. if err != nil {
  1304. return err
  1305. }
  1306. val = toPairs(v)
  1307. return nil
  1308. }, acceptable)
  1309. return
  1310. }
  1311. // ZrangebyscoreWithScoresAndLimit is the implementation of redis zrangebyscore command with scores and limit.
  1312. func (s *Redis) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
  1313. val []Pair, err error) {
  1314. err = s.brk.DoWithAcceptable(func() error {
  1315. if size <= 0 {
  1316. return nil
  1317. }
  1318. conn, err := getRedis(s)
  1319. if err != nil {
  1320. return err
  1321. }
  1322. v, err := conn.ZRangeByScoreWithScores(key, red.ZRangeBy{
  1323. Min: strconv.FormatInt(start, 10),
  1324. Max: strconv.FormatInt(stop, 10),
  1325. Offset: int64(page * size),
  1326. Count: int64(size),
  1327. }).Result()
  1328. if err != nil {
  1329. return err
  1330. }
  1331. val = toPairs(v)
  1332. return nil
  1333. }, acceptable)
  1334. return
  1335. }
  1336. // Zrevrange is the implementation of redis zrevrange command.
  1337. func (s *Redis) Zrevrange(key string, start, stop int64) (val []string, err error) {
  1338. err = s.brk.DoWithAcceptable(func() error {
  1339. conn, err := getRedis(s)
  1340. if err != nil {
  1341. return err
  1342. }
  1343. val, err = conn.ZRevRange(key, start, stop).Result()
  1344. return err
  1345. }, acceptable)
  1346. return
  1347. }
  1348. // ZrevrangebyscoreWithScores is the implementation of redis zrevrangebyscore command with scores.
  1349. func (s *Redis) ZrevrangebyscoreWithScores(key string, start, stop int64) (val []Pair, err error) {
  1350. err = s.brk.DoWithAcceptable(func() error {
  1351. conn, err := getRedis(s)
  1352. if err != nil {
  1353. return err
  1354. }
  1355. v, err := conn.ZRevRangeByScoreWithScores(key, red.ZRangeBy{
  1356. Min: strconv.FormatInt(start, 10),
  1357. Max: strconv.FormatInt(stop, 10),
  1358. }).Result()
  1359. if err != nil {
  1360. return err
  1361. }
  1362. val = toPairs(v)
  1363. return nil
  1364. }, acceptable)
  1365. return
  1366. }
  1367. // ZrevrangebyscoreWithScoresAndLimit is the implementation of redis zrevrangebyscore command with scores and limit.
  1368. func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
  1369. val []Pair, err error) {
  1370. err = s.brk.DoWithAcceptable(func() error {
  1371. if size <= 0 {
  1372. return nil
  1373. }
  1374. conn, err := getRedis(s)
  1375. if err != nil {
  1376. return err
  1377. }
  1378. v, err := conn.ZRevRangeByScoreWithScores(key, red.ZRangeBy{
  1379. Min: strconv.FormatInt(start, 10),
  1380. Max: strconv.FormatInt(stop, 10),
  1381. Offset: int64(page * size),
  1382. Count: int64(size),
  1383. }).Result()
  1384. if err != nil {
  1385. return err
  1386. }
  1387. val = toPairs(v)
  1388. return nil
  1389. }, acceptable)
  1390. return
  1391. }
  1392. // Zrevrank is the implementation of redis zrevrank command.
  1393. func (s *Redis) Zrevrank(key, field string) (val int64, err error) {
  1394. err = s.brk.DoWithAcceptable(func() error {
  1395. conn, err := getRedis(s)
  1396. if err != nil {
  1397. return err
  1398. }
  1399. val, err = conn.ZRevRank(key, field).Result()
  1400. return err
  1401. }, acceptable)
  1402. return
  1403. }
  1404. // Zunionstore is the implementation of redis zunionstore command.
  1405. func (s *Redis) Zunionstore(dest string, store ZStore, keys ...string) (val int64, err error) {
  1406. err = s.brk.DoWithAcceptable(func() error {
  1407. conn, err := getRedis(s)
  1408. if err != nil {
  1409. return err
  1410. }
  1411. val, err = conn.ZUnionStore(dest, store, keys...).Result()
  1412. return err
  1413. }, acceptable)
  1414. return
  1415. }
  1416. // Cluster customizes the given Redis as a cluster.
  1417. func Cluster() Option {
  1418. return func(r *Redis) {
  1419. r.Type = ClusterType
  1420. }
  1421. }
  1422. // WithPass customizes the given Redis with given password.
  1423. func WithPass(pass string) Option {
  1424. return func(r *Redis) {
  1425. r.Pass = pass
  1426. }
  1427. }
  1428. // WithTLS customizes the given Redis with TLS enabled.
  1429. func WithTLS() Option {
  1430. return func(r *Redis) {
  1431. r.tls = true
  1432. }
  1433. }
  1434. func acceptable(err error) bool {
  1435. return err == nil || err == red.Nil
  1436. }
  1437. func getRedis(r *Redis) (RedisNode, error) {
  1438. switch r.Type {
  1439. case ClusterType:
  1440. return getCluster(r)
  1441. case NodeType:
  1442. return getClient(r)
  1443. default:
  1444. return nil, fmt.Errorf("redis type '%s' is not supported", r.Type)
  1445. }
  1446. }
  1447. func toPairs(vals []red.Z) []Pair {
  1448. pairs := make([]Pair, len(vals))
  1449. for i, val := range vals {
  1450. switch member := val.Member.(type) {
  1451. case string:
  1452. pairs[i] = Pair{
  1453. Key: member,
  1454. Score: int64(val.Score),
  1455. }
  1456. default:
  1457. pairs[i] = Pair{
  1458. Key: mapping.Repr(val.Member),
  1459. Score: int64(val.Score),
  1460. }
  1461. }
  1462. }
  1463. return pairs
  1464. }
  1465. func toStrings(vals []interface{}) []string {
  1466. ret := make([]string, len(vals))
  1467. for i, val := range vals {
  1468. if val == nil {
  1469. ret[i] = ""
  1470. } else {
  1471. switch val := val.(type) {
  1472. case string:
  1473. ret[i] = val
  1474. default:
  1475. ret[i] = mapping.Repr(val)
  1476. }
  1477. }
  1478. }
  1479. return ret
  1480. }