Skip to content

Commit be6ea80

Browse files
committed
update singleflight
1 parent 161616d commit be6ea80

5 files changed

Lines changed: 13 additions & 13 deletions

File tree

_example/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
)
1313

1414
require (
15-
github.com/goware/singleflight v0.2.0 // indirect
15+
github.com/goware/singleflight v0.3.0 // indirect
1616
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
1717
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
1818
github.com/zeebo/xxh3 v1.0.2 // indirect

_example/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
44
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
55
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
66
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
7-
github.com/goware/singleflight v0.2.0 h1:e/hZsvNmbLoiZLx3XbihH01oXYA2MwLFo4e+N017U4c=
8-
github.com/goware/singleflight v0.2.0/go.mod h1:SsAslCMS7HizXdbYcBQRBLC7HcNmFrHutRt3Hz6wovY=
7+
github.com/goware/singleflight v0.3.0 h1:b+OM844fuHzanOlE84WeI+G8YMksUY636v0bdcAfnHE=
8+
github.com/goware/singleflight v0.3.0/go.mod h1:vcmu9KY0BS9WbA3Pn+WOdUQlwT1CPZJm1Fgaz2l88Dc=
99
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
1010
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
1111
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.24.1
66

77
require (
88
github.com/go-chi/cors v1.2.1
9-
github.com/goware/singleflight v0.2.0
9+
github.com/goware/singleflight v0.3.0
1010
github.com/hashicorp/golang-lru/v2 v2.0.7
1111
github.com/stretchr/testify v1.5.1
1212
github.com/zeebo/xxh3 v1.0.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
44
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
5-
github.com/goware/singleflight v0.2.0 h1:e/hZsvNmbLoiZLx3XbihH01oXYA2MwLFo4e+N017U4c=
6-
github.com/goware/singleflight v0.2.0/go.mod h1:SsAslCMS7HizXdbYcBQRBLC7HcNmFrHutRt3Hz6wovY=
5+
github.com/goware/singleflight v0.3.0 h1:b+OM844fuHzanOlE84WeI+G8YMksUY636v0bdcAfnHE=
6+
github.com/goware/singleflight v0.3.0/go.mod h1:vcmu9KY0BS9WbA3Pn+WOdUQlwT1CPZJm1Fgaz2l88Dc=
77
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
88
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
99
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=

stampede.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ type Cache[K comparable, V any] struct {
3636
callGroup singleflight.Group[K, V]
3737
}
3838

39-
func (c *Cache[K, V]) Get(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, error) {
39+
func (c *Cache[K, V]) Get(ctx context.Context, key K, fn func() (V, error)) (V, error) {
4040
return c.get(ctx, key, false, fn)
4141
}
4242

43-
func (c *Cache[K, V]) GetFresh(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, error) {
43+
func (c *Cache[K, V]) GetFresh(ctx context.Context, key K, fn func() (V, error)) (V, error) {
4444
return c.get(ctx, key, true, fn)
4545
}
4646

47-
func (c *Cache[K, V]) Set(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, bool, error) {
47+
func (c *Cache[K, V]) Set(ctx context.Context, key K, fn func() (V, error)) (V, bool, error) {
4848
v, err, shared := c.callGroup.Do(key, c.set(key, fn))
4949
return v, shared, err
5050
}
5151

52-
func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn singleflight.DoFunc[V]) (V, error) {
52+
func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn func() (V, error)) (V, error) {
5353
c.mu.RLock()
5454
val, ok := c.values.Get(key)
5555
c.mu.RUnlock()
@@ -73,8 +73,8 @@ func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn singlef
7373
return v, err
7474
}
7575

76-
func (c *Cache[K, V]) set(key K, fn singleflight.DoFunc[V]) singleflight.DoFunc[V] {
77-
return singleflight.DoFunc[V](func() (V, error) {
76+
func (c *Cache[K, V]) set(key K, fn func() (V, error)) func() (V, error) {
77+
return func() (V, error) {
7878
val, err := fn()
7979
if err != nil {
8080
return val, err
@@ -89,7 +89,7 @@ func (c *Cache[K, V]) set(key K, fn singleflight.DoFunc[V]) singleflight.DoFunc[
8989
c.mu.Unlock()
9090

9191
return val, nil
92-
})
92+
}
9393
}
9494

9595
type value[V any] struct {

0 commit comments

Comments
 (0)