store.go 31 KB

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