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