@@ -87,7 +87,7 @@ func TestDeleteFunc(t *testing.T) {
8787 go func () {
8888 defer wg .Done ()
8989 for i := 10 ; i < 30 ; i ++ {
90- v , _ , err := cache .GetOrCreate (i , func (key int ) (any , error ) {
90+ v , err := cache .GetOrCreate (i , func (key int ) (any , error ) {
9191 if key % 2 == 0 {
9292 return nil , errors .New ("failed" )
9393 }
@@ -124,8 +124,7 @@ func TestGetOrCreate(t *testing.T) {
124124 return fmt .Sprintf ("value-%d-%d" , key , counter ), nil
125125 }
126126 for i := 0 ; i < 3 ; i ++ {
127- res , found , err := cache .GetOrCreate (123456 , create )
128- c .Assert (found , qt .IsTrue , qt .Commentf ("iteration %d" , i ))
127+ res , err := cache .GetOrCreate (123456 , create )
129128 c .Assert (err , qt .IsNil )
130129 c .Assert (res , qt .Equals , "value-123456-1" )
131130 }
@@ -141,8 +140,7 @@ func TestGetOrCreateError(t *testing.T) {
141140 return nil , fmt .Errorf ("failed" )
142141 }
143142
144- res , found , err := cache .GetOrCreate (123456 , create )
145- c .Assert (found , qt .IsFalse )
143+ res , err := cache .GetOrCreate (123456 , create )
146144 c .Assert (err , qt .ErrorMatches , "failed" )
147145 c .Assert (res , qt .IsNil )
148146
@@ -173,8 +171,7 @@ func TestGetOrCreateConcurrent(t *testing.T) {
173171 go func () {
174172 defer wg .Done ()
175173 for j := 0 ; j < 12 ; j ++ {
176- res , found , err := cache .GetOrCreate (i , create )
177- c .Assert (found , qt .IsTrue )
174+ res , err := cache .GetOrCreate (i , create )
178175 c .Assert (err , qt .IsNil )
179176 c .Assert (res , qt .Equals , expect )
180177 }
@@ -224,11 +221,11 @@ func TestGetOrCreateRecursive(t *testing.T) {
224221 key2 ++
225222 }
226223 shouldFail := key1 % 10 == 0
227- v , found , err := cache .GetOrCreate (key1 , func (key int ) (any , error ) {
224+ v , err := cache .GetOrCreate (key1 , func (key int ) (any , error ) {
228225 if shouldFail {
229226 return nil , fmt .Errorf ("failed" )
230227 }
231- v , _ , err := cache .GetOrCreate (key2 , func (key int ) (any , error ) {
228+ v , err := cache .GetOrCreate (key2 , func (key int ) (any , error ) {
232229 return "inner" , nil
233230 })
234231 c .Assert (err , qt .IsNil )
@@ -238,10 +235,8 @@ func TestGetOrCreateRecursive(t *testing.T) {
238235 if shouldFail {
239236 c .Assert (err , qt .ErrorMatches , "failed" )
240237 c .Assert (v , qt .IsNil )
241- c .Assert (found , qt .IsFalse )
242238 } else {
243239 c .Assert (err , qt .IsNil )
244- c .Assert (found , qt .IsTrue )
245240 c .Assert (v , qt .Equals , "inner" )
246241 }
247242 }
@@ -255,48 +250,44 @@ func TestGetOrCreateRecursive(t *testing.T) {
255250func BenchmarkGetOrCreateAndGet (b * testing.B ) {
256251 const maxSize = 1000
257252
258- runBenchmark := func (b * testing.B , cache * Cache [int , any ], getOrCreate func (key int , create func (key int ) (any , error )) (any , bool , error )) {
259- r := rand .New (rand .NewSource (99 ))
260- var mu sync.Mutex
253+ cache := New [int , any ](Options {MaxEntries : maxSize })
254+ r := rand .New (rand .NewSource (99 ))
255+ var mu sync.Mutex
256+ // Partially fill the cache.
257+ for i := 0 ; i < maxSize / 2 ; i ++ {
258+ cache .Set (i , i )
259+ }
260+ b .ResetTimer ()
261261
262- b .RunParallel (func (pb * testing.PB ) {
263- // Partially fill the cache.
264- for i := 0 ; i < maxSize / 2 ; i ++ {
265- cache .Set (i , i )
266- }
267- b .ResetTimer ()
268- for pb .Next () {
269- mu .Lock ()
270- i1 , i2 := r .Intn (maxSize ), r .Intn (maxSize )
271- mu .Unlock ()
272- // Just Get the value.
273- v , found := cache .Get (i1 )
274- if found && v != i1 {
275- b .Fatalf ("got %v, want %v" , v , i1 )
276- }
262+ b .RunParallel (func (pb * testing.PB ) {
277263
278- res2 , found , err := getOrCreate (i2 , func (key int ) (any , error ) {
279- if i2 % 100 == 0 {
280- // Simulate a slow create.
281- time .Sleep (1 * time .Second )
282- }
283- return i2 , nil
284- })
264+ b .ResetTimer ()
265+ for pb .Next () {
266+ mu .Lock ()
267+ i1 , i2 := r .Intn (maxSize ), r .Intn (maxSize )
268+ mu .Unlock ()
269+ // Just Get the value.
270+ v , found := cache .Get (i1 )
271+ if found && v != i1 {
272+ b .Fatalf ("got %v, want %v" , v , i1 )
273+ }
285274
286- if err != nil {
287- b .Fatal (err )
275+ res2 , err := cache .GetOrCreate (i2 , func (key int ) (any , error ) {
276+ if i2 % 100 == 0 {
277+ // Simulate a slow create.
278+ time .Sleep (1 * time .Second )
288279 }
280+ return i2 , nil
281+ })
289282
290- if v := res2 ; ! found || v != i2 {
291- b .Fatalf ("got %v, want %v" , v , i2 )
292- }
283+ if err != nil {
284+ b .Fatal (err )
293285 }
294- })
295- }
296286
297- b .Run ("Real" , func (b * testing.B ) {
298- cache := New [int , any ](Options {MaxEntries : maxSize })
299- runBenchmark (b , cache , cache .GetOrCreate )
287+ if v := res2 ; ! found || v != i2 {
288+ b .Fatalf ("got %v, want %v" , v , i2 )
289+ }
290+ }
300291 })
301292
302293}
@@ -318,23 +309,23 @@ func BenchmarkGetOrCreate(b *testing.B) {
318309 b .RunParallel (func (pb * testing.PB ) {
319310 for pb .Next () {
320311 mu .Lock ()
321- i2 := r .Intn (maxSize )
312+ key := r .Intn (maxSize )
322313 mu .Unlock ()
323314
324- res2 , found , err := cache .GetOrCreate (i2 , func (key int ) (any , error ) {
325- if i2 % 100 == 0 {
315+ v , err := cache .GetOrCreate (key , func (int ) (any , error ) {
316+ if key % 100 == 0 {
326317 // Simulate a slow create.
327318 time .Sleep (1 * time .Second )
328319 }
329- return i2 , nil
320+ return key , nil
330321 })
331322
332323 if err != nil {
333324 b .Fatal (err )
334325 }
335326
336- if v := res2 ; ! found || v != i2 {
337- b .Fatalf ("got %v, want %v" , v , i2 )
327+ if v != key {
328+ b .Fatalf ("got %v, want %v" , v , key )
338329 }
339330 }
340331 })
0 commit comments