Debian Codegraph @

cmd/example/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/jedisct1/go-sieve-cache/pkg/sievecache"
8 )
9
10 func main() {
11 // Create a single-threaded cache
12 fmt.Println("=== Single-threaded Cache Example ===")
13 singleThreadedExample()
14
15 // Create a thread-safe cache
16 fmt.Println("\n=== Thread-safe Cache Example ===")
17 threadSafeExample()
18
19 // Create a sharded cache
20 fmt.Println("\n=== Sharded Cache Example ===")
21 shardedExample()
22 }
23
24 func singleThreadedExample() {
25 // Create a new cache with capacity for 3 items
26 cache, err := sievecache.New[string, string](3)
27 if err != nil {
28 fmt.Printf("Error creating cache: %v\n", err)
29 return
30 }
31
32 // Insert some values
33 cache.Insert("key1", "value1")
34 cache.Insert("key2", "value2")
35 cache.Insert("key3", "value3")
36 fmt.Printf("Cache length after initial inserts: %d\n", cache.Len())
37
38 // Access some items to mark them as visited
39 val, ok := cache.Get("key1")
40 if ok {
41 fmt.Printf("Found key1: %s\n", val)
42 }
43
44 // Insert a new item, should evict the least recently visited
45 cache.Insert("key4", "value4")
46 fmt.Printf("Cache length after inserting key4: %d\n", cache.Len())
47
48 // key2 or key3 should have been evicted (they weren't visited)
49 if !cache.ContainsKey("key2") {
50 fmt.Println("key2 was evicted")
51 }
52 if !cache.ContainsKey("key3") {
53 fmt.Println("key3 was evicted")
54 }
55
56 // key1 should still be there (it was visited)
57 if cache.ContainsKey("key1") {
58 fmt.Println("key1 was retained")
59 }
60
61 // Print all keys
62 fmt.Println("Keys in cache:", cache.Keys())
63
64 // Get a recommended capacity based on utilization
65 recommended := cache.RecommendedCapacity(0.5, 2.0, 0.3, 0.7)
66 fmt.Printf("Recommended capacity: %d\n", recommended)
67 }
68
69 func threadSafeExample() {
70 // Create a new thread-safe cache with capacity for 100 items
71 cache, _ := sievecache.NewSync[string, int](100)
72
73 // Insert some values
74 for i := 0; i < 10; i++ {
75 cache.Insert(fmt.Sprintf("key%d", i), i)
76 }
77
78 // Modify a value with the callback
79 cache.GetMut("key5", func(val *int) {
80 *val *= 10
81 })
82
83 // Get the modified value
84 val, _ := cache.Get("key5")
85 fmt.Printf("Modified value of key5: %d\n", val)
86
87 // Use ForEachValue to modify all values
88 cache.ForEachValue(func(val *int) {
89 *val += 1
90 })
91
92 // Check some values
93 val, _ = cache.Get("key5")
94 fmt.Printf("key5 after ForEachValue: %d\n", val)
95 val, _ = cache.Get("key1")
96 fmt.Printf("key1 after ForEachValue: %d\n", val)
97
98 // Retain only even values
99 cache.Retain(func(key string, value int) bool {
100 return value%2 == 0
101 })
102
103 fmt.Printf("Cache length after retain: %d\n", cache.Len())
104 fmt.Println("Keys in cache after retain:", cache.Keys())
105 }
106
107 func shardedExample() {
108 // Create a sharded cache with 8 shards
109 cache, _ := sievecache.NewShardedWithShards[string, int](100, 8)
110 fmt.Printf("Created sharded cache with %d shards\n", cache.NumShards())
111
112 // Insert items with different patterns to test sharding
113 start := time.Now()
114 for i := 0; i < 10000; i++ {
115 cache.Insert(fmt.Sprintf("key%d", i), i)
116 }
117 fmt.Printf("Inserted 10000 items in %v\n", time.Since(start))
118
119 // Check a few keys
120 for i := 0; i < 5; i++ {
121 key := fmt.Sprintf("key%d", i*1000)
122 val, ok := cache.Get(key)
123 if ok {
124 fmt.Printf("Found %s: %d\n", key, val)
125 }
126 }
127
128 // Perform batch modification
129 start = time.Now()
130 cache.ForEachValue(func(val *int) {
131 *val = *val * 2
132 })
133 fmt.Printf("Doubled all values in %v\n", time.Since(start))
134
135 // Check the modified values
136 for i := 0; i < 5; i++ {
137 key := fmt.Sprintf("key%d", i*1000)
138 val, ok := cache.Get(key)
139 if ok {
140 fmt.Printf("%s after doubling: %d\n", key, val)
141 }
142 }
143
144 fmt.Printf("Final cache length: %d\n", cache.Len())
145 }