Add redis. Prep for new save file
This commit is contained in:
parent
6c22be7d7c
commit
0604a3b77f
5 changed files with 295 additions and 87 deletions
137
server/data/redis.go
Normal file
137
server/data/redis.go
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
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 != 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 != 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue