package data import ( "bytes" "context" "encoding/json" "fmt" "time" "github.com/redis/go-redis/v9" ) type RedisCli struct { Client *redis.Client } func NewRedisClient(address, password string) *RedisCli { // option, err := redis.ParseURL("redis://" + username + ":" + password + "@localhost:6379/" + db) options := redis.Options{ Addr: address, Password: password, DB: 0, } client := redis.NewClient(&options) if err := client.Ping(context.Background()); err.Err() != nil { fmt.Println("trying localhost ...", err) client = redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: password, DB: 0, }) if err_local := client.Ping(context.Background()); err_local.Err() != nil { fmt.Println("> result ====> ", err_local) // do as warning } else { fmt.Println("\n> Localhost Redis OK!\n") } } else { fmt.Println("\n> Redis OK! \n") } return &RedisCli{ Client: client, } } func (r *RedisCli) HealthCheck() error { return r.Client.Ping(context.Background()).Err() } func (r *RedisCli) GetKeyTo(source string, dest interface{}) error { fmt.Println("Redis> GET ", source) // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> GET error ", err) return err } saved, err := r.Client.Get(context.Background(), source).Result() // chcek EOF // fmt.Println("GET last char ", saved[len(saved)-1:]) if saved == "" || err != nil { fmt.Println("GET error (empty on error)|", err, "|while saved=", saved) return err } // fmt.Println("GET ", saved) // if err != nil { // fmt.Println("GET error ", err) // return err // } err = json.NewDecoder(bytes.NewBufferString(saved)).Decode(dest) if err != nil { fmt.Println("GET error ", err) } return err } func (r *RedisCli) SetToKey(key string, value interface{}) error { fmt.Println("Redis> SET ", key) // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> SET error ", err) return err } saved, err := json.Marshal(value) if err != nil { fmt.Println("SET error ", err) return err } // late error err = r.Client.Set(context.Background(), key, saved, redis.KeepTTL).Err() if err != nil { fmt.Println("error on SET ", err) } return err } func (r *RedisCli) ExpireKey(key string) error { // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> EXPIRE error ", err) return err } return r.Client.Expire(context.Background(), key, 0).Err() } func (r *RedisCli) SetKeyTimeout(key string, value interface{}, timeout int) error { // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> EXPIRE error ", err) return err } err := r.Client.Expire(context.Background(), key, time.Duration(timeout)*time.Second).Err() fmt.Println("error on EXPIRE ", err) return err } func (r *RedisCli) KeyList() ([]string, error) { // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> KEYS error ", err) return nil, err } keys := r.Client.Keys(context.Background(), "*") return keys.Result() } // list operations func (r *RedisCli) GetList(key string) ([]string, error) { // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> List.GET error ", err) return nil, err } return r.Client.LRange(context.Background(), key, 0, -1).Result() } func (r *RedisCli) Add(key string, value interface{}) error { // if cannot pass healthcheck, return err if err := r.HealthCheck(); err != nil { fmt.Println("HS> List.ADD error ", err) return err } err := r.Client.RPush(context.Background(), key, value) return err.Err() }