| 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 |
|
| 12 |
fmt.Println("=== Single-threaded Cache Example ===") |
| 13 |
singleThreadedExample() |
| 14 |
|
| 15 |
|
| 16 |
fmt.Println("\n=== Thread-safe Cache Example ===") |
| 17 |
threadSafeExample() |
| 18 |
|
| 19 |
|
| 20 |
fmt.Println("\n=== Sharded Cache Example ===") |
| 21 |
shardedExample() |
| 22 |
} |
| 23 |
|
| 24 |
func singleThreadedExample() { |
| 25 |
|
| 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 |
|
| 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 |
|
| 39 |
val, ok := cache.Get("key1") |
| 40 |
if ok { |
| 41 |
fmt.Printf("Found key1: %s\n", val) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
cache.Insert("key4", "value4") |
| 46 |
fmt.Printf("Cache length after inserting key4: %d\n", cache.Len()) |
| 47 |
|
| 48 |
|
| 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 |
|
| 57 |
if cache.ContainsKey("key1") { |
| 58 |
fmt.Println("key1 was retained") |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
fmt.Println("Keys in cache:", cache.Keys()) |
| 63 |
|
| 64 |
|
| 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 |
|
| 71 |
cache, _ := sievecache.NewSync[string, int](100) |
| 72 |
|
| 73 |
|
| 74 |
for i := 0; i < 10; i++ { |
| 75 |
cache.Insert(fmt.Sprintf("key%d", i), i) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
cache.GetMut("key5", func(val *int) { |
| 80 |
*val *= 10 |
| 81 |
}) |
| 82 |
|
| 83 |
|
| 84 |
val, _ := cache.Get("key5") |
| 85 |
fmt.Printf("Modified value of key5: %d\n", val) |
| 86 |
|
| 87 |
|
| 88 |
cache.ForEachValue(func(val *int) { |
| 89 |
*val += 1 |
| 90 |
}) |
| 91 |
|
| 92 |
|
| 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 |
|
| 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 |
|
| 109 |
cache, _ := sievecache.NewShardedWithShards[string, int](100, 8) |
| 110 |
fmt.Printf("Created sharded cache with %d shards\n", cache.NumShards()) |
| 111 |
|
| 112 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |