-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
262 lines (227 loc) · 7.75 KB
/
Copy pathcache.go
File metadata and controls
262 lines (227 loc) · 7.75 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Package cache provides a Laravel-inspired caching library for Go with
// support for multiple backends (memory, file, Redis) configured via env vars.
//
// Quick start:
//
// c, _ := cache.New()
//
// c.Put(ctx, "key", "value", time.Minute)
//
// result, _ := cache.Remember(ctx, c, "users", time.Hour, func() ([]User, error) {
// return db.FindAllUsers()
// })
package cache
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"time"
"github.com/hymns/go-cache/drivers"
"golang.org/x/sync/singleflight"
)
// Cache wraps a Store with a Laravel-like API.
type Cache struct {
store Store
prefix string
ttl time.Duration
group singleflight.Group // prevents cache stampedes in Remember/RememberForever
}
// New creates a Cache using ConfigFromEnv.
// This is the standard entry point — configure via environment variables.
func New() (*Cache, error) {
return NewConfig(ConfigFromEnv())
}
// NewConfig creates a Cache from an explicit Config struct.
func NewConfig(cfg Config) (*Cache, error) {
var (
store Store
err error
)
switch cfg.Driver {
case DriverMemory, "":
store = drivers.NewMemory()
case DriverFile:
store, err = drivers.NewFile(cfg.FilePath)
case DriverRedis:
store, err = drivers.NewRedis(cfg.RedisAddr, cfg.RedisPassword, cfg.RedisDB)
default:
return nil, fmt.Errorf("cache: unknown driver %q (want memory|file|redis)", cfg.Driver)
}
if err != nil {
return nil, err
}
return &Cache{store: store, prefix: cfg.Prefix, ttl: cfg.TTL}, nil
}
// NewWithStore creates a Cache around an existing Store.
// Useful for testing or when you need to share a store across multiple Cache instances.
func NewWithStore(store Store, cfg Config) *Cache {
return &Cache{store: store, prefix: cfg.Prefix, ttl: cfg.TTL}
}
func (c *Cache) prefixed(key string) string {
if c.prefix == "" {
return key
}
return c.prefix + ":" + key
}
// DefaultTTL returns the configured default TTL.
func (c *Cache) DefaultTTL() time.Duration { return c.ttl }
// ----------------------------------------------------------------------------
// Core read/write
// ----------------------------------------------------------------------------
// Get retrieves a cached value into dest (must be a pointer).
// Returns false if the key does not exist or has expired.
func (c *Cache) Get(ctx context.Context, key string, dest any) (bool, error) {
b, found, err := c.store.Get(ctx, c.prefixed(key))
if !found || err != nil {
return false, err
}
return true, json.Unmarshal(b, dest)
}
// Put stores value with the given TTL.
func (c *Cache) Put(ctx context.Context, key string, value any, ttl time.Duration) error {
b, err := json.Marshal(value)
if err != nil {
return err
}
return c.store.Put(ctx, c.prefixed(key), b, ttl)
}
// Forever stores value without an expiry.
func (c *Cache) Forever(ctx context.Context, key string, value any) error {
b, err := json.Marshal(value)
if err != nil {
return err
}
return c.store.Forever(ctx, c.prefixed(key), b)
}
// Add stores value only if the key does not already exist.
// Returns true if the value was stored.
func (c *Cache) Add(ctx context.Context, key string, value any, ttl time.Duration) (bool, error) {
b, err := json.Marshal(value)
if err != nil {
return false, err
}
return c.store.Add(ctx, c.prefixed(key), b, ttl)
}
// ----------------------------------------------------------------------------
// Remember helpers (generic, package-level)
// ----------------------------------------------------------------------------
// Remember returns the cached value for key. If the key is missing it calls fn,
// stores the result with ttl, and returns it.
//
// Concurrent calls for the same key on a cache miss are coalesced: only one
// goroutine executes fn and all others receive the same result, preventing
// cache stampedes.
//
// users, err := cache.Remember(ctx, c, "users.all", time.Hour, func() ([]User, error) {
// return db.FindAllUsers()
// })
func Remember[T any](ctx context.Context, c *Cache, key string, ttl time.Duration, fn func() (T, error)) (T, error) {
var zero T
// Fast path: already cached.
var dest T
if found, err := c.Get(ctx, key, &dest); found || err != nil {
return dest, err
}
// Slow path: coalesce concurrent misses via singleflight.
v, err, _ := c.group.Do(key, func() (any, error) {
// Double-check: another goroutine may have populated the cache while we
// were waiting for the singleflight slot.
var dest2 T
if found, err := c.Get(ctx, key, &dest2); found || err != nil {
return dest2, err
}
val, err := fn()
if err != nil {
return nil, err
}
return val, c.Put(ctx, key, val, ttl)
})
if err != nil {
return zero, err
}
return v.(T), nil
}
// RememberForever is like Remember but stores without expiry.
// Concurrent misses for the same key are coalesced (stampede-safe).
func RememberForever[T any](ctx context.Context, c *Cache, key string, fn func() (T, error)) (T, error) {
var zero T
var dest T
if found, err := c.Get(ctx, key, &dest); found || err != nil {
return dest, err
}
v, err, _ := c.group.Do(key, func() (any, error) {
var dest2 T
if found, err := c.Get(ctx, key, &dest2); found || err != nil {
return dest2, err
}
val, err := fn()
if err != nil {
return nil, err
}
return val, c.Forever(ctx, key, val)
})
if err != nil {
return zero, err
}
return v.(T), nil
}
// ----------------------------------------------------------------------------
// Pull, Has, Forget, Flush
// ----------------------------------------------------------------------------
// Pull retrieves a cached value into dest and immediately removes it.
func (c *Cache) Pull(ctx context.Context, key string, dest any) (bool, error) {
found, err := c.Get(ctx, key, dest)
if !found || err != nil {
return found, err
}
return true, c.store.Forget(ctx, c.prefixed(key))
}
// Has reports whether a non-expired entry exists for key.
func (c *Cache) Has(ctx context.Context, key string) bool {
found, _ := c.store.Has(ctx, c.prefixed(key))
return found
}
// Forget removes a single cache entry.
func (c *Cache) Forget(ctx context.Context, key string) error {
return c.store.Forget(ctx, c.prefixed(key))
}
// Flush clears all cache entries (respects the store scope, e.g. Redis DB).
func (c *Cache) Flush(ctx context.Context) error {
return c.store.Flush(ctx)
}
// ----------------------------------------------------------------------------
// Increment / Decrement
// ----------------------------------------------------------------------------
// Increment atomically increases an integer counter by amount.
// Creates the key with value amount if it does not exist.
func (c *Cache) Increment(ctx context.Context, key string, amount int64) (int64, error) {
return c.store.Increment(ctx, c.prefixed(key), amount)
}
// Decrement atomically decreases an integer counter by amount.
func (c *Cache) Decrement(ctx context.Context, key string, amount int64) (int64, error) {
return c.store.Decrement(ctx, c.prefixed(key), amount)
}
// ----------------------------------------------------------------------------
// Typed convenience helpers
// ----------------------------------------------------------------------------
// GetString returns the cached string value, or def if missing.
func (c *Cache) GetString(ctx context.Context, key, def string) string {
var v string
found, _ := c.Get(ctx, key, &v)
if !found {
return def
}
return v
}
// GetInt returns the cached int64 counter value, or def if missing.
// Only works with keys written via Increment/Decrement.
func (c *Cache) GetInt(ctx context.Context, key string, def int64) int64 {
b, found, _ := c.store.Get(ctx, c.prefixed(key))
if !found || len(b) != 8 {
return def
}
return int64(binary.BigEndian.Uint64(b))
}
// Store returns the underlying Store for driver-specific operations.
func (c *Cache) Store() Store { return c.store }