Skip to content

Commit 425cd6d

Browse files
author
ellie
committed
more fixes
1 parent 075bdd9 commit 425cd6d

9 files changed

Lines changed: 550 additions & 413 deletions

cache.go

Lines changed: 39 additions & 343 deletions
Large diffs are not rendered by default.

cache_benchmark_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func BenchmarkGetMiss(b *testing.B) {
1313
type V int
1414

1515
matrixBenchmark(b,
16-
loadingcache.CacheOptions[K, V]{},
16+
nil,
1717
noopBenchmarkSetupFunc[K, V](),
1818
func(b *testing.B, cache loadingcache.Cache[K, V]) {
1919
for i := 0; i < b.N; i++ {
@@ -27,7 +27,7 @@ func BenchmarkGetHit(b *testing.B) {
2727
type V string
2828

2929
matrixBenchmark(b,
30-
loadingcache.CacheOptions[K, V]{},
30+
nil,
3131
func(b *testing.B, cache loadingcache.Cache[K, V]) {
3232
cache.Put(1, "a")
3333
},
@@ -46,7 +46,7 @@ func BenchmarkPutNew(b *testing.B) {
4646
type V int
4747

4848
matrixBenchmark(b,
49-
loadingcache.CacheOptions[K, V]{},
49+
nil,
5050
noopBenchmarkSetupFunc[K, V](),
5151
func(b *testing.B, cache loadingcache.Cache[K, V]) {
5252
for i := 0; i < b.N; i++ {
@@ -60,7 +60,9 @@ func BenchmarkPutNewNoPreWrite(b *testing.B) {
6060
type V int
6161

6262
matrixBenchmark(b,
63-
loadingcache.CacheOptions[K, V]{BackgroundEvictFrequency: time.Second},
63+
[]loadingcache.CacheOption[K, V]{
64+
loadingcache.WithBackgroundEvictFrequency[K, V](time.Second),
65+
},
6466
noopBenchmarkSetupFunc[K, V](),
6567
func(b *testing.B, cache loadingcache.Cache[K, V]) {
6668
for i := 0; i < b.N; i++ {
@@ -70,7 +72,7 @@ func BenchmarkPutNewNoPreWrite(b *testing.B) {
7072
}
7173

7274
func BenchmarkPutReplace(b *testing.B) {
73-
cache := loadingcache.New(loadingcache.CacheOptions[string, int]{})
75+
cache := loadingcache.New[string, int]()
7476
cache.Put("a", 1)
7577
b.ResetTimer()
7678
for i := 0; i < b.N; i++ {
@@ -79,9 +81,9 @@ func BenchmarkPutReplace(b *testing.B) {
7981
}
8082

8183
func BenchmarkPutAtMaxSize(b *testing.B) {
82-
cache := loadingcache.New(loadingcache.CacheOptions[int, int]{
83-
MaxSize: 1,
84-
})
84+
cache := loadingcache.New(
85+
loadingcache.WithMaxSize[int, int](1),
86+
)
8587
b.ResetTimer()
8688
for i := 0; i < b.N; i++ {
8789
cache.Put(i, 1)

cache_examples_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func ExampleCache_simpleUsage() {
1414
type Key string
1515
type Value int
1616

17-
cache := loadingcache.New[Key, Value](loadingcache.CacheOptions[Key, Value]{})
17+
cache := loadingcache.New[Key, Value]()
1818

1919
// Addign some values and reading them
2020
cache.Put("a", 1)
@@ -45,20 +45,20 @@ func ExampleCache_advancedUsage() {
4545
type Key int
4646
type Value string
4747

48-
cache := loadingcache.New[Key, Value](loadingcache.CacheOptions[Key, Value]{
49-
MaxSize: 2,
50-
ExpireAfterRead: 2 * time.Minute,
51-
ExpireAfterWrite: time.Minute,
52-
RemovalListeners: []loadingcache.RemovalListener[Key, Value]{
48+
cache := loadingcache.New(
49+
loadingcache.WithMaxSize[Key, Value](2),
50+
loadingcache.WithExpireAfterRead[Key, Value](2*time.Minute),
51+
loadingcache.WithExpireAfterWrite[Key, Value](time.Minute),
52+
loadingcache.WithRemovalListeners(
5353
func(notification loadingcache.RemovalNotification[Key, Value]) {
5454
fmt.Printf("Entry removed due to %s\n", notification.Reason)
5555
},
56-
},
57-
Load: func(_ context.Context, key Key) (Value, error) {
56+
),
57+
loadingcache.WithLoadFunc(func(_ context.Context, key Key) (Value, error) {
5858
fmt.Printf("Loading key %v\n", key)
5959
return Value(fmt.Sprint(key)), nil
60-
},
61-
})
60+
}),
61+
)
6262

6363
cache.Put(1, "1")
6464
val1, _ := cache.Get(context.TODO(), 1)

cache_test.go

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func TestExpireAfterWrite(t *testing.T) {
7676
type V int
7777

7878
matrixTest[K, V](t, matrixTestOptions[K, V]{
79-
cacheOptions: loadingcache.CacheOptions[K, V]{
80-
ExpireAfterWrite: time.Minute,
79+
cacheOptions: []loadingcache.CacheOption[K, V]{
80+
loadingcache.WithExpireAfterWrite[K, V](time.Minute),
8181
},
8282
},
8383
func(t *testing.T, ctx context.Context, cache loadingcache.Cache[K, V]) {
@@ -108,8 +108,8 @@ func TestExpireAfterRead(t *testing.T) {
108108
type V int
109109

110110
matrixTest(t, matrixTestOptions[K, V]{
111-
cacheOptions: loadingcache.CacheOptions[K, V]{
112-
ExpireAfterRead: time.Minute,
111+
cacheOptions: []loadingcache.CacheOption[K, V]{
112+
loadingcache.WithExpireAfterRead[K, V](time.Minute),
113113
},
114114
},
115115
func(t *testing.T, ctx context.Context, cache loadingcache.Cache[K, V]) {
@@ -150,8 +150,8 @@ func TestLoadFunc(t *testing.T) {
150150

151151
loadFunc := &testLoadFunc[K, V]{}
152152
matrixTest(t, matrixTestOptions[K, V]{
153-
cacheOptions: loadingcache.CacheOptions[K, V]{
154-
Load: loadFunc.LoadFunc,
153+
cacheOptions: []loadingcache.CacheOption[K, V]{
154+
loadingcache.WithLoadFunc(loadFunc.LoadFunc),
155155
},
156156
},
157157
func(t *testing.T, _ context.Context, cache loadingcache.Cache[K, V]) {
@@ -190,14 +190,14 @@ func TestMaxSize(t *testing.T) {
190190
type V int
191191
// TODO MaxSize is currently not properly enforced in a sharded environment
192192
caches := []loadingcache.Cache[K, V]{
193-
loadingcache.New(loadingcache.CacheOptions[K, V]{
194-
MaxSize: 1,
195-
}),
196-
loadingcache.New(loadingcache.CacheOptions[K, V]{
197-
MaxSize: 1,
198-
ShardCount: 3,
199-
HashCodeFunc: stringHashCodeFunc[K](),
200-
}),
193+
loadingcache.New(
194+
loadingcache.WithMaxSize[K, V](1),
195+
),
196+
loadingcache.New(
197+
loadingcache.WithMaxSize[K, V](1),
198+
loadingcache.WithShardCount[K, V](3),
199+
loadingcache.WithHashCodeFunc[K, V](stringHashCodeFunc[K]()),
200+
),
201201
}
202202
for _, cache := range caches {
203203
// With a capacity of one element, adding a second element
@@ -223,11 +223,14 @@ func TestRemovalListeners(t *testing.T) {
223223
removalListener := &testRemovalListener[K, V]{}
224224
removalListener2 := &testRemovalListener[K, V]{}
225225
matrixTest(t, matrixTestOptions[K, V]{
226-
cacheOptions: loadingcache.CacheOptions[K, V]{
227-
ExpireAfterRead: time.Minute,
228-
ExpireAfterWrite: 2 * time.Minute,
229-
MaxSize: 1,
230-
RemovalListeners: []loadingcache.RemovalListener[K, V]{removalListener.Listener, removalListener2.Listener},
226+
cacheOptions: []loadingcache.CacheOption[K, V]{
227+
loadingcache.WithExpireAfterRead[K, V](time.Minute),
228+
loadingcache.WithExpireAfterWrite[K, V](2 * time.Minute),
229+
loadingcache.WithMaxSize[K, V](1),
230+
loadingcache.WithRemovalListeners(
231+
removalListener.Listener,
232+
removalListener2.Listener,
233+
),
231234
},
232235
},
233236
func(t *testing.T, ctx context.Context, cache loadingcache.Cache[K, V]) {
@@ -295,14 +298,16 @@ func TestBackgroudEvict(t *testing.T) {
295298

296299
var removalWg sync.WaitGroup
297300
matrixTest(t, matrixTestOptions[K, V]{
298-
cacheOptions: loadingcache.CacheOptions[K, V]{
299-
ExpireAfterWrite: 20 * time.Second,
300-
BackgroundEvictFrequency: 10 * time.Second,
301-
RemovalListeners: []loadingcache.RemovalListener[K, V]{func(notification loadingcache.RemovalNotification[K, V]) {
302-
if notification.Reason == loadingcache.RemovalReasonExpired {
303-
removalWg.Done()
304-
}
305-
}},
301+
cacheOptions: []loadingcache.CacheOption[K, V]{
302+
loadingcache.WithExpireAfterWrite[K, V](20 * time.Second),
303+
loadingcache.WithBackgroundEvictFrequency[K, V](10 * time.Second),
304+
loadingcache.WithRemovalListeners(
305+
func(notification loadingcache.RemovalNotification[K, V]) {
306+
if notification.Reason == loadingcache.RemovalReasonExpired {
307+
removalWg.Done()
308+
}
309+
},
310+
),
306311
},
307312
},
308313
func(t *testing.T, ctx context.Context, cache loadingcache.Cache[K, V]) {
@@ -336,11 +341,11 @@ func TestTombstone(t *testing.T) {
336341
type V string
337342

338343
matrixTest(t, matrixTestOptions[K, V]{
339-
cacheOptions: loadingcache.CacheOptions[K, V]{
340-
Load: func(ctx context.Context, k K) (V, error) {
344+
cacheOptions: []loadingcache.CacheOption[K, V]{
345+
loadingcache.WithLoadFunc(func(ctx context.Context, k K) (V, error) {
341346
var Nil V
342347
return Nil, loadingcache.Errorf(30*time.Second, "testing error lol!")
343-
},
348+
}),
344349
},
345350
},
346351
func(t *testing.T, ctx context.Context, cache loadingcache.Cache[K, V]) {

cache_utils_test.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ func uintHashCodeFunc[Key ~int]() func(k Key) uint64 {
5353
}
5454

5555
func matrixBenchmark[K ~int, V any](b *testing.B,
56-
options loadingcache.CacheOptions[K, V],
56+
options []loadingcache.CacheOption[K, V],
5757
setupFunc matrixBenchmarkSetupFunc[K, V],
5858
testFunc matrixBenchmarkFunc[K, V],
5959
) {
6060
matrixOptions := cacheMatrixOptions(options)
6161
b.ResetTimer()
6262
for name := range matrixOptions {
63-
cache := loadingcache.New(matrixOptions[name])
63+
cache := loadingcache.New(matrixOptions[name]...)
6464
setupFunc(b, cache)
6565
b.Run(name, func(b *testing.B) {
6666
b.ResetTimer()
@@ -83,13 +83,14 @@ func matrixTest[K ~int, V any](t *testing.T, options matrixTestOptions[K, V], te
8383
for name := range matrixOptions {
8484
utils := &matrixTestUtils{}
8585
cacheOptions := matrixOptions[name]
86-
if cacheOptions.Clock == nil {
87-
mockClock := clock.NewMock()
88-
utils.clock = mockClock
89-
cacheOptions.Clock = mockClock
90-
}
86+
mockClock := clock.NewMock()
87+
utils.clock = mockClock
88+
// Place our clock first, allowing the user to override if necessary
89+
cacheOptions = append([]loadingcache.CacheOption[K, V]{
90+
loadingcache.WithClock[K, V](mockClock),
91+
}, cacheOptions...)
9192
ctx := put(context.Background(), utils)
92-
cache := loadingcache.New(cacheOptions)
93+
cache := loadingcache.New(cacheOptions...)
9394
if options.setupFunc != nil {
9495
options.setupFunc(t, cache)
9596
}
@@ -101,7 +102,7 @@ func matrixTest[K ~int, V any](t *testing.T, options matrixTestOptions[K, V], te
101102
}
102103

103104
type matrixTestOptions[K comparable, V any] struct {
104-
cacheOptions loadingcache.CacheOptions[K, V]
105+
cacheOptions []loadingcache.CacheOption[K, V]
105106
setupFunc func(t *testing.T, cache loadingcache.Cache[K, V])
106107
}
107108

@@ -125,17 +126,21 @@ func get(ctx context.Context) *matrixTestUtils {
125126

126127
type matrixTestFunc[K comparable, V any] func(t *testing.T, ctx context.Context, cache loadingcache.Cache[K, V])
127128

128-
func cacheMatrixOptions[K ~int, V any](baseOptions loadingcache.CacheOptions[K, V]) map[string]loadingcache.CacheOptions[K, V] {
129-
matrix := map[string]loadingcache.CacheOptions[K, V]{}
129+
func cacheMatrixOptions[K ~int, V any](baseOptions []loadingcache.CacheOption[K, V]) map[string][]loadingcache.CacheOption[K, V] {
130+
matrix := map[string][]loadingcache.CacheOption[K, V]{}
130131

131-
simpleOptions := baseOptions
132-
simpleOptions.ShardCount = 1
132+
var simpleOptions []loadingcache.CacheOption[K, V]
133+
simpleOptions = append(simpleOptions, baseOptions...)
134+
simpleOptions = append(simpleOptions, loadingcache.WithShardCount[K, V](1))
133135
matrix["Simple"] = simpleOptions
134136

135137
for _, shardCount := range []int{2, 3, 16, 32} {
136-
shardedOptions := baseOptions
137-
shardedOptions.ShardCount = shardCount
138-
shardedOptions.HashCodeFunc = uintHashCodeFunc[K]()
138+
var shardedOptions []loadingcache.CacheOption[K, V]
139+
shardedOptions = append(shardedOptions, baseOptions...)
140+
shardedOptions = append(shardedOptions,
141+
loadingcache.WithShardCount[K, V](shardCount),
142+
loadingcache.WithHashCodeFunc[K, V](uintHashCodeFunc[K]()),
143+
)
139144
matrix[fmt.Sprintf("Sharded (%d)", shardCount)] = shardedOptions
140145
}
141146
return matrix

0 commit comments

Comments
 (0)