store.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. package kv
  2. import (
  3. "context"
  4. "errors"
  5. "log"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "github.com/zeromicro/go-zero/core/hash"
  8. "github.com/zeromicro/go-zero/core/stores/cache"
  9. "github.com/zeromicro/go-zero/core/stores/redis"
  10. )
  11. // ErrNoRedisNode is an error that indicates no redis node.
  12. var ErrNoRedisNode = errors.New("no redis node")
  13. type (
  14. // Store interface represents a KV store.
  15. Store interface {
  16. Decr(key string) (int64, error)
  17. DecrCtx(ctx context.Context, key string) (int64, error)
  18. Decrby(key string, decrement int64) (int64, error)
  19. DecrbyCtx(ctx context.Context, key string, decrement int64) (int64, error)
  20. Del(keys ...string) (int, error)
  21. DelCtx(ctx context.Context, keys ...string) (int, error)
  22. Eval(script, key string, args ...interface{}) (interface{}, error)
  23. EvalCtx(ctx context.Context, script, key string, args ...interface{}) (interface{}, error)
  24. Exists(key string) (bool, error)
  25. ExistsCtx(ctx context.Context, key string) (bool, error)
  26. Expire(key string, seconds int) error
  27. ExpireCtx(ctx context.Context, key string, seconds int) error
  28. Expireat(key string, expireTime int64) error
  29. ExpireatCtx(ctx context.Context, key string, expireTime int64) error
  30. Get(key string) (string, error)
  31. GetCtx(ctx context.Context, key string) (string, error)
  32. GetSet(key, value string) (string, error)
  33. GetSetCtx(ctx context.Context, key, value string) (string, error)
  34. Hdel(key, field string) (bool, error)
  35. HdelCtx(ctx context.Context, key, field string) (bool, error)
  36. Hexists(key, field string) (bool, error)
  37. HexistsCtx(ctx context.Context, key, field string) (bool, error)
  38. Hget(key, field string) (string, error)
  39. HgetCtx(ctx context.Context, key, field string) (string, error)
  40. Hgetall(key string) (map[string]string, error)
  41. HgetallCtx(ctx context.Context, key string) (map[string]string, error)
  42. Hincrby(key, field string, increment int) (int, error)
  43. HincrbyCtx(ctx context.Context, key, field string, increment int) (int, error)
  44. Hkeys(key string) ([]string, error)
  45. HkeysCtx(ctx context.Context, key string) ([]string, error)
  46. Hlen(key string) (int, error)
  47. HlenCtx(ctx context.Context, key string) (int, error)
  48. Hmget(key string, fields ...string) ([]string, error)
  49. HmgetCtx(ctx context.Context, key string, fields ...string) ([]string, error)
  50. Hset(key, field, value string) error
  51. HsetCtx(ctx context.Context, key, field, value string) error
  52. Hsetnx(key, field, value string) (bool, error)
  53. HsetnxCtx(ctx context.Context, key, field, value string) (bool, error)
  54. Hmset(key string, fieldsAndValues map[string]string) error
  55. HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error
  56. Hvals(key string) ([]string, error)
  57. HvalsCtx(ctx context.Context, key string) ([]string, error)
  58. Incr(key string) (int64, error)
  59. IncrCtx(ctx context.Context, key string) (int64, error)
  60. Incrby(key string, increment int64) (int64, error)
  61. IncrbyCtx(ctx context.Context, key string, increment int64) (int64, error)
  62. Lindex(key string, index int64) (string, error)
  63. LindexCtx(ctx context.Context, key string, index int64) (string, error)
  64. Llen(key string) (int, error)
  65. LlenCtx(ctx context.Context, key string) (int, error)
  66. Lpop(key string) (string, error)
  67. LpopCtx(ctx context.Context, key string) (string, error)
  68. Lpush(key string, values ...interface{}) (int, error)
  69. LpushCtx(ctx context.Context, key string, values ...interface{}) (int, error)
  70. Lrange(key string, start, stop int) ([]string, error)
  71. LrangeCtx(ctx context.Context, key string, start, stop int) ([]string, error)
  72. Lrem(key string, count int, value string) (int, error)
  73. LremCtx(ctx context.Context, key string, count int, value string) (int, error)
  74. Persist(key string) (bool, error)
  75. PersistCtx(ctx context.Context, key string) (bool, error)
  76. Pfadd(key string, values ...interface{}) (bool, error)
  77. PfaddCtx(ctx context.Context, key string, values ...interface{}) (bool, error)
  78. Pfcount(key string) (int64, error)
  79. PfcountCtx(ctx context.Context, key string) (int64, error)
  80. Rpush(key string, values ...interface{}) (int, error)
  81. RpushCtx(ctx context.Context, key string, values ...interface{}) (int, error)
  82. Sadd(key string, values ...interface{}) (int, error)
  83. SaddCtx(ctx context.Context, key string, values ...interface{}) (int, error)
  84. Scard(key string) (int64, error)
  85. ScardCtx(ctx context.Context, key string) (int64, error)
  86. Set(key, value string) error
  87. SetCtx(ctx context.Context, key, value string) error
  88. Setex(key, value string, seconds int) error
  89. SetexCtx(ctx context.Context, key, value string, seconds int) error
  90. Setnx(key, value string) (bool, error)
  91. SetnxCtx(ctx context.Context, key, value string) (bool, error)
  92. SetnxEx(key, value string, seconds int) (bool, error)
  93. SetnxExCtx(ctx context.Context, key, value string, seconds int) (bool, error)
  94. Sismember(key string, value interface{}) (bool, error)
  95. SismemberCtx(ctx context.Context, key string, value interface{}) (bool, error)
  96. Smembers(key string) ([]string, error)
  97. SmembersCtx(ctx context.Context, key string) ([]string, error)
  98. Spop(key string) (string, error)
  99. SpopCtx(ctx context.Context, key string) (string, error)
  100. Srandmember(key string, count int) ([]string, error)
  101. SrandmemberCtx(ctx context.Context, key string, count int) ([]string, error)
  102. Srem(key string, values ...interface{}) (int, error)
  103. SremCtx(ctx context.Context, key string, values ...interface{}) (int, error)
  104. Sscan(key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error)
  105. SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error)
  106. Ttl(key string) (int, error)
  107. TtlCtx(ctx context.Context, key string) (int, error)
  108. Zadd(key string, score int64, value string) (bool, error)
  109. ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error)
  110. Zadds(key string, ps ...redis.Pair) (int64, error)
  111. ZaddsCtx(ctx context.Context, key string, ps ...redis.Pair) (int64, error)
  112. Zcard(key string) (int, error)
  113. ZcardCtx(ctx context.Context, key string) (int, error)
  114. Zcount(key string, start, stop int64) (int, error)
  115. ZcountCtx(ctx context.Context, key string, start, stop int64) (int, error)
  116. Zincrby(key string, increment int64, field string) (int64, error)
  117. ZincrbyCtx(ctx context.Context, key string, increment int64, field string) (int64, error)
  118. Zrange(key string, start, stop int64) ([]string, error)
  119. ZrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error)
  120. ZrangeWithScores(key string, start, stop int64) ([]redis.Pair, error)
  121. ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error)
  122. ZrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error)
  123. ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error)
  124. ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) ([]redis.Pair, error)
  125. ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) ([]redis.Pair, error)
  126. Zrank(key, field string) (int64, error)
  127. ZrankCtx(ctx context.Context, key, field string) (int64, error)
  128. Zrem(key string, values ...interface{}) (int, error)
  129. ZremCtx(ctx context.Context, key string, values ...interface{}) (int, error)
  130. Zremrangebyrank(key string, start, stop int64) (int, error)
  131. ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) (int, error)
  132. Zremrangebyscore(key string, start, stop int64) (int, error)
  133. ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) (int, error)
  134. Zrevrange(key string, start, stop int64) ([]string, error)
  135. ZrevrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error)
  136. ZrevrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error)
  137. ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error)
  138. ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) ([]redis.Pair, error)
  139. ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) ([]redis.Pair, error)
  140. Zscore(key, value string) (int64, error)
  141. ZscoreCtx(ctx context.Context, key, value string) (int64, error)
  142. Zrevrank(key, field string) (int64, error)
  143. ZrevrankCtx(ctx context.Context, key, field string) (int64, error)
  144. }
  145. clusterStore struct {
  146. dispatcher *hash.ConsistentHash
  147. }
  148. )
  149. // NewStore returns a Store.
  150. func NewStore(c KvConf) Store {
  151. if len(c) == 0 || cache.TotalWeights(c) <= 0 {
  152. log.Fatal("no cache nodes")
  153. }
  154. // even if only one node, we chose to use consistent hash,
  155. // because Store and redis.Redis has different methods.
  156. dispatcher := hash.NewConsistentHash()
  157. for _, node := range c {
  158. cn := node.NewRedis()
  159. dispatcher.AddWithWeight(cn, node.Weight)
  160. }
  161. return clusterStore{
  162. dispatcher: dispatcher,
  163. }
  164. }
  165. func (cs clusterStore) Decr(key string) (int64, error) {
  166. return cs.DecrCtx(context.Background(), key)
  167. }
  168. func (cs clusterStore) DecrCtx(ctx context.Context, key string) (int64, error) {
  169. node, err := cs.getRedis(key)
  170. if err != nil {
  171. return 0, err
  172. }
  173. return node.DecrCtx(ctx, key)
  174. }
  175. func (cs clusterStore) Decrby(key string, decrement int64) (int64, error) {
  176. return cs.DecrbyCtx(context.Background(), key, decrement)
  177. }
  178. func (cs clusterStore) DecrbyCtx(ctx context.Context, key string, decrement int64) (int64, error) {
  179. node, err := cs.getRedis(key)
  180. if err != nil {
  181. return 0, err
  182. }
  183. return node.DecrbyCtx(ctx, key, decrement)
  184. }
  185. func (cs clusterStore) Del(keys ...string) (int, error) {
  186. return cs.DelCtx(context.Background(), keys...)
  187. }
  188. func (cs clusterStore) DelCtx(ctx context.Context, keys ...string) (int, error) {
  189. var val int
  190. var be errorx.BatchError
  191. for _, key := range keys {
  192. node, e := cs.getRedis(key)
  193. if e != nil {
  194. be.Add(e)
  195. continue
  196. }
  197. if v, e := node.DelCtx(ctx, key); e != nil {
  198. be.Add(e)
  199. } else {
  200. val += v
  201. }
  202. }
  203. return val, be.Err()
  204. }
  205. func (cs clusterStore) Eval(script, key string, args ...interface{}) (interface{}, error) {
  206. return cs.EvalCtx(context.Background(), script, key, args...)
  207. }
  208. func (cs clusterStore) EvalCtx(ctx context.Context, script, key string, args ...interface{}) (interface{}, error) {
  209. node, err := cs.getRedis(key)
  210. if err != nil {
  211. return nil, err
  212. }
  213. return node.EvalCtx(ctx, script, []string{key}, args...)
  214. }
  215. func (cs clusterStore) Exists(key string) (bool, error) {
  216. return cs.ExistsCtx(context.Background(), key)
  217. }
  218. func (cs clusterStore) ExistsCtx(ctx context.Context, key string) (bool, error) {
  219. node, err := cs.getRedis(key)
  220. if err != nil {
  221. return false, err
  222. }
  223. return node.ExistsCtx(ctx, key)
  224. }
  225. func (cs clusterStore) Expire(key string, seconds int) error {
  226. return cs.ExpireCtx(context.Background(), key, seconds)
  227. }
  228. func (cs clusterStore) ExpireCtx(ctx context.Context, key string, seconds int) error {
  229. node, err := cs.getRedis(key)
  230. if err != nil {
  231. return err
  232. }
  233. return node.ExpireCtx(ctx, key, seconds)
  234. }
  235. func (cs clusterStore) Expireat(key string, expireTime int64) error {
  236. return cs.ExpireatCtx(context.Background(), key, expireTime)
  237. }
  238. func (cs clusterStore) ExpireatCtx(ctx context.Context, key string, expireTime int64) error {
  239. node, err := cs.getRedis(key)
  240. if err != nil {
  241. return err
  242. }
  243. return node.ExpireatCtx(ctx, key, expireTime)
  244. }
  245. func (cs clusterStore) Get(key string) (string, error) {
  246. return cs.GetCtx(context.Background(), key)
  247. }
  248. func (cs clusterStore) GetCtx(ctx context.Context, key string) (string, error) {
  249. node, err := cs.getRedis(key)
  250. if err != nil {
  251. return "", err
  252. }
  253. return node.GetCtx(ctx, key)
  254. }
  255. func (cs clusterStore) Hdel(key, field string) (bool, error) {
  256. return cs.HdelCtx(context.Background(), key, field)
  257. }
  258. func (cs clusterStore) HdelCtx(ctx context.Context, key, field string) (bool, error) {
  259. node, err := cs.getRedis(key)
  260. if err != nil {
  261. return false, err
  262. }
  263. return node.HdelCtx(ctx, key, field)
  264. }
  265. func (cs clusterStore) Hexists(key, field string) (bool, error) {
  266. return cs.HexistsCtx(context.Background(), key, field)
  267. }
  268. func (cs clusterStore) HexistsCtx(ctx context.Context, key, field string) (bool, error) {
  269. node, err := cs.getRedis(key)
  270. if err != nil {
  271. return false, err
  272. }
  273. return node.HexistsCtx(ctx, key, field)
  274. }
  275. func (cs clusterStore) Hget(key, field string) (string, error) {
  276. return cs.HgetCtx(context.Background(), key, field)
  277. }
  278. func (cs clusterStore) HgetCtx(ctx context.Context, key, field string) (string, error) {
  279. node, err := cs.getRedis(key)
  280. if err != nil {
  281. return "", err
  282. }
  283. return node.HgetCtx(ctx, key, field)
  284. }
  285. func (cs clusterStore) Hgetall(key string) (map[string]string, error) {
  286. return cs.HgetallCtx(context.Background(), key)
  287. }
  288. func (cs clusterStore) HgetallCtx(ctx context.Context, key string) (map[string]string, error) {
  289. node, err := cs.getRedis(key)
  290. if err != nil {
  291. return nil, err
  292. }
  293. return node.HgetallCtx(ctx, key)
  294. }
  295. func (cs clusterStore) Hincrby(key, field string, increment int) (int, error) {
  296. return cs.HincrbyCtx(context.Background(), key, field, increment)
  297. }
  298. func (cs clusterStore) HincrbyCtx(ctx context.Context, key, field string, increment int) (int, error) {
  299. node, err := cs.getRedis(key)
  300. if err != nil {
  301. return 0, err
  302. }
  303. return node.HincrbyCtx(ctx, key, field, increment)
  304. }
  305. func (cs clusterStore) Hkeys(key string) ([]string, error) {
  306. return cs.HkeysCtx(context.Background(), key)
  307. }
  308. func (cs clusterStore) HkeysCtx(ctx context.Context, key string) ([]string, error) {
  309. node, err := cs.getRedis(key)
  310. if err != nil {
  311. return nil, err
  312. }
  313. return node.HkeysCtx(ctx, key)
  314. }
  315. func (cs clusterStore) Hlen(key string) (int, error) {
  316. return cs.HlenCtx(context.Background(), key)
  317. }
  318. func (cs clusterStore) HlenCtx(ctx context.Context, key string) (int, error) {
  319. node, err := cs.getRedis(key)
  320. if err != nil {
  321. return 0, err
  322. }
  323. return node.HlenCtx(ctx, key)
  324. }
  325. func (cs clusterStore) Hmget(key string, fields ...string) ([]string, error) {
  326. return cs.HmgetCtx(context.Background(), key, fields...)
  327. }
  328. func (cs clusterStore) HmgetCtx(ctx context.Context, key string, fields ...string) ([]string, error) {
  329. node, err := cs.getRedis(key)
  330. if err != nil {
  331. return nil, err
  332. }
  333. return node.HmgetCtx(ctx, key, fields...)
  334. }
  335. func (cs clusterStore) Hset(key, field, value string) error {
  336. return cs.HsetCtx(context.Background(), key, field, value)
  337. }
  338. func (cs clusterStore) HsetCtx(ctx context.Context, key, field, value string) error {
  339. node, err := cs.getRedis(key)
  340. if err != nil {
  341. return err
  342. }
  343. return node.HsetCtx(ctx, key, field, value)
  344. }
  345. func (cs clusterStore) Hsetnx(key, field, value string) (bool, error) {
  346. return cs.HsetnxCtx(context.Background(), key, field, value)
  347. }
  348. func (cs clusterStore) HsetnxCtx(ctx context.Context, key, field, value string) (bool, error) {
  349. node, err := cs.getRedis(key)
  350. if err != nil {
  351. return false, err
  352. }
  353. return node.HsetnxCtx(ctx, key, field, value)
  354. }
  355. func (cs clusterStore) Hmset(key string, fieldsAndValues map[string]string) error {
  356. return cs.HmsetCtx(context.Background(), key, fieldsAndValues)
  357. }
  358. func (cs clusterStore) HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error {
  359. node, err := cs.getRedis(key)
  360. if err != nil {
  361. return err
  362. }
  363. return node.HmsetCtx(ctx, key, fieldsAndValues)
  364. }
  365. func (cs clusterStore) Hvals(key string) ([]string, error) {
  366. return cs.HvalsCtx(context.Background(), key)
  367. }
  368. func (cs clusterStore) HvalsCtx(ctx context.Context, key string) ([]string, error) {
  369. node, err := cs.getRedis(key)
  370. if err != nil {
  371. return nil, err
  372. }
  373. return node.HvalsCtx(ctx, key)
  374. }
  375. func (cs clusterStore) Incr(key string) (int64, error) {
  376. return cs.IncrCtx(context.Background(), key)
  377. }
  378. func (cs clusterStore) IncrCtx(ctx context.Context, key string) (int64, error) {
  379. node, err := cs.getRedis(key)
  380. if err != nil {
  381. return 0, err
  382. }
  383. return node.IncrCtx(ctx, key)
  384. }
  385. func (cs clusterStore) Incrby(key string, increment int64) (int64, error) {
  386. return cs.IncrbyCtx(context.Background(), key, increment)
  387. }
  388. func (cs clusterStore) IncrbyCtx(ctx context.Context, key string, increment int64) (int64, error) {
  389. node, err := cs.getRedis(key)
  390. if err != nil {
  391. return 0, err
  392. }
  393. return node.IncrbyCtx(ctx, key, increment)
  394. }
  395. func (cs clusterStore) Llen(key string) (int, error) {
  396. return cs.LlenCtx(context.Background(), key)
  397. }
  398. func (cs clusterStore) LlenCtx(ctx context.Context, key string) (int, error) {
  399. node, err := cs.getRedis(key)
  400. if err != nil {
  401. return 0, err
  402. }
  403. return node.LlenCtx(ctx, key)
  404. }
  405. func (cs clusterStore) Lindex(key string, index int64) (string, error) {
  406. return cs.LindexCtx(context.Background(), key, index)
  407. }
  408. func (cs clusterStore) LindexCtx(ctx context.Context, key string, index int64) (string, error) {
  409. node, err := cs.getRedis(key)
  410. if err != nil {
  411. return "", err
  412. }
  413. return node.LindexCtx(ctx, key, index)
  414. }
  415. func (cs clusterStore) Lpop(key string) (string, error) {
  416. return cs.LpopCtx(context.Background(), key)
  417. }
  418. func (cs clusterStore) LpopCtx(ctx context.Context, key string) (string, error) {
  419. node, err := cs.getRedis(key)
  420. if err != nil {
  421. return "", err
  422. }
  423. return node.LpopCtx(ctx, key)
  424. }
  425. func (cs clusterStore) Lpush(key string, values ...interface{}) (int, error) {
  426. return cs.LpushCtx(context.Background(), key, values...)
  427. }
  428. func (cs clusterStore) LpushCtx(ctx context.Context, key string, values ...interface{}) (int, error) {
  429. node, err := cs.getRedis(key)
  430. if err != nil {
  431. return 0, err
  432. }
  433. return node.LpushCtx(ctx, key, values...)
  434. }
  435. func (cs clusterStore) Lrange(key string, start, stop int) ([]string, error) {
  436. return cs.LrangeCtx(context.Background(), key, start, stop)
  437. }
  438. func (cs clusterStore) LrangeCtx(ctx context.Context, key string, start, stop int) ([]string, error) {
  439. node, err := cs.getRedis(key)
  440. if err != nil {
  441. return nil, err
  442. }
  443. return node.LrangeCtx(ctx, key, start, stop)
  444. }
  445. func (cs clusterStore) Lrem(key string, count int, value string) (int, error) {
  446. return cs.LremCtx(context.Background(), key, count, value)
  447. }
  448. func (cs clusterStore) LremCtx(ctx context.Context, key string, count int, value string) (int, error) {
  449. node, err := cs.getRedis(key)
  450. if err != nil {
  451. return 0, err
  452. }
  453. return node.LremCtx(ctx, key, count, value)
  454. }
  455. func (cs clusterStore) Persist(key string) (bool, error) {
  456. return cs.PersistCtx(context.Background(), key)
  457. }
  458. func (cs clusterStore) PersistCtx(ctx context.Context, key string) (bool, error) {
  459. node, err := cs.getRedis(key)
  460. if err != nil {
  461. return false, err
  462. }
  463. return node.PersistCtx(ctx, key)
  464. }
  465. func (cs clusterStore) Pfadd(key string, values ...interface{}) (bool, error) {
  466. return cs.PfaddCtx(context.Background(), key, values...)
  467. }
  468. func (cs clusterStore) PfaddCtx(ctx context.Context, key string, values ...interface{}) (bool, error) {
  469. node, err := cs.getRedis(key)
  470. if err != nil {
  471. return false, err
  472. }
  473. return node.PfaddCtx(ctx, key, values...)
  474. }
  475. func (cs clusterStore) Pfcount(key string) (int64, error) {
  476. return cs.PfcountCtx(context.Background(), key)
  477. }
  478. func (cs clusterStore) PfcountCtx(ctx context.Context, key string) (int64, error) {
  479. node, err := cs.getRedis(key)
  480. if err != nil {
  481. return 0, err
  482. }
  483. return node.PfcountCtx(ctx, key)
  484. }
  485. func (cs clusterStore) Rpush(key string, values ...interface{}) (int, error) {
  486. return cs.RpushCtx(context.Background(), key, values...)
  487. }
  488. func (cs clusterStore) RpushCtx(ctx context.Context, key string, values ...interface{}) (int, error) {
  489. node, err := cs.getRedis(key)
  490. if err != nil {
  491. return 0, err
  492. }
  493. return node.RpushCtx(ctx, key, values...)
  494. }
  495. func (cs clusterStore) Sadd(key string, values ...interface{}) (int, error) {
  496. return cs.SaddCtx(context.Background(), key, values...)
  497. }
  498. func (cs clusterStore) SaddCtx(ctx context.Context, key string, values ...interface{}) (int, error) {
  499. node, err := cs.getRedis(key)
  500. if err != nil {
  501. return 0, err
  502. }
  503. return node.SaddCtx(ctx, key, values...)
  504. }
  505. func (cs clusterStore) Scard(key string) (int64, error) {
  506. return cs.ScardCtx(context.Background(), key)
  507. }
  508. func (cs clusterStore) ScardCtx(ctx context.Context, key string) (int64, error) {
  509. node, err := cs.getRedis(key)
  510. if err != nil {
  511. return 0, err
  512. }
  513. return node.ScardCtx(ctx, key)
  514. }
  515. func (cs clusterStore) Set(key, value string) error {
  516. return cs.SetCtx(context.Background(), key, value)
  517. }
  518. func (cs clusterStore) SetCtx(ctx context.Context, key, value string) error {
  519. node, err := cs.getRedis(key)
  520. if err != nil {
  521. return err
  522. }
  523. return node.SetCtx(ctx, key, value)
  524. }
  525. func (cs clusterStore) Setex(key, value string, seconds int) error {
  526. return cs.SetexCtx(context.Background(), key, value, seconds)
  527. }
  528. func (cs clusterStore) SetexCtx(ctx context.Context, key, value string, seconds int) error {
  529. node, err := cs.getRedis(key)
  530. if err != nil {
  531. return err
  532. }
  533. return node.SetexCtx(ctx, key, value, seconds)
  534. }
  535. func (cs clusterStore) Setnx(key, value string) (bool, error) {
  536. return cs.SetnxCtx(context.Background(), key, value)
  537. }
  538. func (cs clusterStore) SetnxCtx(ctx context.Context, key, value string) (bool, error) {
  539. node, err := cs.getRedis(key)
  540. if err != nil {
  541. return false, err
  542. }
  543. return node.SetnxCtx(ctx, key, value)
  544. }
  545. func (cs clusterStore) SetnxEx(key, value string, seconds int) (bool, error) {
  546. return cs.SetnxExCtx(context.Background(), key, value, seconds)
  547. }
  548. func (cs clusterStore) SetnxExCtx(ctx context.Context, key, value string, seconds int) (bool, error) {
  549. node, err := cs.getRedis(key)
  550. if err != nil {
  551. return false, err
  552. }
  553. return node.SetnxExCtx(ctx, key, value, seconds)
  554. }
  555. func (cs clusterStore) GetSet(key, value string) (string, error) {
  556. return cs.GetSetCtx(context.Background(), key, value)
  557. }
  558. func (cs clusterStore) GetSetCtx(ctx context.Context, key, value string) (string, error) {
  559. node, err := cs.getRedis(key)
  560. if err != nil {
  561. return "", err
  562. }
  563. return node.GetSetCtx(ctx, key, value)
  564. }
  565. func (cs clusterStore) Sismember(key string, value interface{}) (bool, error) {
  566. return cs.SismemberCtx(context.Background(), key, value)
  567. }
  568. func (cs clusterStore) SismemberCtx(ctx context.Context, key string, value interface{}) (bool, error) {
  569. node, err := cs.getRedis(key)
  570. if err != nil {
  571. return false, err
  572. }
  573. return node.SismemberCtx(ctx, key, value)
  574. }
  575. func (cs clusterStore) Smembers(key string) ([]string, error) {
  576. return cs.SmembersCtx(context.Background(), key)
  577. }
  578. func (cs clusterStore) SmembersCtx(ctx context.Context, key string) ([]string, error) {
  579. node, err := cs.getRedis(key)
  580. if err != nil {
  581. return nil, err
  582. }
  583. return node.SmembersCtx(ctx, key)
  584. }
  585. func (cs clusterStore) Spop(key string) (string, error) {
  586. return cs.SpopCtx(context.Background(), key)
  587. }
  588. func (cs clusterStore) SpopCtx(ctx context.Context, key string) (string, error) {
  589. node, err := cs.getRedis(key)
  590. if err != nil {
  591. return "", err
  592. }
  593. return node.SpopCtx(ctx, key)
  594. }
  595. func (cs clusterStore) Srandmember(key string, count int) ([]string, error) {
  596. return cs.SrandmemberCtx(context.Background(), key, count)
  597. }
  598. func (cs clusterStore) SrandmemberCtx(ctx context.Context, key string, count int) ([]string, error) {
  599. node, err := cs.getRedis(key)
  600. if err != nil {
  601. return nil, err
  602. }
  603. return node.SrandmemberCtx(ctx, key, count)
  604. }
  605. func (cs clusterStore) Srem(key string, values ...interface{}) (int, error) {
  606. return cs.SremCtx(context.Background(), key, values...)
  607. }
  608. func (cs clusterStore) SremCtx(ctx context.Context, key string, values ...interface{}) (int, error) {
  609. node, err := cs.getRedis(key)
  610. if err != nil {
  611. return 0, err
  612. }
  613. return node.SremCtx(ctx, key, values...)
  614. }
  615. func (cs clusterStore) Sscan(key string, cursor uint64, match string, count int64) (
  616. keys []string, cur uint64, err error) {
  617. return cs.SscanCtx(context.Background(), key, cursor, match, count)
  618. }
  619. func (cs clusterStore) SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  620. keys []string, cur uint64, err error) {
  621. node, err := cs.getRedis(key)
  622. if err != nil {
  623. return nil, 0, err
  624. }
  625. return node.SscanCtx(ctx, key, cursor, match, count)
  626. }
  627. func (cs clusterStore) Ttl(key string) (int, error) {
  628. return cs.TtlCtx(context.Background(), key)
  629. }
  630. func (cs clusterStore) TtlCtx(ctx context.Context, key string) (int, error) {
  631. node, err := cs.getRedis(key)
  632. if err != nil {
  633. return 0, err
  634. }
  635. return node.TtlCtx(ctx, key)
  636. }
  637. func (cs clusterStore) Zadd(key string, score int64, value string) (bool, error) {
  638. return cs.ZaddCtx(context.Background(), key, score, value)
  639. }
  640. func (cs clusterStore) ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error) {
  641. node, err := cs.getRedis(key)
  642. if err != nil {
  643. return false, err
  644. }
  645. return node.ZaddCtx(ctx, key, score, value)
  646. }
  647. func (cs clusterStore) Zadds(key string, ps ...redis.Pair) (int64, error) {
  648. return cs.ZaddsCtx(context.Background(), key, ps...)
  649. }
  650. func (cs clusterStore) ZaddsCtx(ctx context.Context, key string, ps ...redis.Pair) (int64, error) {
  651. node, err := cs.getRedis(key)
  652. if err != nil {
  653. return 0, err
  654. }
  655. return node.ZaddsCtx(ctx, key, ps...)
  656. }
  657. func (cs clusterStore) Zcard(key string) (int, error) {
  658. return cs.ZcardCtx(context.Background(), key)
  659. }
  660. func (cs clusterStore) ZcardCtx(ctx context.Context, key string) (int, error) {
  661. node, err := cs.getRedis(key)
  662. if err != nil {
  663. return 0, err
  664. }
  665. return node.ZcardCtx(ctx, key)
  666. }
  667. func (cs clusterStore) Zcount(key string, start, stop int64) (int, error) {
  668. return cs.ZcountCtx(context.Background(), key, start, stop)
  669. }
  670. func (cs clusterStore) ZcountCtx(ctx context.Context, key string, start, stop int64) (int, error) {
  671. node, err := cs.getRedis(key)
  672. if err != nil {
  673. return 0, err
  674. }
  675. return node.ZcountCtx(ctx, key, start, stop)
  676. }
  677. func (cs clusterStore) Zincrby(key string, increment int64, field string) (int64, error) {
  678. return cs.ZincrbyCtx(context.Background(), key, increment, field)
  679. }
  680. func (cs clusterStore) ZincrbyCtx(ctx context.Context, key string, increment int64, field string) (int64, error) {
  681. node, err := cs.getRedis(key)
  682. if err != nil {
  683. return 0, err
  684. }
  685. return node.ZincrbyCtx(ctx, key, increment, field)
  686. }
  687. func (cs clusterStore) Zrank(key, field string) (int64, error) {
  688. return cs.ZrankCtx(context.Background(), key, field)
  689. }
  690. func (cs clusterStore) ZrankCtx(ctx context.Context, key, field string) (int64, error) {
  691. node, err := cs.getRedis(key)
  692. if err != nil {
  693. return 0, err
  694. }
  695. return node.ZrankCtx(ctx, key, field)
  696. }
  697. func (cs clusterStore) Zrange(key string, start, stop int64) ([]string, error) {
  698. return cs.ZrangeCtx(context.Background(), key, start, stop)
  699. }
  700. func (cs clusterStore) ZrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error) {
  701. node, err := cs.getRedis(key)
  702. if err != nil {
  703. return nil, err
  704. }
  705. return node.ZrangeCtx(ctx, key, start, stop)
  706. }
  707. func (cs clusterStore) ZrangeWithScores(key string, start, stop int64) ([]redis.Pair, error) {
  708. return cs.ZrangeWithScoresCtx(context.Background(), key, start, stop)
  709. }
  710. func (cs clusterStore) ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error) {
  711. node, err := cs.getRedis(key)
  712. if err != nil {
  713. return nil, err
  714. }
  715. return node.ZrangeWithScoresCtx(ctx, key, start, stop)
  716. }
  717. func (cs clusterStore) ZrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error) {
  718. return cs.ZrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
  719. }
  720. func (cs clusterStore) ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error) {
  721. node, err := cs.getRedis(key)
  722. if err != nil {
  723. return nil, err
  724. }
  725. return node.ZrangebyscoreWithScoresCtx(ctx, key, start, stop)
  726. }
  727. func (cs clusterStore) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
  728. []redis.Pair, error) {
  729. return cs.ZrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
  730. }
  731. func (cs clusterStore) ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) (
  732. []redis.Pair, error) {
  733. node, err := cs.getRedis(key)
  734. if err != nil {
  735. return nil, err
  736. }
  737. return node.ZrangebyscoreWithScoresAndLimitCtx(ctx, key, start, stop, page, size)
  738. }
  739. func (cs clusterStore) Zrem(key string, values ...interface{}) (int, error) {
  740. return cs.ZremCtx(context.Background(), key, values...)
  741. }
  742. func (cs clusterStore) ZremCtx(ctx context.Context, key string, values ...interface{}) (int, error) {
  743. node, err := cs.getRedis(key)
  744. if err != nil {
  745. return 0, err
  746. }
  747. return node.ZremCtx(ctx, key, values...)
  748. }
  749. func (cs clusterStore) Zremrangebyrank(key string, start, stop int64) (int, error) {
  750. return cs.ZremrangebyrankCtx(context.Background(), key, start, stop)
  751. }
  752. func (cs clusterStore) ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) (int, error) {
  753. node, err := cs.getRedis(key)
  754. if err != nil {
  755. return 0, err
  756. }
  757. return node.ZremrangebyrankCtx(ctx, key, start, stop)
  758. }
  759. func (cs clusterStore) Zremrangebyscore(key string, start, stop int64) (int, error) {
  760. return cs.ZremrangebyscoreCtx(context.Background(), key, start, stop)
  761. }
  762. func (cs clusterStore) ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) (int, error) {
  763. node, err := cs.getRedis(key)
  764. if err != nil {
  765. return 0, err
  766. }
  767. return node.ZremrangebyscoreCtx(ctx, key, start, stop)
  768. }
  769. func (cs clusterStore) Zrevrange(key string, start, stop int64) ([]string, error) {
  770. return cs.ZrevrangeCtx(context.Background(), key, start, stop)
  771. }
  772. func (cs clusterStore) ZrevrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error) {
  773. node, err := cs.getRedis(key)
  774. if err != nil {
  775. return nil, err
  776. }
  777. return node.ZrevrangeCtx(ctx, key, start, stop)
  778. }
  779. func (cs clusterStore) ZrevrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error) {
  780. return cs.ZrevrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
  781. }
  782. func (cs clusterStore) ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error) {
  783. node, err := cs.getRedis(key)
  784. if err != nil {
  785. return nil, err
  786. }
  787. return node.ZrevrangebyscoreWithScoresCtx(ctx, key, start, stop)
  788. }
  789. func (cs clusterStore) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
  790. []redis.Pair, error) {
  791. return cs.ZrevrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
  792. }
  793. func (cs clusterStore) ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) (
  794. []redis.Pair, error) {
  795. node, err := cs.getRedis(key)
  796. if err != nil {
  797. return nil, err
  798. }
  799. return node.ZrevrangebyscoreWithScoresAndLimitCtx(ctx, key, start, stop, page, size)
  800. }
  801. func (cs clusterStore) Zrevrank(key, field string) (int64, error) {
  802. return cs.ZrevrankCtx(context.Background(), key, field)
  803. }
  804. func (cs clusterStore) ZrevrankCtx(ctx context.Context, key, field string) (int64, error) {
  805. node, err := cs.getRedis(key)
  806. if err != nil {
  807. return 0, err
  808. }
  809. return node.ZrevrankCtx(ctx, key, field)
  810. }
  811. func (cs clusterStore) Zscore(key, value string) (int64, error) {
  812. return cs.ZscoreCtx(context.Background(), key, value)
  813. }
  814. func (cs clusterStore) ZscoreCtx(ctx context.Context, key, value string) (int64, error) {
  815. node, err := cs.getRedis(key)
  816. if err != nil {
  817. return 0, err
  818. }
  819. return node.ZscoreCtx(ctx, key, value)
  820. }
  821. func (cs clusterStore) getRedis(key string) (*redis.Redis, error) {
  822. val, ok := cs.dispatcher.Get(key)
  823. if !ok {
  824. return nil, ErrNoRedisNode
  825. }
  826. return val.(*redis.Redis), nil
  827. }