redis.go 38 KB

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