-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheviction.go
More file actions
111 lines (85 loc) · 2.72 KB
/
eviction.go
File metadata and controls
111 lines (85 loc) · 2.72 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/internal/constants"
"github.com/hyp3rd/hypercache/pkg/backend"
)
const (
cacheCapacity = 10
evictionInterval = 10 * time.Second
evictionIntervalSlippage = 65 * time.Second
)
// This example demonstrates how to setup eviction of items from the cache.
func main() {
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultTimeout)
defer cancel()
log.Println("running an example of eviction with a background 3 seconds interval")
executeExample(ctx, evictionInterval)
log.Println("running an example with background eviction disabled and proactive eviction enabled")
executeExample(ctx, 0)
}
// executeExample runs the example.
func executeExample(ctx context.Context, evictionInterval time.Duration) {
// Create a new HyperCache with a capacity of 10
config, err := hypercache.NewConfig[backend.InMemory](constants.InMemoryBackend)
if err != nil {
fmt.Println(err)
return
}
config.HyperCacheOptions = []hypercache.Option[backend.InMemory]{
hypercache.WithEvictionInterval[backend.InMemory](evictionInterval),
}
config.InMemoryOptions = []backend.Option[backend.InMemory]{
backend.WithCapacity[backend.InMemory](cacheCapacity),
}
// Create a new HyperCache with a capacity of 10
cache, err := hypercache.New(ctx, hypercache.GetDefaultManager(), config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// Close the cache when the program exits
defer cache.Stop(ctx)
log.Println("cache capacity", cache.Capacity())
log.Println("adding 15 items to the cache, 5 over capacity")
for i := range 15 {
key := fmt.Sprintf("key%d", i)
val := fmt.Sprintf("val%d", i)
err = cache.Set(ctx, key, val, time.Minute)
if err != nil {
fmt.Fprintf(os.Stdout, "unexpected error: %v\n", err)
return
}
}
log.Println("capacity after adding 15 items", cache.Capacity())
log.Println("listing all items in the cache")
// Apply filters
sortByFilter := backend.WithSortBy(constants.SortByKey.String())
items, err := cache.List(ctx, sortByFilter)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
for _, item := range items {
fmt.Fprintln(os.Stdout, item.Key, item.Value)
}
if evictionInterval > 0 {
fmt.Fprintln(os.Stdout, "sleeping to allow the eviction loop to complete", evictionInterval+evictionIntervalSlippage)
time.Sleep(evictionInterval + evictionIntervalSlippage)
log.Println("listing all items in the cache the eviction is triggered")
list, err := cache.List(ctx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// Print the list of items
for i, ci := range list {
fmt.Fprintln(os.Stdout, i, ci.Value)
}
}
}