redis.go 36 KB

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