redis.go 36 KB

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