-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.go
39 lines (32 loc) · 1.04 KB
/
value.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
package sc
import (
"time"
)
var t0 = time.Now()
// monoTime represents the elapsed nanoseconds since t0.
// This utilizes monotonic clock of package time.
//
// Just like time.Duration, the maximum representation is approximately 290 years.
type monoTime int64
func monoTimeNow() monoTime {
return monoTime(time.Since(t0))
}
// value represents a cache item.
//
// Value can be in one of 3 states:
// 1. A value is "fresh" for the given freshFor duration.
// 2. A value is "stale" if it is neither fresh nor expired.
// 3. A value is "expired" after the given ttl duration.
type value[V any] struct {
v V
// created is the time the function to retrieve v was called.
// Storing created as monoTime instead of time.Time allows GC to skip the scan of values entirely if V does not
// contain pointers.
created monoTime
}
func (v *value[V]) isFresh(now monoTime, freshFor time.Duration) bool {
return now <= v.created+monoTime(freshFor)
}
func (v *value[V]) isExpired(now monoTime, ttl time.Duration) bool {
return v.created+monoTime(ttl) < now
}