refactor: enhance Redis client initialization and context management
All checks were successful
🚀 Publish Release Package / publish (push) Successful in 21s
All checks were successful
🚀 Publish Release Package / publish (push) Successful in 21s
This commit is contained in:
@@ -63,7 +63,12 @@ func getToken(configuration *Configuration) (*Token, error) {
|
||||
}
|
||||
|
||||
func FromCache() *Token {
|
||||
rdb := redis.NewRedis()
|
||||
rdb, err := redis.NewRedis()
|
||||
|
||||
// Check if Redis connection was successful
|
||||
if rdb == nil || err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
token := Token{}
|
||||
|
||||
@@ -81,17 +86,18 @@ func FromCache() *Token {
|
||||
}
|
||||
|
||||
func saveCache(token *Token) {
|
||||
rdb := redis.NewRedis()
|
||||
rdb, err := redis.NewRedis()
|
||||
|
||||
tokenJson, _ := json.Marshal(token)
|
||||
|
||||
expiresIn := time.Duration(int64(token.ExpiresIn) * 100000000)
|
||||
|
||||
cmd := rdb.Set("api.access_token", tokenJson, expiresIn)
|
||||
result, err := cmd.Result()
|
||||
if err != nil {
|
||||
// no cache available
|
||||
if rdb == nil || err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
println(result)
|
||||
tokenJson, _ := json.Marshal(token)
|
||||
|
||||
const nanosPerSecond = int64(time.Second)
|
||||
expiresIn := time.Duration(int64(token.ExpiresIn) * nanosPerSecond)
|
||||
|
||||
cmd := rdb.Set("api.access_token", tokenJson, expiresIn)
|
||||
_, _ = cmd.Result()
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"gitea.siteworxpro.com/golang-packages/utilities/Env"
|
||||
"github.com/go-redis/redis"
|
||||
"strconv"
|
||||
@@ -12,20 +14,20 @@ const (
|
||||
redisDb Env.EnvironmentVariable = "REDIS_DB"
|
||||
)
|
||||
|
||||
var r = struct {
|
||||
client *redis.Client
|
||||
initialized bool
|
||||
}{}
|
||||
type contextKey string
|
||||
|
||||
func NewRedis() *redis.Client {
|
||||
if r.initialized {
|
||||
return r.client
|
||||
const redisContextKey contextKey = "redisClient"
|
||||
|
||||
func NewRedis() (*redis.Client, error) {
|
||||
|
||||
if redisHost.GetEnvString("") == "" {
|
||||
return nil, nil // Redis host not set, return nil
|
||||
}
|
||||
|
||||
db, err := strconv.ParseInt(redisDb.GetEnvString("0"), 10, 64)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, errors.New("invalid REDIS_DB value: " + err.Error())
|
||||
}
|
||||
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
@@ -34,7 +36,38 @@ func NewRedis() *redis.Client {
|
||||
DB: int(db),
|
||||
})
|
||||
|
||||
r.client = rdb
|
||||
cmd := rdb.Ping() // Ensure the connection is established
|
||||
|
||||
return r.client
|
||||
if cmd.Err() != nil {
|
||||
return nil, errors.New("failed to connect to Redis: " + cmd.Err().Error())
|
||||
}
|
||||
|
||||
return rdb, nil
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) (*redis.Client, error) {
|
||||
client := ctx.Value(redisContextKey)
|
||||
if client == nil {
|
||||
return nil, errors.New("no Redis client found in context")
|
||||
}
|
||||
|
||||
if rdb, ok := client.(*redis.Client); ok {
|
||||
return rdb, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("invalid Redis client type in context")
|
||||
}
|
||||
|
||||
func NewWithContext(ctx context.Context) (context.Context, error) {
|
||||
rdb, err := NewRedis()
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to create Redis client: " + err.Error())
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, redisContextKey, rdb)
|
||||
if c, ok := ctx.Value(redisContextKey).(*redis.Client); ok {
|
||||
return context.WithValue(ctx, redisContextKey, c), nil
|
||||
} else {
|
||||
return nil, errors.New("failed to set Redis client in context")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user