package cache import ( "encoding/json" "github.com/go-redis/redis" "time" ) const IsExists = 1 func RedisTx(fn func(pipeline redis.Pipeliner) error) (err error) { if _isCluster { _, err = rcdb.TxPipelined(fn) } _, err = rdb.TxPipelined(fn) return } func RedisGet(key string) (string, error) { if _isCluster { return rcdb.Get(key).Result() } return rdb.Get(key).Result() } func RedisGetWithAddExpiration(key string, expiration time.Duration) (result string, err error) { if _isCluster { result, err = rcdb.Get(key).Result() if err != nil { rcdb.Expire(key, expiration) } } else { result, err = rdb.Get(key).Result() if err != nil { rdb.Expire(key, expiration) } } return } func RedisSet(key string, value interface{}) error { if _isCluster { return rcdb.Set(key, value, 0).Err() } return rdb.Set(key, value, 0).Err() } func RedisSetWithExpiration(key string, value interface{}, expiration time.Duration) error { if _isCluster { return rcdb.Set(key, value, expiration).Err() } return rdb.Set(key, value, expiration).Err() } func RedisGetObj(key string, obj interface{}) error { valueStr, err := RedisGet(key) if err != nil { return err } return json.Unmarshal([]byte(valueStr), &obj) } func RedisGetObjWithAddExpiration(key string, obj interface{}, expiration time.Duration) error { valueStr, err := RedisGetWithAddExpiration(key, expiration) if err != nil { return err } return json.Unmarshal([]byte(valueStr), &obj) } func RedisSetObj(key string, obj interface{}) error { valueBytes, err := json.Marshal(obj) if err != nil { return err } return RedisSet(key, string(valueBytes)) } func RedisSetObjWithExpiration(key string, obj interface{}, expiration time.Duration) error { valueBytes, err := json.Marshal(obj) if err != nil { return err } return RedisSetWithExpiration(key, string(valueBytes), expiration) } func RedisLRange(key string, start int64, end int64) ([]string, error) { if _isCluster { return rcdb.LRange(key, start, end).Result() } return rdb.LRange(key, start, end).Result() } func RedisHMGet(key string, fields ...string) ([]interface{}, error) { if _isCluster { return rcdb.HMGet(key, fields...).Result() } return rdb.HMGet(key, fields...).Result() } func RedisHMSet(key string, fields map[string]interface{}) error { if _isCluster { return rcdb.HMSet(key, fields).Err() } return rdb.HMSet(key, fields).Err() } func RedisHSet(key string, field string, value interface{}) error { if _isCluster { return rcdb.HSet(key, field, value).Err() } return rdb.HSet(key, field, value).Err() } func RedisHIncrBy(key string, field string, incr int64) error { if _isCluster { return rcdb.HIncrBy(key, field, incr).Err() } return rdb.HIncrBy(key, field, incr).Err() } func RedisHGet(key string, field string) (string, error) { if _isCluster { return rcdb.HGet(key, field).Result() } return rdb.HGet(key, field).Result() } func RedisHKeys(key string) ([]string, error) { if _isCluster { return rcdb.HKeys(key).Result() } return rdb.HKeys(key).Result() } func RedisHGetAll(key string) (map[string]string, error) { if _isCluster { return rcdb.HGetAll(key).Result() } return rdb.HGetAll(key).Result() } func RedisDel(key string) error { if _isCluster { return rcdb.Del(key).Err() } return rdb.Del(key).Err() } func RedisExists(key string) (bool, error) { if _isCluster { result, err := rcdb.Do("EXISTS", key).Result() if err != nil { return false, err } return result == IsExists, nil } else { result, err := rdb.Do("EXISTS", key).Result() if err != nil { return false, err } return result == IsExists, nil } } func RedisExpireAt(key string, tm time.Time) error { if _isCluster { return rcdb.ExpireAt(key, tm).Err() } return rdb.ExpireAt(key, tm).Err() } func RedisLPush(key string, obj interface{}) error { if _isCluster { return rcdb.LPush(key, obj).Err() } return rdb.LPush(key, obj).Err() } func RedisLPushObj(key string, obj interface{}) error { valueBytes, err := json.Marshal(obj) if err != nil { return err } if _isCluster { return rcdb.LPush(key, string(valueBytes)).Err() } return rdb.LPush(key, string(valueBytes)).Err() } func RedisLPushObjList(key string, objList ...interface{}) error { strList := make([]string, 0) for _, obj := range objList { valueBytes, err := json.Marshal(obj) if err != nil { return err } strList = append(strList, string(valueBytes)) } if _isCluster { return rcdb.LPush(key, strList).Err() } return rdb.LPush(key, strList).Err() } func RedisLTrim(key string, start int64, end int64) error { if _isCluster { return rcdb.LTrim(key, start, end).Err() } return rdb.LTrim(key, start, end).Err() } func RedisZIncr(key string, score float64, value string) error { if _isCluster { err := rcdb.ZIncr(key, redis.Z{Score: score, Member: value}).Err() return err } err := rdb.ZIncr(key, redis.Z{Score: score, Member: value}).Err() return err } func RedisZRevRange(key string, start int64, stop int64) ([]redis.Z, error) { if _isCluster { z, err := rcdb.ZRevRangeWithScores(key, start, stop).Result() return z, err } z, err := rdb.ZRevRangeWithScores(key, start, stop).Result() return z, err } func RedisZScore(key string, value string) (float64, error) { if _isCluster { score, err := rcdb.ZScore(key, value).Result() return score, err } score, err := rdb.ZScore(key, value).Result() return score, err } func RedisZRevRank(key string, value string) (int64, error) { if _isCluster { rank, err := rcdb.ZRevRank(key, value).Result() return rank, err } rank, err := rdb.ZRevRank(key, value).Result() return rank, err }