You've already forked email-api-client
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 {
|
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{}
|
token := Token{}
|
||||||
|
|
||||||
@@ -81,17 +86,18 @@ func FromCache() *Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func saveCache(token *Token) {
|
func saveCache(token *Token) {
|
||||||
rdb := redis.NewRedis()
|
rdb, err := redis.NewRedis()
|
||||||
|
|
||||||
tokenJson, _ := json.Marshal(token)
|
// no cache available
|
||||||
|
if rdb == nil || err != nil {
|
||||||
expiresIn := time.Duration(int64(token.ExpiresIn) * 100000000)
|
|
||||||
|
|
||||||
cmd := rdb.Set("api.access_token", tokenJson, expiresIn)
|
|
||||||
result, err := cmd.Result()
|
|
||||||
if err != nil {
|
|
||||||
return
|
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
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"gitea.siteworxpro.com/golang-packages/utilities/Env"
|
"gitea.siteworxpro.com/golang-packages/utilities/Env"
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -12,20 +14,20 @@ const (
|
|||||||
redisDb Env.EnvironmentVariable = "REDIS_DB"
|
redisDb Env.EnvironmentVariable = "REDIS_DB"
|
||||||
)
|
)
|
||||||
|
|
||||||
var r = struct {
|
type contextKey string
|
||||||
client *redis.Client
|
|
||||||
initialized bool
|
|
||||||
}{}
|
|
||||||
|
|
||||||
func NewRedis() *redis.Client {
|
const redisContextKey contextKey = "redisClient"
|
||||||
if r.initialized {
|
|
||||||
return r.client
|
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)
|
db, err := strconv.ParseInt(redisDb.GetEnvString("0"), 10, 64)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, errors.New("invalid REDIS_DB value: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
rdb := redis.NewClient(&redis.Options{
|
rdb := redis.NewClient(&redis.Options{
|
||||||
@@ -34,7 +36,38 @@ func NewRedis() *redis.Client {
|
|||||||
DB: int(db),
|
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