redis.go 30 KB

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