-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize.go
More file actions
98 lines (81 loc) · 2.2 KB
/
size.go
File metadata and controls
98 lines (81 loc) · 2.2 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"
"math/rand/v2"
"os"
"time"
"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/internal/constants"
"github.com/hyp3rd/hypercache/pkg/backend"
)
const (
numUsers = 100
maxCacheSize = 37326
cacheCapacity = 100000
)
type user struct {
Name string
Age int
LastAccess time.Time
Pass string
Email string
Phone string
IsAdmin bool
IsGuest bool
IsBanned bool
}
func generateRandomUsers() []user {
users := make([]user, 0, numUsers)
for i := range numUsers {
users = append(users, user{
Name: fmt.Sprintf("User%d", i),
Age: rand.IntN(numUsers),
LastAccess: time.Now(),
Pass: fmt.Sprintf("Pass%d", i),
Email: fmt.Sprintf("user%d@example.com", i),
Phone: fmt.Sprintf("123456789%d", i),
IsAdmin: rand.IntN(2) == 0,
IsGuest: rand.IntN(2) == 0,
IsBanned: rand.IntN(2) == 0,
})
}
return users
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultTimeout*2)
defer cancel()
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](0),
hypercache.WithEvictionAlgorithm[backend.InMemory]("cawolfu"),
hypercache.WithMaxCacheSize[backend.InMemory](maxCacheSize),
}
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 {
panic(err)
}
for i := range 3 {
err = cache.Set(context.TODO(), fmt.Sprintf("key-%d", i), generateRandomUsers(), 0)
if err != nil {
fmt.Fprintln(os.Stdout, err, "set", i)
}
}
key, ok := cache.GetWithInfo(context.TODO(), "key-1")
if ok {
fmt.Fprintln(os.Stdout, "value", key.Value)
fmt.Fprintln(os.Stdout, "size", key.Size)
} else {
fmt.Fprintln(os.Stdout, "key not found")
}
fmt.Fprintln(os.Stdout, cache.Count(context.TODO()))
fmt.Fprintln(os.Stdout, cache.Allocation())
}