redis.go 37 KB

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