Skip to content

Commit ce472e9

Browse files
committed
Remove the bool return from GetOrCreate
1 parent ac54fac commit ce472e9

File tree

3 files changed

+47
-61
lines changed

3 files changed

+47
-61
lines changed

‎.github/workflows/test.yml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ jobs:
1818
- name: Install staticcheck
1919
run: go install honnef.co/go/tools/cmd/staticcheck@latest
2020
shell: bash
21-
- name: Install golint
22-
run: go install golang.org/x/lint/golint@latest
23-
shell: bash
2421
- name: Update PATH
2522
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
2623
shell: bash
@@ -34,8 +31,6 @@ jobs:
3431
run: go vet ./...
3532
- name: Staticcheck
3633
run: staticcheck ./...
37-
- name: Lint
38-
run: golint ./...
3934
- name: Test
4035
run: go test -race ./... -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic
4136
- name: Upload coverage

‎lazycache.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ func (c *Cache[K, V]) Get(key K) (V, bool) {
8888
// it is not called with the cache lock held.
8989
// Note that any error returned by create will be returned by GetOrCreate and repeated calls with the same key will
9090
// receive the same error.
91-
func (c *Cache[K, V]) GetOrCreate(key K, create func(key K) (V, error)) (V, bool, error) {
91+
func (c *Cache[K, V]) GetOrCreate(key K, create func(key K) (V, error)) (V, error) {
9292
c.mu.Lock()
9393
w := c.get(key)
9494
if w != nil {
9595
c.mu.Unlock()
9696
w.wait()
9797
// If w.ready is nil, we will repeat any error from the create function to concurrent callers.
98-
return w.value, w.found, w.err
98+
return w.value, w.err
9999
}
100100

101101
w = &valueWrapper[V]{
@@ -117,9 +117,9 @@ func (c *Cache[K, V]) GetOrCreate(key K, create func(key K) (V, error)) (V, bool
117117

118118
if err != nil {
119119
c.Delete(key)
120-
return c.zerov, false, err
120+
return c.zerov, err
121121
}
122-
return v, true, nil
122+
return v, nil
123123
}
124124

125125
// Resize changes the cache size and returns the number of entries evicted.

‎lazycache_test.go‎

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
255250
func 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

Comments
 (0)