generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cachestore.go
189 lines (153 loc) · 4.57 KB
/
cachestore.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Package cachestore is the caching (key->value) service abstraction layer
*/
package cachestore
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
"github.com/coocood/freecache"
"github.com/gomodule/redigo/redis"
"github.com/mrz1836/go-cache"
)
// Set will set a key->value using the current engine
//
// NOTE: redis only supports dependency keys at this time
// Value should be used as a string for best results
func (c *Client) Set(ctx context.Context, key string, value interface{}, dependencies ...string) error {
// Sanitize the key (trailing or leading spaces)
key = strings.TrimSpace(key)
// Require a key to be present
if len(key) == 0 {
return ErrKeyRequired
}
// Redis
if c.Engine() == Redis {
return cache.Set(ctx, c.options.redis, key, value, dependencies...)
}
// FreeCache
return c.options.freeCache.Set([]byte(key), []byte(value.(string)), 0)
}
// SetTTL will set a key->value using the current engine with a TTL
//
// NOTE: redis only supports dependency keys at this time
// Value should be used as a string for best results
func (c *Client) SetTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, dependencies ...string) error {
// Sanitize the key (trailing or leading spaces)
key = strings.TrimSpace(key)
// Require a key to be present
if len(key) == 0 {
return ErrKeyRequired
}
// Redis
if c.Engine() == Redis {
return cache.SetExp(ctx, c.options.redis, key, value, ttl, dependencies...)
}
// FreeCache
return c.options.freeCache.Set([]byte(key), []byte(value.(string)), int(ttl.Seconds()))
}
// Get will return a value from a given key
//
// Redis will be an interface{} but really a string (empty string)
func (c *Client) Get(ctx context.Context, key string) (string, error) {
// Sanitize the key (trailing or leading spaces)
key = strings.TrimSpace(key)
// Require a key to be present
if len(key) == 0 {
return "", ErrKeyRequired
}
// Switch on the engine
if c.Engine() == Redis {
str, err := cache.Get(ctx, c.options.redis, key)
if err != nil && errors.Is(err, redis.ErrNil) {
return "", nil
} else if err != nil {
return "", err
}
return str, nil
}
// Check using FreeCache
data, err := c.options.freeCache.Get([]byte(key))
if err != nil && errors.Is(err, freecache.ErrNotFound) { // Ignore this error
return "", nil
} else if err != nil { // Real error getting the cache value
return "", err
}
return string(data), nil
}
// Delete will remove a key from the cache
func (c *Client) Delete(ctx context.Context, key string) error {
// Sanitize the key (trailing or leading spaces)
key = strings.TrimSpace(key)
// Require a key to be present
if len(key) == 0 {
return ErrKeyRequired
}
// Switch on the engine
if c.Engine() == Redis {
_, err := cache.DeleteWithoutDependency(ctx, c.options.redis, key)
return err
}
// Use FreeCache
_ = c.options.freeCache.Del([]byte(key))
return nil
}
// SetModel will set any model or struct (parsing Model->JSON (bytes))
//
// Model needs to be a pointer to a struct
// NOTE: redis only supports dependency keys at this time
func (c *Client) SetModel(ctx context.Context, key string, model interface{},
ttl time.Duration, dependencies ...string) error {
// Sanitize the key (trailing or leading spaces)
key = strings.TrimSpace(key)
// Require a key to be present
if len(key) == 0 {
return ErrKeyRequired
}
// Redis
if c.Engine() == Redis {
return cache.SetToJSON(ctx, c.options.redis, key, model, ttl, dependencies...)
}
// Parse into JSON
responseBytes, err := json.Marshal(&model)
if err != nil {
return err
}
// FreeCache (store the bytes)
return c.options.freeCache.Set([]byte(key), responseBytes, int(ttl.Seconds()))
}
// GetModel will get a model (parsing JSON (bytes) -> Model)
//
// Model needs to be a pointer to a struct
func (c *Client) GetModel(ctx context.Context, key string, model interface{}) error {
// Sanitize the key (trailing or leading spaces)
key = strings.TrimSpace(key)
// Require a key to be present
if len(key) == 0 {
return ErrKeyRequired
}
// Redis
if c.Engine() == Redis {
// Get the record as bytes
b, err := cache.GetBytes(ctx, c.options.redis, key)
if err != nil {
if errors.Is(err, redis.ErrNil) {
return ErrKeyNotFound
}
return err
}
// Sanity check to make sure there is a value to unmarshal
if len(b) == 0 {
return ErrKeyNotFound
}
return json.Unmarshal(b, &model)
} else if c.Engine() == FreeCache {
if b, err := c.options.freeCache.Get([]byte(key)); err == nil && len(b) > 0 {
return json.Unmarshal(b, &model)
}
}
// Not found
return ErrKeyNotFound
}