| 1 | # SIEVE Cache for Go |
| 2 | |
| 3 | [](https://pkg.go.dev/github.com/jedisct1/go-sieve-cache) |
| 4 | [](https://opensource.org/licenses/MIT) |
| 5 | |
| 6 | A high-performance Go implementation of the SIEVE cache replacement algorithm with thread-safe and sharded variants. |
| 7 | |
| 8 | ## What is SIEVE? |
| 9 | |
| 10 | SIEVE (Simple, space-efficient, In-memory, EViction mEchanism) is a cache eviction algorithm that maintains a single bit per entry to track whether an item has been "visited" since it was last considered for eviction. This approach requires less state than LRU but achieves excellent performance, especially on skewed workloads. |
| 11 | |
| 12 | ## Features |
| 13 | |
| 14 | - **Simple API**: Easy to use and integrate with existing code |
| 15 | - **Generic implementation**: Works with any key and value types |
| 16 | - **High performance**: Efficient implementation with O(1) operations |
| 17 | - **Thread safety options**: Choose the right level of concurrency for your needs |
| 18 | - **Minimal memory overhead**: Uses only a single bit per entry for tracking |
| 19 | - **Dynamic sizing**: Can recommend capacity adjustments based on access patterns |
| 20 | |
| 21 | ## Performance |
| 22 | |
| 23 | SIEVE offers excellent performance comparable to or better than LRU for most workloads, while using significantly less memory overhead (1 bit per entry vs. pointers for doubly-linked list in LRU). |
| 24 | |
| 25 | ### When to use SIEVE |
| 26 | |
| 27 | - When memory efficiency is important |
| 28 | - For applications with skewed access patterns (some keys accessed much more frequently) |
| 29 | - When you need a simple, fast, and effective caching solution |
| 30 | |
| 31 | ### Benchmarks |
| 32 | |
| 33 | ``` |
| 34 | $ go test -bench=. ./pkg/sievecache -benchtime=1s |
| 35 | |
| 36 | BenchmarkSieveCache_Mixed-10 5177452 224.9 ns/op |
| 37 | BenchmarkSyncSieveCache_Mixed-10 2671064 434.5 ns/op |
| 38 | BenchmarkShardedSieveCache_Mixed-10 3809210 325.1 ns/op |
| 39 | BenchmarkParallelAccess-10 12815046 88.4 ns/op |
| 40 | ``` |
| 41 | |
| 42 | The sharded implementation offers the best performance under high concurrent load, as shown in the parallel access benchmark. |
| 43 | |
| 44 | ## Implementation Options |
| 45 | |
| 46 | This package provides three cache implementations: |
| 47 | |
| 48 | 1. **SieveCache**: The core single-threaded implementation for use in simple scenarios |
| 49 | 2. **SyncSieveCache**: A thread-safe cache for multi-threaded applications with moderate concurrency |
| 50 | 3. **ShardedSieveCache**: A highly concurrent cache that shards data across multiple internal caches |
| 51 | |
| 52 | ## Quick Start |
| 53 | |
| 54 | ```go |
| 55 | package main |
| 56 | |
| 57 | import ( |
| 58 | "fmt" |
| 59 | "github.com/jedisct1/go-sieve-cache/pkg/sievecache" |
| 60 | ) |
| 61 | |
| 62 | func main() { |
| 63 | // Create a new cache with capacity for 100 items |
| 64 | cache, _ := sievecache.New[string, string](100) |
| 65 | |
| 66 | // Insert some items |
| 67 | cache.Insert("key1", "value1") |
| 68 | cache.Insert("key2", "value2") |
| 69 | |
| 70 | // Get an item |
| 71 | value, found := cache.Get("key1") |
| 72 | if found { |
| 73 | fmt.Println("Found:", value) |
| 74 | } |
| 75 | |
| 76 | // Remove an item |
| 77 | cache.Remove("key2") |
| 78 | |
| 79 | // Check the current cache size |
| 80 | fmt.Println("Cache size:", cache.Len()) |
| 81 | |
| 82 | // For thread-safe operations |
| 83 | syncCache, _ := sievecache.NewSync[string, int](100) |
| 84 | |
| 85 | // For high-concurrency applications |
| 86 | shardedCache, _ := sievecache.NewSharded[string, int](1000) |
| 87 | |
| 88 | // The sharded cache can update values with mutator functions |
| 89 | shardedCache.Insert("counter", 0) |
| 90 | shardedCache.GetMut("counter", func(value *int) { |
| 91 | *value++ |
| 92 | }) |
| 93 | |
| 94 | // Check the current counter value |
| 95 | counterValue, _ := shardedCache.Get("counter") |
| 96 | fmt.Println("Counter:", counterValue) |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | ## Advanced Usage |
| 101 | |
| 102 | ### Using the Thread-Safe Cache |
| 103 | |
| 104 | ```go |
| 105 | // Create a thread-safe cache |
| 106 | cache, _ := sievecache.NewSync[string, int](1000) |
| 107 | |
| 108 | // Safely modify values |
| 109 | cache.GetMut("key", func(value *int) { |
| 110 | *value = *value * 2 |
| 111 | }) |
| 112 | |
| 113 | // Perform multiple operations atomically |
| 114 | cache.WithLock(func(innerCache *sievecache.SieveCache[string, int]) { |
| 115 | // All operations here are atomic |
| 116 | item1, _ := innerCache.Get("item1") |
| 117 | item2, _ := innerCache.Get("item2") |
| 118 | innerCache.Insert("sum", item1 + item2) |
| 119 | }) |
| 120 | |
| 121 | // Modify all values in one operation |
| 122 | cache.ForEachValue(func(value *int) { |
| 123 | *value += 1 |
| 124 | }) |
| 125 | ``` |
| 126 | |
| 127 | ### Working with the Sharded Cache |
| 128 | |
| 129 | ```go |
| 130 | // Create a sharded cache with 32 shards for high concurrency |
| 131 | cache, _ := sievecache.NewShardedWithShards[string, string](10000, 32) |
| 132 | |
| 133 | // Operations work the same as the other cache types |
| 134 | cache.Insert("key", "value") |
| 135 | value, _ := cache.Get("key") |
| 136 | |
| 137 | // For operations that need to be atomic within a shard |
| 138 | cache.WithKeyLock("key", func(shard *sievecache.SieveCache[string, string]) { |
| 139 | // These operations are atomic only for keys in the same shard as "key" |
| 140 | shard.Insert("key", "new value") |
| 141 | shard.Insert("related_key", "related value") |
| 142 | }) |
| 143 | ``` |
| 144 | |
| 145 | ## Performance Tuning |
| 146 | |
| 147 | The cache provides a `RecommendedCapacity` method that analyzes the current usage pattern and recommends an optimal capacity: |
| 148 | |
| 149 | ```go |
| 150 | // Get a recommended cache size based on access patterns |
| 151 | newCapacity := cache.RecommendedCapacity(0.5, 2.0, 0.3, 0.7) |
| 152 | fmt.Printf("Recommended capacity: %d\n", newCapacity) |
| 153 | ``` |
| 154 | |
| 155 | Parameters: |
| 156 | - `minFactor`: Minimum scaling factor (0.5 means never go below 50% of current capacity) |
| 157 | - `maxFactor`: Maximum scaling factor (2.0 means never go above 200% of current capacity) |
| 158 | - `lowThreshold`: Utilization threshold below which capacity is reduced |
| 159 | - `highThreshold`: Utilization threshold above which capacity is increased |
| 160 | |
| 161 | ## Installation |
| 162 | |
| 163 | ```sh |
| 164 | go get github.com/jedisct1/go-sieve-cache |
| 165 | ``` |
| 166 | |
| 167 | ## License |
| 168 | |
| 169 | MIT License |