github.com/lingyao2333/mo-zero@v1.4.1/core/stores/kv/store.go (about)

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