Skip to content
Snippets Groups Projects
Commit 3533283f authored by Jano Hendriks's avatar Jano Hendriks
Browse files

Added function to set a redis object with specific expiration.

parent 58fffbd8
No related branches found
No related tags found
No related merge requests found
......@@ -91,6 +91,28 @@ func (r ClientWithHelpers) SetObjectByKey(key string, object interface{}) {
}
}
func (r ClientWithHelpers) SetObjectByKeyWithExpiry(key string, object interface{}, expiration time.Duration) {
if !r.IsConnected() {
return
}
jsonBytes, err := json.Marshal(object)
if err != nil {
logs.ErrorWithMsg("Error marshalling object to Redis: %s", err)
return
}
_, err = r.Client.Set(ctx, key, string(jsonBytes), expiration).Result()
if err != nil {
logs.ErrorWithMsg(fmt.Sprintf("Error setting value to Redis for key: %s", key), err)
/* Prevent further calls in this execution from trying to connect and also timeout */
if strings.HasSuffix(err.Error(), "i/o timeout") {
r.Available = false
}
}
}
func (r ClientWithHelpers) SetObjectByKeyIndefinitely(key string, object interface{}) {
if !r.IsConnected() {
return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment