-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.go
More file actions
98 lines (79 loc) · 2.79 KB
/
stats.go
File metadata and controls
98 lines (79 loc) · 2.79 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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/internal/constants"
"github.com/hyp3rd/hypercache/pkg/backend"
)
const (
cacheCapacity = 100
delay = 10 * time.Second
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultTimeout)
defer cancel()
// Create a new HyperCache with a capacity of 100
config, err := hypercache.NewConfig[backend.InMemory](constants.InMemoryBackend)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
config.HyperCacheOptions = []hypercache.Option[backend.InMemory]{
hypercache.WithEvictionInterval[backend.InMemory](constants.DefaultEvictionInterval),
hypercache.WithEvictionAlgorithm[backend.InMemory](constants.DefaultEvictionAlgorithm),
hypercache.WithExpirationInterval[backend.InMemory](constants.DefaultExpirationInterval),
}
config.InMemoryOptions = []backend.Option[backend.InMemory]{
backend.WithCapacity[backend.InMemory](cacheCapacity),
}
// Create a new HyperCache with a capacity of 10
hyperCache, err := hypercache.New(ctx, hypercache.GetDefaultManager(), config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// Stop the cache when the program exits
defer hyperCache.Stop(ctx)
fmt.Fprintln(os.Stdout, "Adding 300 items to the cache")
// Add 300 items to the cache
for i := range 300 {
key := fmt.Sprintf("key%d", i)
val := fmt.Sprintf("val%d", i)
err = hyperCache.Set(context.TODO(), key, val, time.Minute)
if err != nil {
fmt.Fprintf(os.Stdout, "unexpected error: %v\n", err)
return
}
}
fmt.Fprintln(os.Stdout, "Sleeping to allow the cache to run its eviction cycle")
time.Sleep(delay)
// Retrieve the list of items from the cache
list, err := hyperCache.List(context.TODO())
// Check for errors
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Fprintf(os.Stdout, "Printing the list of items in the cache (should be %v items)\n\n", hyperCache.Capacity())
// Print the list of items
for i, ci := range list {
fmt.Fprintln(os.Stdout, i, ci.Value)
}
fmt.Fprintf(os.Stdout, "\nDisplaying the stats for the cache (should be %v items) with 1 eviction cycle\n\n", hyperCache.Capacity())
stats := hyperCache.GetStats()
// iterate over the stats and print them
for stat, statData := range stats {
fmt.Fprintf(os.Stdout, "# Stat: %s\n", stat)
fmt.Fprintf(os.Stdout, "Mean: %f\n", statData.Mean)
fmt.Fprintf(os.Stdout, "Median: %f\n", statData.Median)
fmt.Fprintf(os.Stdout, "Min: %d\n", statData.Min)
fmt.Fprintf(os.Stdout, "Max: %d\n", statData.Max)
fmt.Fprintf(os.Stdout, "Values: %v\n", statData.Values)
fmt.Fprintf(os.Stdout, "Count: %d\n", statData.Count)
fmt.Fprintf(os.Stdout, "Sum: %d\n", statData.Sum)
fmt.Fprintf(os.Stdout, "Variance: %f\n\n", statData.Variance)
}
}