123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- 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
- }
|