redis.go 38 KB

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