-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear.go
More file actions
54 lines (43 loc) · 1.27 KB
/
clear.go
File metadata and controls
54 lines (43 loc) · 1.27 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
package main
import (
"context"
"fmt"
"os"
"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/internal/constants"
)
const cacheCapacity = 100000
func main() {
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultTimeout)
defer cancel()
// Create a new HyperCache with a capacity of 100000
cache, err := hypercache.NewInMemoryWithDefaults(ctx, cacheCapacity)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// Stop the cache when the program exits
defer cache.Stop(ctx)
fmt.Fprintln(os.Stdout, "adding 100000 items to cache")
for i := range cacheCapacity {
err := cache.Set(ctx, fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), 0)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
item, ok := cache.Get(ctx, "key100")
if ok {
fmt.Fprintln(os.Stdout, "key100", item)
}
fmt.Fprintln(os.Stdout, "capacity", cache.Capacity())
fmt.Fprintln(os.Stdout, "count", cache.Count(ctx))
fmt.Fprintln(os.Stdout, "allocation", cache.Allocation())
fmt.Fprintln(os.Stdout, "clearing cache")
err = cache.Clear(ctx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Fprintln(os.Stdout, "capacity", cache.Capacity())
fmt.Fprintln(os.Stdout, "count", cache.Count(ctx))
fmt.Fprintln(os.Stdout, "allocation", cache.Allocation())
}