123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041 |
- package kv
- import (
- "context"
- "errors"
- "log"
- "github.com/wuntsong-org/go-zero-plus/core/errorx"
- "github.com/wuntsong-org/go-zero-plus/core/hash"
- "github.com/wuntsong-org/go-zero-plus/core/stores/cache"
- "github.com/wuntsong-org/go-zero-plus/core/stores/redis"
- )
- // ErrNoRedisNode is an error that indicates no redis node.
- var ErrNoRedisNode = errors.New("no redis node")
- type (
- // Store interface represents a KV store.
- Store interface {
- Decr(key string) (int64, error)
- DecrCtx(ctx context.Context, key string) (int64, error)
- Decrby(key string, decrement int64) (int64, error)
- DecrbyCtx(ctx context.Context, key string, decrement int64) (int64, error)
- Del(keys ...string) (int, error)
- DelCtx(ctx context.Context, keys ...string) (int, error)
- Eval(script, key string, args ...any) (any, error)
- EvalCtx(ctx context.Context, script, key string, args ...any) (any, error)
- Exists(key string) (bool, error)
- ExistsCtx(ctx context.Context, key string) (bool, error)
- Expire(key string, seconds int) error
- ExpireCtx(ctx context.Context, key string, seconds int) error
- Expireat(key string, expireTime int64) error
- ExpireatCtx(ctx context.Context, key string, expireTime int64) error
- Get(key string) (string, error)
- GetCtx(ctx context.Context, key string) (string, error)
- GetSet(key, value string) (string, error)
- GetSetCtx(ctx context.Context, key, value string) (string, error)
- Hdel(key, field string) (bool, error)
- HdelCtx(ctx context.Context, key, field string) (bool, error)
- Hexists(key, field string) (bool, error)
- HexistsCtx(ctx context.Context, key, field string) (bool, error)
- Hget(key, field string) (string, error)
- HgetCtx(ctx context.Context, key, field string) (string, error)
- Hgetall(key string) (map[string]string, error)
- HgetallCtx(ctx context.Context, key string) (map[string]string, error)
- Hincrby(key, field string, increment int) (int, error)
- HincrbyCtx(ctx context.Context, key, field string, increment int) (int, error)
- Hkeys(key string) ([]string, error)
- HkeysCtx(ctx context.Context, key string) ([]string, error)
- Hlen(key string) (int, error)
- HlenCtx(ctx context.Context, key string) (int, error)
- Hmget(key string, fields ...string) ([]string, error)
- HmgetCtx(ctx context.Context, key string, fields ...string) ([]string, error)
- Hset(key, field, value string) error
- HsetCtx(ctx context.Context, key, field, value string) error
- Hsetnx(key, field, value string) (bool, error)
- HsetnxCtx(ctx context.Context, key, field, value string) (bool, error)
- Hmset(key string, fieldsAndValues map[string]string) error
- HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error
- Hvals(key string) ([]string, error)
- HvalsCtx(ctx context.Context, key string) ([]string, error)
- Incr(key string) (int64, error)
- IncrCtx(ctx context.Context, key string) (int64, error)
- Incrby(key string, increment int64) (int64, error)
- IncrbyCtx(ctx context.Context, key string, increment int64) (int64, error)
- Lindex(key string, index int64) (string, error)
- LindexCtx(ctx context.Context, key string, index int64) (string, error)
- Llen(key string) (int, error)
- LlenCtx(ctx context.Context, key string) (int, error)
- Lpop(key string) (string, error)
- LpopCtx(ctx context.Context, key string) (string, error)
- Lpush(key string, values ...any) (int, error)
- LpushCtx(ctx context.Context, key string, values ...any) (int, error)
- Lrange(key string, start, stop int) ([]string, error)
- LrangeCtx(ctx context.Context, key string, start, stop int) ([]string, error)
- Lrem(key string, count int, value string) (int, error)
- LremCtx(ctx context.Context, key string, count int, value string) (int, error)
- Persist(key string) (bool, error)
- PersistCtx(ctx context.Context, key string) (bool, error)
- Pfadd(key string, values ...any) (bool, error)
- PfaddCtx(ctx context.Context, key string, values ...any) (bool, error)
- Pfcount(key string) (int64, error)
- PfcountCtx(ctx context.Context, key string) (int64, error)
- Rpush(key string, values ...any) (int, error)
- RpushCtx(ctx context.Context, key string, values ...any) (int, error)
- Sadd(key string, values ...any) (int, error)
- SaddCtx(ctx context.Context, key string, values ...any) (int, error)
- Scard(key string) (int64, error)
- ScardCtx(ctx context.Context, key string) (int64, error)
- Set(key, value string) error
- SetCtx(ctx context.Context, key, value string) error
- Setex(key, value string, seconds int) error
- SetexCtx(ctx context.Context, key, value string, seconds int) error
- Setnx(key, value string) (bool, error)
- SetnxCtx(ctx context.Context, key, value string) (bool, error)
- SetnxEx(key, value string, seconds int) (bool, error)
- SetnxExCtx(ctx context.Context, key, value string, seconds int) (bool, error)
- Sismember(key string, value any) (bool, error)
- SismemberCtx(ctx context.Context, key string, value any) (bool, error)
- Smembers(key string) ([]string, error)
- SmembersCtx(ctx context.Context, key string) ([]string, error)
- Spop(key string) (string, error)
- SpopCtx(ctx context.Context, key string) (string, error)
- Srandmember(key string, count int) ([]string, error)
- SrandmemberCtx(ctx context.Context, key string, count int) ([]string, error)
- Srem(key string, values ...any) (int, error)
- SremCtx(ctx context.Context, key string, values ...any) (int, error)
- Sscan(key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error)
- SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (keys []string, cur uint64, err error)
- Ttl(key string) (int, error)
- TtlCtx(ctx context.Context, key string) (int, error)
- Zadd(key string, score int64, value string) (bool, error)
- ZaddFloat(key string, score float64, value string) (bool, error)
- ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error)
- ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (bool, error)
- Zadds(key string, ps ...redis.Pair) (int64, error)
- ZaddsCtx(ctx context.Context, key string, ps ...redis.Pair) (int64, error)
- Zcard(key string) (int, error)
- ZcardCtx(ctx context.Context, key string) (int, error)
- Zcount(key string, start, stop int64) (int, error)
- ZcountCtx(ctx context.Context, key string, start, stop int64) (int, error)
- Zincrby(key string, increment int64, field string) (int64, error)
- ZincrbyCtx(ctx context.Context, key string, increment int64, field string) (int64, error)
- Zrange(key string, start, stop int64) ([]string, error)
- ZrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error)
- ZrangeWithScores(key string, start, stop int64) ([]redis.Pair, error)
- ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error)
- ZrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error)
- ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error)
- ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) ([]redis.Pair, error)
- ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) ([]redis.Pair, error)
- Zrank(key, field string) (int64, error)
- ZrankCtx(ctx context.Context, key, field string) (int64, error)
- Zrem(key string, values ...any) (int, error)
- ZremCtx(ctx context.Context, key string, values ...any) (int, error)
- Zremrangebyrank(key string, start, stop int64) (int, error)
- ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) (int, error)
- Zremrangebyscore(key string, start, stop int64) (int, error)
- ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) (int, error)
- Zrevrange(key string, start, stop int64) ([]string, error)
- ZrevrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error)
- ZrevrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error)
- ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error)
- ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) ([]redis.Pair, error)
- ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) ([]redis.Pair, error)
- Zscore(key, value string) (int64, error)
- ZscoreCtx(ctx context.Context, key, value string) (int64, error)
- Zrevrank(key, field string) (int64, error)
- ZrevrankCtx(ctx context.Context, key, field string) (int64, error)
- }
- clusterStore struct {
- dispatcher *hash.ConsistentHash
- }
- )
- // NewStore returns a Store.
- func NewStore(c KvConf) Store {
- if len(c) == 0 || cache.TotalWeights(c) <= 0 {
- log.Fatal("no cache nodes")
- }
- // even if only one node, we chose to use consistent hash,
- // because Store and redis.Redis has different methods.
- dispatcher := hash.NewConsistentHash()
- for _, node := range c {
- cn := redis.MustNewRedis(node.RedisConf)
- dispatcher.AddWithWeight(cn, node.Weight)
- }
- return clusterStore{
- dispatcher: dispatcher,
- }
- }
- func (cs clusterStore) Decr(key string) (int64, error) {
- return cs.DecrCtx(context.Background(), key)
- }
- func (cs clusterStore) DecrCtx(ctx context.Context, key string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.DecrCtx(ctx, key)
- }
- func (cs clusterStore) Decrby(key string, decrement int64) (int64, error) {
- return cs.DecrbyCtx(context.Background(), key, decrement)
- }
- func (cs clusterStore) DecrbyCtx(ctx context.Context, key string, decrement int64) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.DecrbyCtx(ctx, key, decrement)
- }
- func (cs clusterStore) Del(keys ...string) (int, error) {
- return cs.DelCtx(context.Background(), keys...)
- }
- func (cs clusterStore) DelCtx(ctx context.Context, keys ...string) (int, error) {
- var val int
- var be errorx.BatchError
- for _, key := range keys {
- node, e := cs.getRedis(key)
- if e != nil {
- be.Add(e)
- continue
- }
- if v, e := node.DelCtx(ctx, key); e != nil {
- be.Add(e)
- } else {
- val += v
- }
- }
- return val, be.Err()
- }
- func (cs clusterStore) Eval(script, key string, args ...any) (any, error) {
- return cs.EvalCtx(context.Background(), script, key, args...)
- }
- func (cs clusterStore) EvalCtx(ctx context.Context, script, key string, args ...any) (any, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.EvalCtx(ctx, script, []string{key}, args...)
- }
- func (cs clusterStore) Exists(key string) (bool, error) {
- return cs.ExistsCtx(context.Background(), key)
- }
- func (cs clusterStore) ExistsCtx(ctx context.Context, key string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.ExistsCtx(ctx, key)
- }
- func (cs clusterStore) Expire(key string, seconds int) error {
- return cs.ExpireCtx(context.Background(), key, seconds)
- }
- func (cs clusterStore) ExpireCtx(ctx context.Context, key string, seconds int) error {
- node, err := cs.getRedis(key)
- if err != nil {
- return err
- }
- return node.ExpireCtx(ctx, key, seconds)
- }
- func (cs clusterStore) Expireat(key string, expireTime int64) error {
- return cs.ExpireatCtx(context.Background(), key, expireTime)
- }
- func (cs clusterStore) ExpireatCtx(ctx context.Context, key string, expireTime int64) error {
- node, err := cs.getRedis(key)
- if err != nil {
- return err
- }
- return node.ExpireatCtx(ctx, key, expireTime)
- }
- func (cs clusterStore) Get(key string) (string, error) {
- return cs.GetCtx(context.Background(), key)
- }
- func (cs clusterStore) GetCtx(ctx context.Context, key string) (string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return "", err
- }
- return node.GetCtx(ctx, key)
- }
- func (cs clusterStore) Hdel(key, field string) (bool, error) {
- return cs.HdelCtx(context.Background(), key, field)
- }
- func (cs clusterStore) HdelCtx(ctx context.Context, key, field string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.HdelCtx(ctx, key, field)
- }
- func (cs clusterStore) Hexists(key, field string) (bool, error) {
- return cs.HexistsCtx(context.Background(), key, field)
- }
- func (cs clusterStore) HexistsCtx(ctx context.Context, key, field string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.HexistsCtx(ctx, key, field)
- }
- func (cs clusterStore) Hget(key, field string) (string, error) {
- return cs.HgetCtx(context.Background(), key, field)
- }
- func (cs clusterStore) HgetCtx(ctx context.Context, key, field string) (string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return "", err
- }
- return node.HgetCtx(ctx, key, field)
- }
- func (cs clusterStore) Hgetall(key string) (map[string]string, error) {
- return cs.HgetallCtx(context.Background(), key)
- }
- func (cs clusterStore) HgetallCtx(ctx context.Context, key string) (map[string]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.HgetallCtx(ctx, key)
- }
- func (cs clusterStore) Hincrby(key, field string, increment int) (int, error) {
- return cs.HincrbyCtx(context.Background(), key, field, increment)
- }
- func (cs clusterStore) HincrbyCtx(ctx context.Context, key, field string, increment int) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.HincrbyCtx(ctx, key, field, increment)
- }
- func (cs clusterStore) Hkeys(key string) ([]string, error) {
- return cs.HkeysCtx(context.Background(), key)
- }
- func (cs clusterStore) HkeysCtx(ctx context.Context, key string) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.HkeysCtx(ctx, key)
- }
- func (cs clusterStore) Hlen(key string) (int, error) {
- return cs.HlenCtx(context.Background(), key)
- }
- func (cs clusterStore) HlenCtx(ctx context.Context, key string) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.HlenCtx(ctx, key)
- }
- func (cs clusterStore) Hmget(key string, fields ...string) ([]string, error) {
- return cs.HmgetCtx(context.Background(), key, fields...)
- }
- func (cs clusterStore) HmgetCtx(ctx context.Context, key string, fields ...string) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.HmgetCtx(ctx, key, fields...)
- }
- func (cs clusterStore) Hset(key, field, value string) error {
- return cs.HsetCtx(context.Background(), key, field, value)
- }
- func (cs clusterStore) HsetCtx(ctx context.Context, key, field, value string) error {
- node, err := cs.getRedis(key)
- if err != nil {
- return err
- }
- return node.HsetCtx(ctx, key, field, value)
- }
- func (cs clusterStore) Hsetnx(key, field, value string) (bool, error) {
- return cs.HsetnxCtx(context.Background(), key, field, value)
- }
- func (cs clusterStore) HsetnxCtx(ctx context.Context, key, field, value string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.HsetnxCtx(ctx, key, field, value)
- }
- func (cs clusterStore) Hmset(key string, fieldsAndValues map[string]string) error {
- return cs.HmsetCtx(context.Background(), key, fieldsAndValues)
- }
- func (cs clusterStore) HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error {
- node, err := cs.getRedis(key)
- if err != nil {
- return err
- }
- return node.HmsetCtx(ctx, key, fieldsAndValues)
- }
- func (cs clusterStore) Hvals(key string) ([]string, error) {
- return cs.HvalsCtx(context.Background(), key)
- }
- func (cs clusterStore) HvalsCtx(ctx context.Context, key string) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.HvalsCtx(ctx, key)
- }
- func (cs clusterStore) Incr(key string) (int64, error) {
- return cs.IncrCtx(context.Background(), key)
- }
- func (cs clusterStore) IncrCtx(ctx context.Context, key string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.IncrCtx(ctx, key)
- }
- func (cs clusterStore) Incrby(key string, increment int64) (int64, error) {
- return cs.IncrbyCtx(context.Background(), key, increment)
- }
- func (cs clusterStore) IncrbyCtx(ctx context.Context, key string, increment int64) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.IncrbyCtx(ctx, key, increment)
- }
- func (cs clusterStore) Llen(key string) (int, error) {
- return cs.LlenCtx(context.Background(), key)
- }
- func (cs clusterStore) LlenCtx(ctx context.Context, key string) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.LlenCtx(ctx, key)
- }
- func (cs clusterStore) Lindex(key string, index int64) (string, error) {
- return cs.LindexCtx(context.Background(), key, index)
- }
- func (cs clusterStore) LindexCtx(ctx context.Context, key string, index int64) (string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return "", err
- }
- return node.LindexCtx(ctx, key, index)
- }
- func (cs clusterStore) Lpop(key string) (string, error) {
- return cs.LpopCtx(context.Background(), key)
- }
- func (cs clusterStore) LpopCtx(ctx context.Context, key string) (string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return "", err
- }
- return node.LpopCtx(ctx, key)
- }
- func (cs clusterStore) Lpush(key string, values ...any) (int, error) {
- return cs.LpushCtx(context.Background(), key, values...)
- }
- func (cs clusterStore) LpushCtx(ctx context.Context, key string, values ...any) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.LpushCtx(ctx, key, values...)
- }
- func (cs clusterStore) Lrange(key string, start, stop int) ([]string, error) {
- return cs.LrangeCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) LrangeCtx(ctx context.Context, key string, start, stop int) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.LrangeCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) Lrem(key string, count int, value string) (int, error) {
- return cs.LremCtx(context.Background(), key, count, value)
- }
- func (cs clusterStore) LremCtx(ctx context.Context, key string, count int, value string) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.LremCtx(ctx, key, count, value)
- }
- func (cs clusterStore) Persist(key string) (bool, error) {
- return cs.PersistCtx(context.Background(), key)
- }
- func (cs clusterStore) PersistCtx(ctx context.Context, key string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.PersistCtx(ctx, key)
- }
- func (cs clusterStore) Pfadd(key string, values ...any) (bool, error) {
- return cs.PfaddCtx(context.Background(), key, values...)
- }
- func (cs clusterStore) PfaddCtx(ctx context.Context, key string, values ...any) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.PfaddCtx(ctx, key, values...)
- }
- func (cs clusterStore) Pfcount(key string) (int64, error) {
- return cs.PfcountCtx(context.Background(), key)
- }
- func (cs clusterStore) PfcountCtx(ctx context.Context, key string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.PfcountCtx(ctx, key)
- }
- func (cs clusterStore) Rpush(key string, values ...any) (int, error) {
- return cs.RpushCtx(context.Background(), key, values...)
- }
- func (cs clusterStore) RpushCtx(ctx context.Context, key string, values ...any) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.RpushCtx(ctx, key, values...)
- }
- func (cs clusterStore) Sadd(key string, values ...any) (int, error) {
- return cs.SaddCtx(context.Background(), key, values...)
- }
- func (cs clusterStore) SaddCtx(ctx context.Context, key string, values ...any) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.SaddCtx(ctx, key, values...)
- }
- func (cs clusterStore) Scard(key string) (int64, error) {
- return cs.ScardCtx(context.Background(), key)
- }
- func (cs clusterStore) ScardCtx(ctx context.Context, key string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ScardCtx(ctx, key)
- }
- func (cs clusterStore) Set(key, value string) error {
- return cs.SetCtx(context.Background(), key, value)
- }
- func (cs clusterStore) SetCtx(ctx context.Context, key, value string) error {
- node, err := cs.getRedis(key)
- if err != nil {
- return err
- }
- return node.SetCtx(ctx, key, value)
- }
- func (cs clusterStore) Setex(key, value string, seconds int) error {
- return cs.SetexCtx(context.Background(), key, value, seconds)
- }
- func (cs clusterStore) SetexCtx(ctx context.Context, key, value string, seconds int) error {
- node, err := cs.getRedis(key)
- if err != nil {
- return err
- }
- return node.SetexCtx(ctx, key, value, seconds)
- }
- func (cs clusterStore) Setnx(key, value string) (bool, error) {
- return cs.SetnxCtx(context.Background(), key, value)
- }
- func (cs clusterStore) SetnxCtx(ctx context.Context, key, value string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.SetnxCtx(ctx, key, value)
- }
- func (cs clusterStore) SetnxEx(key, value string, seconds int) (bool, error) {
- return cs.SetnxExCtx(context.Background(), key, value, seconds)
- }
- func (cs clusterStore) SetnxExCtx(ctx context.Context, key, value string, seconds int) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.SetnxExCtx(ctx, key, value, seconds)
- }
- func (cs clusterStore) GetSet(key, value string) (string, error) {
- return cs.GetSetCtx(context.Background(), key, value)
- }
- func (cs clusterStore) GetSetCtx(ctx context.Context, key, value string) (string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return "", err
- }
- return node.GetSetCtx(ctx, key, value)
- }
- func (cs clusterStore) Sismember(key string, value any) (bool, error) {
- return cs.SismemberCtx(context.Background(), key, value)
- }
- func (cs clusterStore) SismemberCtx(ctx context.Context, key string, value any) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.SismemberCtx(ctx, key, value)
- }
- func (cs clusterStore) Smembers(key string) ([]string, error) {
- return cs.SmembersCtx(context.Background(), key)
- }
- func (cs clusterStore) SmembersCtx(ctx context.Context, key string) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.SmembersCtx(ctx, key)
- }
- func (cs clusterStore) Spop(key string) (string, error) {
- return cs.SpopCtx(context.Background(), key)
- }
- func (cs clusterStore) SpopCtx(ctx context.Context, key string) (string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return "", err
- }
- return node.SpopCtx(ctx, key)
- }
- func (cs clusterStore) Srandmember(key string, count int) ([]string, error) {
- return cs.SrandmemberCtx(context.Background(), key, count)
- }
- func (cs clusterStore) SrandmemberCtx(ctx context.Context, key string, count int) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.SrandmemberCtx(ctx, key, count)
- }
- func (cs clusterStore) Srem(key string, values ...any) (int, error) {
- return cs.SremCtx(context.Background(), key, values...)
- }
- func (cs clusterStore) SremCtx(ctx context.Context, key string, values ...any) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.SremCtx(ctx, key, values...)
- }
- func (cs clusterStore) Sscan(key string, cursor uint64, match string, count int64) (
- keys []string, cur uint64, err error) {
- return cs.SscanCtx(context.Background(), key, cursor, match, count)
- }
- func (cs clusterStore) SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
- keys []string, cur uint64, err error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, 0, err
- }
- return node.SscanCtx(ctx, key, cursor, match, count)
- }
- func (cs clusterStore) Ttl(key string) (int, error) {
- return cs.TtlCtx(context.Background(), key)
- }
- func (cs clusterStore) TtlCtx(ctx context.Context, key string) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.TtlCtx(ctx, key)
- }
- func (cs clusterStore) Zadd(key string, score int64, value string) (bool, error) {
- return cs.ZaddCtx(context.Background(), key, score, value)
- }
- func (cs clusterStore) ZaddFloat(key string, score float64, value string) (bool, error) {
- return cs.ZaddFloatCtx(context.Background(), key, score, value)
- }
- func (cs clusterStore) ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error) {
- return cs.ZaddFloatCtx(ctx, key, float64(score), value)
- }
- func (cs clusterStore) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (bool, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return false, err
- }
- return node.ZaddFloatCtx(ctx, key, score, value)
- }
- func (cs clusterStore) Zadds(key string, ps ...redis.Pair) (int64, error) {
- return cs.ZaddsCtx(context.Background(), key, ps...)
- }
- func (cs clusterStore) ZaddsCtx(ctx context.Context, key string, ps ...redis.Pair) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZaddsCtx(ctx, key, ps...)
- }
- func (cs clusterStore) Zcard(key string) (int, error) {
- return cs.ZcardCtx(context.Background(), key)
- }
- func (cs clusterStore) ZcardCtx(ctx context.Context, key string) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZcardCtx(ctx, key)
- }
- func (cs clusterStore) Zcount(key string, start, stop int64) (int, error) {
- return cs.ZcountCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZcountCtx(ctx context.Context, key string, start, stop int64) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZcountCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) Zincrby(key string, increment int64, field string) (int64, error) {
- return cs.ZincrbyCtx(context.Background(), key, increment, field)
- }
- func (cs clusterStore) ZincrbyCtx(ctx context.Context, key string, increment int64, field string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZincrbyCtx(ctx, key, increment, field)
- }
- func (cs clusterStore) Zrank(key, field string) (int64, error) {
- return cs.ZrankCtx(context.Background(), key, field)
- }
- func (cs clusterStore) ZrankCtx(ctx context.Context, key, field string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZrankCtx(ctx, key, field)
- }
- func (cs clusterStore) Zrange(key string, start, stop int64) ([]string, error) {
- return cs.ZrangeCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrangeCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) ZrangeWithScores(key string, start, stop int64) ([]redis.Pair, error) {
- return cs.ZrangeWithScoresCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrangeWithScoresCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) ZrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error) {
- return cs.ZrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrangebyscoreWithScoresCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
- []redis.Pair, error) {
- return cs.ZrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
- }
- func (cs clusterStore) ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) (
- []redis.Pair, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrangebyscoreWithScoresAndLimitCtx(ctx, key, start, stop, page, size)
- }
- func (cs clusterStore) Zrem(key string, values ...any) (int, error) {
- return cs.ZremCtx(context.Background(), key, values...)
- }
- func (cs clusterStore) ZremCtx(ctx context.Context, key string, values ...any) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZremCtx(ctx, key, values...)
- }
- func (cs clusterStore) Zremrangebyrank(key string, start, stop int64) (int, error) {
- return cs.ZremrangebyrankCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZremrangebyrankCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) Zremrangebyscore(key string, start, stop int64) (int, error) {
- return cs.ZremrangebyscoreCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) (int, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZremrangebyscoreCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) Zrevrange(key string, start, stop int64) ([]string, error) {
- return cs.ZrevrangeCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZrevrangeCtx(ctx context.Context, key string, start, stop int64) ([]string, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrevrangeCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) ZrevrangebyscoreWithScores(key string, start, stop int64) ([]redis.Pair, error) {
- return cs.ZrevrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
- }
- func (cs clusterStore) ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) ([]redis.Pair, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrevrangebyscoreWithScoresCtx(ctx, key, start, stop)
- }
- func (cs clusterStore) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64, page, size int) (
- []redis.Pair, error) {
- return cs.ZrevrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
- }
- func (cs clusterStore) ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start, stop int64, page, size int) (
- []redis.Pair, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return nil, err
- }
- return node.ZrevrangebyscoreWithScoresAndLimitCtx(ctx, key, start, stop, page, size)
- }
- func (cs clusterStore) Zrevrank(key, field string) (int64, error) {
- return cs.ZrevrankCtx(context.Background(), key, field)
- }
- func (cs clusterStore) ZrevrankCtx(ctx context.Context, key, field string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZrevrankCtx(ctx, key, field)
- }
- func (cs clusterStore) Zscore(key, value string) (int64, error) {
- return cs.ZscoreCtx(context.Background(), key, value)
- }
- func (cs clusterStore) ZscoreCtx(ctx context.Context, key, value string) (int64, error) {
- node, err := cs.getRedis(key)
- if err != nil {
- return 0, err
- }
- return node.ZscoreCtx(ctx, key, value)
- }
- func (cs clusterStore) getRedis(key string) (*redis.Redis, error) {
- val, ok := cs.dispatcher.Get(key)
- if !ok {
- return nil, ErrNoRedisNode
- }
- return val.(*redis.Redis), nil
- }
|