1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| package redis
import ( "context" "github.com/go-redis/redis/v8" "go-api-practice/pkg/logger" "sync" "time" )
type RedisClient struct { Client *redis.Client Context context.Context }
var once sync.Once
var Redis *RedisClient
func ConnectRedis(address string, username string, password string, db int) { once.Do(func() { Redis = NewClient(address, username, password, db) }) }
func NewClient(address string, username string, password string, db int) *RedisClient { rds := &RedisClient{} rds.Context = context.Background() rds.Client = redis.NewClient(&redis.Options{ Addr: address, Username: username, Password: password, DB: db, }) err := rds.Ping() logger.LogIf(err)
return rds }
func (rds RedisClient) Ping() error { _, err := rds.Client.Ping(rds.Context).Result() return err }
func (rds RedisClient) Set(key string, value interface{}, expiration time.Duration) bool { if err := rds.Client.Set(rds.Context, key, value, expiration).Err(); err != nil { logger.LogIf(err) return false } return true }
func (rds RedisClient) Get(key string) string { result, err := rds.Client.Get(rds.Context, key).Result() if err != nil { if err != redis.Nil { logger.ErrorString("Redis", "Get", err.Error()) } return "" } return result }
func (rds RedisClient) Del(key ...string) bool { if err := rds.Client.Del(rds.Context, key...).Err(); err != nil { logger.ErrorString("Redis", "Del", err.Error()) return false } return true }
func (rds RedisClient) Has(key string) bool { _, err := rds.Client.Get(rds.Context, key).Result() if err != nil { if err != redis.Nil { logger.ErrorString("Redis", "Has", err.Error()) } return false } return true }
func (rds RedisClient) FlushDB() bool { if err := rds.Client.FlushDB(rds.Context).Err(); err != nil { logger.ErrorString("Redis", "FlushDB", err.Error()) return false } return true }
func (rds RedisClient) Increment(parameters ...interface{}) bool { switch len(parameters) { case 1: key := parameters[0].(string) if err := rds.Client.Incr(rds.Context, key).Err(); err != nil { logger.ErrorString("Redis", "Increment", err.Error()) return false } case 2: key := parameters[0].(string) value := parameters[1].(int64) if err := rds.Client.IncrBy(rds.Context, key, value).Err(); err != nil { logger.ErrorString("Redis", "Increment", err.Error()) return false } default: logger.ErrorString("Redis", "Increment", "parameters error") return false } return true }
func (rds RedisClient) Decrement(parameters ...interface{}) bool { switch len(parameters) { case 1: key := parameters[0].(string) if err := rds.Client.Decr(rds.Context, key).Err(); err != nil { logger.ErrorString("Redis", "Decrement", err.Error()) return false } case 2: key := parameters[0].(string) value := parameters[1].(int64) if err := rds.Client.DecrBy(rds.Context, key, value).Err(); err != nil { logger.ErrorString("Redis", "Decrement", err.Error()) return false } default: logger.ErrorString("Redis", "Decrement", "parameters error") return false } return true }
|