redis.go 39 KB

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