redis.go 37 KB

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