Debian Codegraph @

pkg/sievecache/example_test.go

raw symbols SCIP
1 package sievecache_test
2
3 import (
4 "fmt"
5
6 "github.com/jedisct1/go-sieve-cache/pkg/sievecache"
7 )
8
9 // Example demonstrates basic usage of the SieveCache
10 func Example() {
11 // Create a new cache with capacity for 100 items
12 cache, _ := sievecache.New[string, string](100)
13
14 // Insert some items
15 cache.Insert("key1", "value1")
16 cache.Insert("key2", "value2")
17
18 // Get an item
19 value, found := cache.Get("key1")
20 if found {
21 fmt.Println("Found:", value)
22 }
23
24 // Remove an item
25 cache.Remove("key2")
26
27 // Check the current cache size
28 fmt.Println("Cache size:", cache.Len())
29
30 // Output:
31 // Found: value1
32 // Cache size: 1
33 }
34
35 // ExampleSyncSieveCache demonstrates thread-safe cache usage
36 func ExampleSyncSieveCache() {
37 // Create a thread-safe cache
38 cache, _ := sievecache.NewSync[string, int](100)
39
40 // Insert some values
41 cache.Insert("counter", 5)
42
43 // Safely modify values
44 cache.GetMut("counter", func(value *int) {
45 *value = *value * 2
46 })
47
48 // Retrieve modified value
49 val, _ := cache.Get("counter")
50 fmt.Println("Counter:", val)
51
52 // Perform multiple operations atomically
53 cache.WithLock(func(innerCache *sievecache.SieveCache[string, int]) {
54 innerCache.Insert("a", 1)
55 innerCache.Insert("b", 2)
56 innerCache.Insert("sum", 3)
57 })
58
59 fmt.Println("Cache size:", cache.Len())
60
61 // Output:
62 // Counter: 10
63 // Cache size: 4
64 }
65
66 // ExampleShardedSieveCache demonstrates usage of the sharded cache for high concurrency
67 func ExampleShardedSieveCache() {
68 // We'll use a simpler example to avoid issues with sharding
69 cache, _ := sievecache.NewShardedWithShards[string, int](1000, 8)
70 fmt.Printf("Created sharded cache with %d shards\n", cache.NumShards())
71
72 // Basic operations work the same as other cache types
73 cache.Insert("counter", 0)
74
75 // Increment counter using GetMut
76 cache.GetMut("counter", func(value *int) {
77 *value += 1
78 })
79
80 // Check the value
81 val, _ := cache.Get("counter")
82 fmt.Println("Counter:", val)
83
84 // Get and modify the value
85 cache.Insert("counter", 5)
86 val, _ = cache.Get("counter")
87 fmt.Println("Counter:", val)
88
89 // Insert a new key
90 cache.Insert("related", 10)
91 relatedVal, _ := cache.Get("related")
92 fmt.Println("Related:", relatedVal)
93
94 // Output:
95 // Created sharded cache with 8 shards
96 // Counter: 1
97 // Counter: 5
98 // Related: 10
99 }
100
101 // ExampleSieveCache_RecommendedCapacity demonstrates the capacity recommendation feature
102 func ExampleSieveCache_RecommendedCapacity() {
103 cache, _ := sievecache.New[string, int](100)
104
105 // Add some data and access some of it to create a pattern
106 for i := 0; i < 80; i++ {
107 cache.Insert(fmt.Sprintf("key%d", i), i)
108
109 // Access every other key to mark it as visited
110 if i%2 == 0 {
111 cache.Get(fmt.Sprintf("key%d", i))
112 }
113 }
114
115 // Parameters:
116 // - minFactor: 0.5 (never go below 50% of current capacity)
117 // - maxFactor: 2.0 (never go above 200% of current capacity)
118 // - lowThreshold: 0.3 (reduce capacity if utilization is below 30%)
119 // - highThreshold: 0.7 (increase capacity if utilization is above 70%)
120 newCapacity := cache.RecommendedCapacity(0.5, 2.0, 0.3, 0.7)
121 fmt.Printf("Recommended capacity for a cache with %d/%d items: %d\n",
122 cache.Len(), cache.Capacity(), newCapacity)
123
124 // Note: Exact output may vary slightly, so we're not including the Output comment
125 }