Skip to content

Commit 20309fb

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
lint
1 parent ae9f861 commit 20309fb

12 files changed

+273
-182
lines changed

benchmarks/benchmark_comparison_test.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func generateWorkload(n int) []string {
3131
keys := make([]string, n)
3232
oneHitIndex := 0
3333

34-
for i := 0; i < n; i++ {
34+
for i := range n {
3535
if i%4 == 0 {
3636
// 25% one-hit wonders (cache pollution)
3737
idx := oneHitIndex % oneHitWonders
@@ -58,10 +58,12 @@ func BenchmarkHitRate_bdcache(b *testing.B) {
5858
workload := generateWorkload(totalOps)
5959

6060
// Warmup phase - not measured
61-
for i := 0; i < 50000; i++ {
61+
for i := range 50000 {
6262
key := workload[i]
63-
if _, found, _ := cache.Get(ctx, key); !found {
64-
_ = cache.Set(ctx, key, i, 0)
63+
if _, found, err := cache.Get(ctx, key); err == nil && !found {
64+
if err := cache.Set(ctx, key, i, 0); err != nil {
65+
b.Fatalf("Set failed: %v", err)
66+
}
6567
}
6668
}
6769

@@ -70,14 +72,17 @@ func BenchmarkHitRate_bdcache(b *testing.B) {
7072
misses := 0
7173

7274
b.ResetTimer()
75+
//nolint:intrange // b.N is dynamic and cannot use range
7376
for i := 0; i < b.N; i++ {
7477
key := workload[50000+i]
7578

76-
if _, found, _ := cache.Get(ctx, key); found {
79+
if _, found, err := cache.Get(ctx, key); err == nil && found {
7780
hits++
7881
} else {
7982
misses++
80-
_ = cache.Set(ctx, key, i, 0)
83+
if err := cache.Set(ctx, key, i, 0); err != nil {
84+
b.Fatalf("Set failed: %v", err)
85+
}
8186
}
8287
}
8388
b.StopTimer()
@@ -98,7 +103,7 @@ func BenchmarkHitRate_LRU(b *testing.B) {
98103
workload := generateWorkload(totalOps)
99104

100105
// Warmup phase - not measured
101-
for i := 0; i < 50000; i++ {
106+
for i := range 50000 {
102107
key := workload[i]
103108
if _, found := cache.Get(key); !found {
104109
cache.Add(key, i)
@@ -110,6 +115,7 @@ func BenchmarkHitRate_LRU(b *testing.B) {
110115
misses := 0
111116

112117
b.ResetTimer()
118+
//nolint:intrange // b.N is dynamic and cannot use range
113119
for i := 0; i < b.N; i++ {
114120
key := workload[50000+i]
115121

@@ -143,7 +149,7 @@ func BenchmarkHitRate_ristretto(b *testing.B) {
143149
workload := generateWorkload(totalOps)
144150

145151
// Warmup phase - not measured
146-
for i := 0; i < 50000; i++ {
152+
for i := range 50000 {
147153
key := workload[i]
148154
if _, found := cache.Get(key); !found {
149155
cache.Set(key, i, 1)
@@ -156,6 +162,7 @@ func BenchmarkHitRate_ristretto(b *testing.B) {
156162
misses := 0
157163

158164
b.ResetTimer()
165+
//nolint:intrange // b.N is dynamic and cannot use range
159166
for i := 0; i < b.N; i++ {
160167
key := workload[50000+i]
161168

@@ -181,13 +188,18 @@ func BenchmarkSpeed_bdcache(b *testing.B) {
181188
}
182189

183190
// Pre-populate
184-
for i := 0; i < 1000; i++ {
185-
_ = cache.Set(ctx, i, i, 0)
191+
for i := range 1000 {
192+
if err := cache.Set(ctx, i, i, 0); err != nil {
193+
b.Fatalf("Set failed: %v", err)
194+
}
186195
}
187196

188197
b.ResetTimer()
198+
//nolint:intrange // b.N is dynamic and cannot use range
189199
for i := 0; i < b.N; i++ {
190-
_, _, _ = cache.Get(ctx, i%1000)
200+
if _, _, err := cache.Get(ctx, i%1000); err != nil {
201+
b.Fatalf("Get failed: %v", err)
202+
}
191203
}
192204
}
193205

@@ -199,11 +211,12 @@ func BenchmarkSpeed_LRU(b *testing.B) {
199211
}
200212

201213
// Pre-populate
202-
for i := 0; i < 1000; i++ {
214+
for i := range 1000 {
203215
cache.Add(i, i)
204216
}
205217

206218
b.ResetTimer()
219+
//nolint:intrange // b.N is dynamic and cannot use range
207220
for i := 0; i < b.N; i++ {
208221
_, _ = cache.Get(i % 1000)
209222
}
@@ -222,12 +235,13 @@ func BenchmarkSpeed_ristretto(b *testing.B) {
222235
defer cache.Close()
223236

224237
// Pre-populate
225-
for i := 0; i < 1000; i++ {
238+
for i := range 1000 {
226239
cache.Set(i, i, 1)
227240
}
228241
cache.Wait() // Ristretto sets are async
229242

230243
b.ResetTimer()
244+
//nolint:intrange // b.N is dynamic and cannot use range
231245
for i := 0; i < b.N; i++ {
232246
_, _ = cache.Get(i % 1000)
233247
}
@@ -240,11 +254,12 @@ func BenchmarkSpeed_otter(b *testing.B) {
240254
})
241255

242256
// Pre-populate
243-
for i := 0; i < 1000; i++ {
257+
for i := range 1000 {
244258
cache.Set(i, i)
245259
}
246260

247261
b.ResetTimer()
262+
//nolint:intrange // b.N is dynamic and cannot use range
248263
for i := 0; i < b.N; i++ {
249264
_, _ = cache.GetIfPresent(i % 1000)
250265
}

benchmarks/fifo_vs_lru_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,66 @@ func TestFIFOvsLRU_ScanResistance(t *testing.T) {
2323

2424
// Test S3-FIFO
2525
ctx := context.Background()
26-
s3Cache, _ := bdcache.New[int, int](ctx, bdcache.WithMemorySize(cacheSize))
26+
s3Cache, err := bdcache.New[int, int](ctx, bdcache.WithMemorySize(cacheSize))
27+
if err != nil {
28+
fmt.Printf("Failed to create cache: %v\n", err)
29+
return
30+
}
2731

2832
// Phase 1: Build working set
2933
fmt.Println("Phase 1: Build working set (both caches)")
30-
for i := 0; i < workingSetSize; i++ {
31-
_ = s3Cache.Set(ctx, i, i, 0)
34+
for i := range workingSetSize {
35+
if err := s3Cache.Set(ctx, i, i, 0); err != nil {
36+
fmt.Printf("Set error: %v\n", err)
37+
}
3238
}
3339

3440
// Phase 2: Access working set once (marks as hot in S3-FIFO)
3541
fmt.Println("Phase 2: Access working set (marks as hot)")
36-
for i := 0; i < workingSetSize; i++ {
37-
_, _, _ = s3Cache.Get(ctx, i)
42+
for i := range workingSetSize {
43+
if _, _, err := s3Cache.Get(ctx, i); err != nil {
44+
fmt.Printf("Get error: %v\n", err)
45+
}
3846
}
3947

4048
fmt.Printf(" S3-FIFO after warmup: items=%d\n", s3Cache.Len())
4149

4250
// Phase 3: One-time scan through large dataset
4351
fmt.Println("Phase 3: One-time scan through cold data")
4452
for i := 100000; i < 100000+scanSize; i++ {
45-
_ = s3Cache.Set(ctx, i, i, 0)
53+
if err := s3Cache.Set(ctx, i, i, 0); err != nil {
54+
fmt.Printf("Set error: %v\n", err)
55+
}
4656
}
4757
fmt.Printf(" S3-FIFO after scan: items=%d\n", s3Cache.Len())
4858

4959
// Phase 4: Re-access working set
5060
fmt.Println("Phase 4: Re-access working set")
5161
s3Hits := 0
52-
for i := 0; i < workingSetSize; i++ {
53-
if _, found, _ := s3Cache.Get(ctx, i); found {
62+
for i := range workingSetSize {
63+
if _, found, err := s3Cache.Get(ctx, i); err != nil {
64+
fmt.Printf("Get error: %v\n", err)
65+
} else if found {
5466
s3Hits++
5567
}
5668
}
5769

5870
fmt.Printf(" S3-FIFO hits: %d/%d (%.1f%%)\n", s3Hits, workingSetSize, float64(s3Hits)/float64(workingSetSize)*100)
5971

6072
// Now test LRU with same workload
61-
lruCache, _ := lru.New[int, int](cacheSize)
73+
lruCache, err := lru.New[int, int](cacheSize)
74+
if err != nil {
75+
fmt.Printf("Failed to create LRU cache: %v\n", err)
76+
return
77+
}
6278

6379
// Phase 1: Build working set
64-
for i := 0; i < workingSetSize; i++ {
80+
for i := range workingSetSize {
6581
lruCache.Add(i, i)
6682
}
6783

6884
// Phase 2: Access working set once
69-
for i := 0; i < workingSetSize; i++ {
85+
for i := range workingSetSize {
7086
_, _ = lruCache.Get(i)
7187
}
7288

@@ -77,7 +93,7 @@ func TestFIFOvsLRU_ScanResistance(t *testing.T) {
7793

7894
// Phase 4: Re-access working set
7995
lruHits := 0
80-
for i := 0; i < workingSetSize; i++ {
96+
for i := range workingSetSize {
8197
if _, found := lruCache.Get(i); found {
8298
lruHits++
8399
}

benchmarks/hitrate_comparison_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package benchmarks
33
import (
44
"context"
55
"fmt"
6-
"github.com/codeGROOVE-dev/bdcache"
76
"testing"
87

8+
"github.com/codeGROOVE-dev/bdcache"
9+
910
lru "github.com/hashicorp/golang-lru/v2"
1011
)
1112

@@ -22,7 +23,7 @@ func generateOneHitWonderWorkload(n int) []int {
2223
hotSetSize := 5000 // Fits in cache
2324
oneHitWonderID := 100000
2425

25-
for i := 0; i < n; i++ {
26+
for i := range n {
2627
if i%3 == 0 {
2728
// 33% one-hit wonders - each unique, accessed once
2829
keys[i] = oneHitWonderID
@@ -44,7 +45,7 @@ func generateScanWorkload(n int) []int {
4445
scanSize := 50000 // Large scan that would evict everything in LRU
4546

4647
scanCounter := 0
47-
for i := 0; i < n; i++ {
48+
for i := range n {
4849
if i%100 < 90 {
4950
// 90% working set access
5051
keys[i] = i % workingSet
@@ -65,7 +66,7 @@ func generateLoopWorkload(n int) []int {
6566
loopSize := 6000 // Fits in cache
6667

6768
pollutionID := 200000
68-
for i := 0; i < n; i++ {
69+
for i := range n {
6970
if i%10 < 8 {
7071
// 80% loop through working set
7172
keys[i] = i % loopSize
@@ -93,11 +94,13 @@ func runCacheWorkload(b *testing.B, workload []int, cacheName string) float64 {
9394
}
9495

9596
for _, key := range workload {
96-
if _, found, _ := cache.Get(ctx, key); found {
97+
if _, found, err := cache.Get(ctx, key); err == nil && found {
9798
hits++
9899
} else {
99100
misses++
100-
_ = cache.Set(ctx, key, key, 0)
101+
if err := cache.Set(ctx, key, key, 0); err != nil {
102+
b.Fatalf("Set failed: %v", err)
103+
}
101104
}
102105
}
103106

@@ -185,11 +188,12 @@ func TestHitRateComparison(t *testing.T) {
185188
fmt.Printf("\n%s:\n", name)
186189
fmt.Printf(" bdcache (S3-FIFO): %.2f%%\n", bdcacheRate)
187190
fmt.Printf(" golang-lru (LRU): %.2f%%\n", lruRate)
188-
if diff > 0 {
191+
switch {
192+
case diff > 0:
189193
fmt.Printf(" ✅ bdcache wins by %.2f percentage points\n", diff)
190-
} else if diff < 0 {
194+
case diff < 0:
191195
fmt.Printf(" ❌ LRU wins by %.2f percentage points\n", -diff)
192-
} else {
196+
default:
193197
fmt.Printf(" 🤝 Tie\n")
194198
}
195199
}

cache.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (c *Cache[K, V]) warmup(ctx context.Context) {
7979

8080
loaded := 0
8181
for entry := range entryCh {
82-
c.memory.set(entry.Key, entry.Value, entry.Expiry)
82+
c.memory.setToMemory(entry.Key, entry.Value, entry.Expiry)
8383
loaded++
8484
}
8585

@@ -99,9 +99,11 @@ func (c *Cache[K, V]) warmup(ctx context.Context) {
9999

100100
// Get retrieves a value from the cache.
101101
// It first checks the memory cache, then falls back to persistence if available.
102+
//
103+
//nolint:gocritic // unnamedResult - public API signature is intentionally clear without named returns
102104
func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
103105
// Check memory first
104-
if val, ok := c.memory.get(key); ok {
106+
if val, ok := c.memory.getFromMemory(key); ok {
105107
return val, true, nil
106108
}
107109

@@ -131,7 +133,7 @@ func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
131133
}
132134

133135
// Add to memory cache for future hits
134-
c.memory.set(key, val, expiry)
136+
c.memory.setToMemory(key, val, expiry)
135137

136138
return val, true, nil
137139
}
@@ -157,7 +159,7 @@ func (c *Cache[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration
157159
}
158160

159161
// ALWAYS update memory first - reliability guarantee
160-
c.memory.set(key, value, expiry)
162+
c.memory.setToMemory(key, value, expiry)
161163

162164
// Update persistence if available
163165
if c.persist != nil {
@@ -170,9 +172,11 @@ func (c *Cache[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration
170172
}
171173

172174
// Delete removes a value from the cache.
175+
//
176+
//nolint:revive // confusing-naming - standard cache operation
173177
func (c *Cache[K, V]) Delete(ctx context.Context, key K) {
174178
// Remove from memory
175-
c.memory.delete(key)
179+
c.memory.deleteFromMemory(key)
176180

177181
// Remove from persistence if available
178182
if c.persist != nil {
@@ -190,16 +194,20 @@ func (c *Cache[K, V]) Delete(ctx context.Context, key K) {
190194

191195
// Cleanup removes expired entries from the cache.
192196
// Returns the number of entries removed.
197+
//
198+
//nolint:revive // confusing-naming - standard cache operation
193199
func (c *Cache[K, V]) Cleanup() int {
194-
return c.memory.cleanup()
200+
return c.memory.cleanupMemory()
195201
}
196202

197203
// Len returns the number of items in the memory cache.
198204
func (c *Cache[K, V]) Len() int {
199-
return c.memory.len()
205+
return c.memory.memoryLen()
200206
}
201207

202208
// Close releases resources held by the cache.
209+
//
210+
//nolint:revive // confusing-naming - standard cache operation
203211
func (c *Cache[K, V]) Close() error {
204212
if c.persist != nil {
205213
if err := c.persist.Close(); err != nil {

0 commit comments

Comments
 (0)