-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
75 lines (65 loc) · 2.15 KB
/
Copy pathexample_test.go
File metadata and controls
75 lines (65 loc) · 2.15 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
package cacheobj_test
import (
"fmt"
"regexp"
"time"
"github.com/ubgo/cache"
cacheobj "github.com/ubgo/cache-obj"
)
// The headline use case: cache a live, non-serializable object — here a
// compiled *regexp.Regexp — and get the exact same instance back, with no
// recompilation and no serialization.
func Example() {
re := cacheobj.New[*regexp.Regexp](cacheobj.WithCapacity(128))
compile := func(pattern string) *regexp.Regexp {
if r, ok := re.Get(pattern); ok {
return r // same compiled program, zero cost
}
r := regexp.MustCompile(pattern)
re.Set(pattern, r)
return r
}
a := compile(`\d+`)
b := compile(`\d+`) // served from cache — identical instance
fmt.Println(a == b)
fmt.Println(a.MatchString("abc123"))
// Output:
// true
// true
}
// Remember is get-or-load with single-flight: the loader runs once on a miss
// (and once across concurrent misses for the same key), and the result is
// cached. A second call is served from cache without re-running the loader.
func ExampleStore_Remember() {
c := cacheobj.New[int]()
calls := 0
load := func() (int, error) { calls++; return 42, nil }
v1, _ := c.Remember("answer", time.Minute, load)
v2, _ := c.Remember("answer", time.Minute, load) // cached — loader skipped
fmt.Println(v1, v2, calls)
// Output: 42 42 1
}
// SetTTL stores a value with an explicit lifetime; a non-positive TTL means
// the entry never expires.
func ExampleStore_SetTTL() {
c := cacheobj.New[string]()
c.SetTTL("token", "secret", 5*time.Minute)
c.SetTTL("config", "immutable", 0) // 0 => never expires
v, ok := c.Get("token")
fmt.Println(v, ok)
// Output: secret true
}
// WithOnEvict observes involuntary drops (capacity or expiry) and receives
// the evicted key AND value — so it can release whatever the value owns. Here
// capacity is 1, so the second Set evicts the first.
func ExampleWithOnEvict() {
c := cacheobj.New[string](
cacheobj.WithCapacity(1),
cacheobj.WithOnEvict(func(key, value string, cause cache.EvictionCause) {
fmt.Printf("evicted %q=%q (%s)\n", key, value, cause)
}),
)
c.Set("a", "first")
c.Set("b", "second") // evicts "a" by capacity
// Output: evicted "a"="first" (size)
}