-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
26 lines (23 loc) · 752 Bytes
/
entry.go
File metadata and controls
26 lines (23 loc) · 752 Bytes
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
package cachex
import "time"
// Entry is a wrapper for a value with a cache timestamp
type Entry[T any] struct {
Data T `json:"data"`
CachedAt time.Time `json:"cachedAt"`
}
// EntryWithTTL is a convenience function to configure entry caching with TTL
// freshTTL: how long data stays fresh
// staleTTL: how long data stays stale (additional time after freshTTL)
// Entries in [0, freshTTL) are fresh, [freshTTL, freshTTL+staleTTL) are stale
func EntryWithTTL[T any](freshTTL, staleTTL time.Duration) ClientOption[*Entry[T]] {
return WithStale(func(v *Entry[T]) State {
age := NowFunc().Sub(v.CachedAt)
if age < freshTTL {
return StateFresh
}
if age < freshTTL+staleTTL {
return StateStale
}
return StateRotten
})
}