diff --git a/redis/redis.go b/redis/redis.go index b5c392bbfe2694382aeaf0ac5bf6e2c0fa61806b..86958ed2e67e6e3d5b41a0e9b71add25ae2a88d2 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -177,6 +177,27 @@ func (r ClientWithHelpers) SetObjectByKeyIndefinitely(key string, object interfa } +func GetObjectByKey[T any](redisClient *ClientWithHelpers, key string, object T) *T { + // Make sure we have a Redis client, and it is connected + if redisClient == nil || !redisClient.IsConnected() { + return nil + } + + // Get the object from Redis + jsonString := redisClient.GetValueByKey(key) + if jsonString == "" { + return nil + } + + // Now unmarshal + err := json.Unmarshal([]byte(jsonString), &object) + if err != nil { + return nil + } + + return &object +} + func (r ClientWithHelpers) GetValueByKey(key string) string { if !r.IsConnected() { return ""