-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
79 lines (66 loc) 路 1.77 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package app
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"time"
)
const (
CacheTTL = time.Hour * 24
)
var l = logrus.WithField("source", "app")
type cacheRepository interface {
SetKey(string, string, time.Duration) error
GetKey(string) (string, error)
}
type CacheService struct {
cache cacheRepository
}
func NewCacheService(cache cacheRepository) CacheService {
if cache == nil {
panic("missing cacheRepository")
}
return CacheService{
cache: cache,
}
}
// Set is the same as SetKey but before that it marshals value. Simple helper
func (c *CacheService) Set(key string, value interface{}) error {
marshaled, err := json.Marshal(value)
if err != nil {
l.WithError(err).WithField("value", value).WithField("key", key).Errorf("can't marshal")
return err
}
err = c.cache.SetKey(key, string(marshaled), CacheTTL)
if err != nil {
l.WithError(err).Error("can't set marshaled value")
}
return nil
}
// Get looks for object via key in cache
func (c *CacheService) Get(key string, to interface{}) error {
entity, err := c.cache.GetKey(key)
if err != nil {
return errors.Wrap(err, "can't get cache for CacheService")
}
if to != nil {
err = json.Unmarshal([]byte(entity), to)
}
if err != nil {
return errors.Wrapf(err, "can't unmarshal value for key %s", key)
}
return nil
}
// MarshalAndSetKey is the same as SetKey but before that it marshals value. Simple helper
func (c *CacheService) MarshalAndSet(key string, value interface{}) error {
marshaled, err := json.Marshal(value)
if err != nil {
l.WithError(err).WithField("value", value).WithField("key", key).Errorf("can't marshal")
return err
}
err = c.Set(key, string(marshaled))
if err != nil {
l.WithError(err).Error("can't set marshaled value")
}
return nil
}