Skip to content

Commit

Permalink
Fix WriteKeyValAsJSONTTL
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Sep 27, 2023
1 parent 80596a9 commit f995d9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
22 changes: 19 additions & 3 deletions examples/redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,37 @@ func main() {
v1, err := cache.GetVal("key1")
if err != nil {
fmt.Println("Error:", err)
return
}

v2, err := cache.GetVal("key2")
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("v1:", v1)
fmt.Println("v2:", v2)

v3, err := cache.GetVal("key3")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("v3:", v3)

type data struct {
Name string
}

d := data{
Name: "Thiago",
}
if err := cache.WriteKeyValAsJSONTTL("key4", d, 2000); err != nil {
fmt.Println("Error:", err)
}

v, err := cache.GetVal("key4")
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("v4:", v)

}
18 changes: 9 additions & 9 deletions v1/cache/drivers/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ func (d *RedisLayer) WriteKeyVal(key string, val string) error {
return d.rdb.Set(context.Background(), key, val, time.Duration(0)).Err()
}

func (d *RedisLayer) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
if ttlSeconds == 0 {
func (d *RedisLayer) WriteKeyValTTL(key string, val string, ttlInSeconds int) error {
if ttlInSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValTTL")
ttlSeconds = d.ttl
ttlInSeconds = d.ttl
}
return d.rdb.Set(context.Background(), key, val, time.Duration(ttlSeconds)).Err()
return d.rdb.Set(context.Background(), key, val, time.Duration(ttlInSeconds)*time.Second).Err()
}

func (d *RedisLayer) WriteKeyValAsJSON(key string, val interface{}) error {
func (d *RedisLayer) WriteKeyValAsJSON(key string, val any) error {
valueAsJSON, err := json.Marshal(val)
if err != nil {
d.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSON")
Expand All @@ -144,18 +144,18 @@ func (d *RedisLayer) WriteKeyValAsJSON(key string, val interface{}) error {
return d.WriteKeyVal(key, string(valueAsJSON))
}

func (d *RedisLayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
if ttlSeconds == 0 {
func (d *RedisLayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlInSeconds int) error {
if ttlInSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValAsJSONTTL")
ttlSeconds = d.ttl
ttlInSeconds = d.ttl
}
valueAsJSON, err := json.Marshal(val)
if err != nil {
d.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSONTTL")
return err
}

return d.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
return d.WriteKeyValTTL(key, string(valueAsJSON), ttlInSeconds)
}

func (d *RedisLayer) GetDriver() kind.Driver {
Expand Down

0 comments on commit f995d9e

Please sign in to comment.