redis.go 29 KB

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