@@ -16,6 +16,7 @@ Here is what it brings in detail:
16
16
* ✅ A loadable cache: allow you to call a callback function to put your data back in cache
17
17
* ✅ A metric cache to let you store metrics about your caches usage (hits, miss, set success, set error, ...)
18
18
* ✅ A marshaler to automatically marshal/unmarshal your cache values as a struct
19
+ * ✅ Define default values in stores and override them when setting data
19
20
20
21
## Built-in stores
21
22
@@ -40,10 +41,15 @@ Here is a simple cache instanciation with Redis but you can also look at other a
40
41
``` go
41
42
memcacheStore := store.NewMemcache (
42
43
memcache.New (" 10.0.0.1:11211" , " 10.0.0.2:11211" , " 10.0.0.3:11212" ),
44
+ &store.Options {
45
+ Expiration : 10 *time.Second ,
46
+ },
43
47
)
44
48
45
- cacheManager := cache.New (memcacheStore, &cache.Options {Expiration: 15 *time.Second })
46
- err := cacheManager.Set (" my-key" , []byte (" my-value))
49
+ cacheManager := cache.New (memcacheStore)
50
+ err := cacheManager.Set (" my-key" , []byte (" my-value), &cache.Options{
51
+ Expiration: 15*time.Second, // Override default value of 10 seconds defined in the store
52
+ })
47
53
if err != nil {
48
54
panic(err)
49
55
}
@@ -55,12 +61,10 @@ value := cacheManager.Get("my-key")
55
61
56
62
```go
57
63
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Minute))
58
- bigcacheStore := store.NewBigcache(
59
- bigcacheClient,
60
- )
64
+ bigcacheStore := store.NewBigcache(bigcacheClient, nil) // No otions provided (as second argument)
61
65
62
- cacheManager := cache.New(bigcacheStore, nil )
63
- err := cacheManager.Set(" my-key" , " my-value" )
66
+ cacheManager := cache.New(bigcacheStore)
67
+ err := cacheManager.Set(" my-key" , " my-value" , nil )
64
68
if err != nil {
65
69
panic(err)
66
70
}
@@ -71,14 +75,18 @@ value := cacheManager.Get("my-key")
71
75
#### Memory (using Ristretto)
72
76
73
77
```go
74
- ristrettoCache, err := ristretto.NewCache(&ristretto.Config{NumCounters: 1000, MaxCost: 100, BufferItems: 64})
78
+ ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
79
+ NumCounters: 1000,
80
+ MaxCost: 100,
81
+ BufferItems: 64,
82
+ })
75
83
if err != nil {
76
84
panic(err)
77
85
}
78
- ristrettoStore := store.NewRistretto(ristrettoCache)
86
+ ristrettoStore := store.NewRistretto(ristrettoCache, nil )
79
87
80
- cacheManager := cache.New(ristrettoStore, nil )
81
- err := cacheManager.Set(" my-key" , " my-value" )
88
+ cacheManager := cache.New(ristrettoStore)
89
+ err := cacheManager.Set(" my-key" , " my-value" , &cache.Options{Cost: 2} )
82
90
if err != nil {
83
91
panic(err)
84
92
}
@@ -89,10 +97,12 @@ value := cacheManager.Get("my-key")
89
97
#### Redis
90
98
91
99
```go
92
- redisStore := store.NewRedis(redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " }))
100
+ redisStore := store.NewRedis(redis.NewClient(&redis.Options{
101
+ Addr: " 127.0.0.1 :6379 " ,
102
+ }), nil)
93
103
94
- cacheManager := cache.New(redisStore, &cache.Options{Expiration: 15*time.Second} )
95
- err := cacheManager.Set(" my-key" , " my-value" )
104
+ cacheManager := cache.New(redisStore)
105
+ err := cacheManager.Set(" my-key" , " my-value" , &cache.Options{Expiration: 15*time.Second} )
96
106
if err != nil {
97
107
panic(err)
98
108
}
@@ -114,13 +124,13 @@ if err != nil {
114
124
redisClient := redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " })
115
125
116
126
// Initialize stores
117
- ristrettoStore := store.NewRistretto(ristrettoCache)
118
- redisStore := store.NewRedis(redisClient)
127
+ ristrettoStore := store.NewRistretto(ristrettoCache, nil )
128
+ redisStore := store.NewRedis(redisClient, &cache.Optiobs{Expiration: 5*time.Second} )
119
129
120
130
// Initialize chained cache
121
131
cacheManager := cache.NewChain(
122
- cache.New(ristrettoStore, nil ),
123
- cache.New(redisStore, &cache.Options{Expiration: 15*time.Second} ),
132
+ cache.New(ristrettoStore),
133
+ cache.New(redisStore),
124
134
)
125
135
126
136
// ... Then, do what you want with your cache
@@ -135,7 +145,7 @@ This cache will provide a load function that acts as a callable function and wil
135
145
```go
136
146
// Initialize Redis client and store
137
147
redisClient := redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " })
138
- redisStore := store.NewRedis(redisClient)
148
+ redisStore := store.NewRedis(redisClient, nil )
139
149
140
150
// Initialize a load function that loads your data from a custom source
141
151
loadFunction := func(key interface{}) (interface{}, error) {
@@ -146,7 +156,7 @@ loadFunction := func(key interface{}) (interface{}, error) {
146
156
// Initialize loadable cache
147
157
cacheManager := cache.NewLoadable(
148
158
loadFunction,
149
- cache.New(redisStore, &cache.Options{Expiration: 15*time.Second} ),
159
+ cache.New(redisStore),
150
160
)
151
161
152
162
// ... Then, you can get your data and your function will automatically put them in cache(s)
@@ -161,15 +171,15 @@ This cache will record metrics depending on the metric provider you pass to it.
161
171
```go
162
172
// Initialize Redis client and store
163
173
redisClient := redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " })
164
- redisStore := store.NewRedis(redisClient)
174
+ redisStore := store.NewRedis(redisClient, nil )
165
175
166
176
// Initializes Prometheus metrics service
167
177
promMetrics := metrics.NewPrometheus(" my-test-app" )
168
178
169
179
// Initialize metric cache
170
180
cacheManager := cache.NewMetric(
171
181
promMetrics,
172
- cache.New(redisStore, &cache.Options{Expiration: 15*time.Second} ),
182
+ cache.New(redisStore),
173
183
)
174
184
175
185
// ... Then, you can get your data and metrics will be observed by Prometheus
@@ -182,12 +192,12 @@ Some caches like Redis stores and returns the value as a string so you have to m
182
192
```go
183
193
// Initialize Redis client and store
184
194
redisClient := redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " })
185
- redisStore := store.NewRedis(redisClient)
195
+ redisStore := store.NewRedis(redisClient, nil )
186
196
187
197
// Initialize chained cache
188
198
cacheManager := cache.NewMetric(
189
199
promMetrics,
190
- cache.New(redisStore, &cache.Options{Expiration: 15*time.Second} ),
200
+ cache.New(redisStore),
191
201
)
192
202
193
203
// Initializes marshaler
@@ -242,13 +252,20 @@ func main() {
242
252
// Initialize Prometheus metrics collector
243
253
promMetrics := metrics.NewPrometheus(" my-test-app" )
244
254
245
- ristrettoCache, err := ristretto.NewCache(&ristretto.Config{NumCounters: 1000, MaxCost: 100, BufferItems: 64})
255
+ // Initialize Ristretto store
256
+ ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
257
+ NumCounters: 1000,
258
+ MaxCost: 100,
259
+ BufferItems: 64,
260
+ })
246
261
if err != nil {
247
262
panic(err)
248
263
}
249
264
250
- ristrettoStore := store.NewRistretto(ristrettoCache)
251
- redisStore := store.NewRedis(redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " }))
265
+ ristrettoStore := store.NewRistretto(ristrettoCache, &cache.Options{Cost: 4})
266
+
267
+ // Initialize Redis store
268
+ redisStore := store.NewRedis(redis.NewClient(&redis.Options{Addr: " 127.0.0.1 :6379 " }), &cache.Options{Expiration: 5*time.Second})
252
269
253
270
// Initialize a load function that loads your data from a custom source
254
271
loadFunction := func(key interface{}) (interface{}, error) {
@@ -260,8 +277,8 @@ func main() {
260
277
// and a load function that will put data back into caches if none has the value
261
278
cacheManager := cache.NewMetric(promMetrics, cache.NewLoadable(loadFunction,
262
279
cache.NewChain(
263
- cache.New(ristrettoStore, nil ),
264
- cache.New(redisStore, &cache.Options{Expiration: 15*time.Second} ),
280
+ cache.New(ristrettoStore),
281
+ cache.New(redisStore),
265
282
),
266
283
))
267
284
@@ -270,7 +287,7 @@ func main() {
270
287
key := Book{Slug: " my-test-amazing-book" }
271
288
value := Book{ID: 1, Name: " My test amazing book" , Slug: " my-test-amazing-book" }
272
289
273
- err = marshaller.Set(key, value)
290
+ err = marshaller.Set(key, value, nil )
274
291
if err != nil {
275
292
panic(err)
276
293
}
0 commit comments