Skip to content

Commit

Permalink
refactor: update redis logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhstr committed Dec 6, 2019
1 parent c8e4f39 commit b2c7ce5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 42 deletions.
43 changes: 6 additions & 37 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,12 @@ func Del(keys ...string) error {
CacherInit(nil)
log := log.With().Str("operation", "DEL").Logger()

var err error

if client == nil {
err = noClientLogErr(log)
err := noClientLogErr(log)
return err
}

err = client.Del(keys...)
if err != nil {
log.Info().
Err(err).
Msg(err.Error())
}

return err
return client.Del(keys...)
}

// Get returns the data stored under the given key.
Expand All @@ -50,24 +41,12 @@ func Get(key string) ([]byte, error) {
CacherInit(nil)
log := log.With().Str("operation", "GET").Logger()

var (
data []byte
err error
)

if client == nil {
err = noClientLogErr(log)
err := noClientLogErr(log)
return []byte{}, err
}

data, err = client.Get(key)
if err != nil {
log.Info().
Str("key", key).
Msg("Cache key not found")
}

return data, err
return client.Get(key)
}

// Set stores data for a set period of time at the given key.
Expand All @@ -76,22 +55,12 @@ func Set(key string, data []byte, expiration time.Duration) error {
CacherInit(nil)
log := log.With().Str("operation", "SET").Logger()

var err error

if client == nil {
err = noClientLogErr(log)
return err
}

err = client.Set(key, data, expiration)
if err != nil {
log.Info().
Err(err).
Msg(err.Error())
err := noClientLogErr(log)
return err
}

return nil
return client.Set(key, data, expiration)
}

var cacherInit sync.Once
Expand Down
22 changes: 17 additions & 5 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package redis

import (
"io"
"net"
"strings"
"time"

"github.com/go-redis/redis"
Expand All @@ -28,6 +28,12 @@ type Cacher struct {
// Del deletes keys.
func (c *Cacher) Del(keys ...string) error {
_, err := c.client.Del(keys...).Result()
if err != nil {
log.Err(err).
Str("keys", strings.Join(keys, ",")).
Str("command", "DEL").
Msg("Redis command failed")
}
return err
}

Expand All @@ -39,11 +45,11 @@ func (c *Cacher) Get(key string) ([]byte, error) {
log.Debug().
Str("key", key).
Msg("Key not found")
} else if err == io.EOF {
log.Error().
} else {
log.Err(err).
Str("key", key).
Err(err).
Msg("Redis unavailable")
Str("command", "GET").
Msg("Redis command failed")
}
}
return data, err
Expand All @@ -52,6 +58,12 @@ func (c *Cacher) Get(key string) ([]byte, error) {
// Set stores data under a key for a set amount of time.
func (c *Cacher) Set(key string, val interface{}, t time.Duration) error {
_, err := c.client.Set(key, val, t).Result()
if err != nil {
log.Err(err).
Str("key", key).
Str("command", "SET").
Msg("Redis command failed")
}
return err
}

Expand Down

0 comments on commit b2c7ce5

Please sign in to comment.