redis.go 36 KB

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