Skip to content

Commit 458637f

Browse files
authored
add supressed loader initialization function
Currently there is no way to use the suppressed loader, as the group attribute is always nil (issue #86). This adds a new function which initializes the structure with the Loader and group parameters, if the group parameter is nil, it creates a new single flight group object.
1 parent 40f09cf commit 458637f

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

cache.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,22 @@ func (l LoaderFunc[K, V]) Load(c *Cache[K, V], key K) *Item[K, V] {
538538
// SuppressedLoader wraps another Loader and suppresses duplicate
539539
// calls to its Load method.
540540
type SuppressedLoader[K comparable, V any] struct {
541-
Loader[K, V]
541+
loader Loader[K, V]
542+
group *singleflight.Group
543+
}
544+
545+
// NewSuppressedLoader creates a new instance of suppressed loader.
546+
// If the group parameter is nil, a newly created instance of
547+
// *singleflight.Group is used.
548+
func NewSuppressedLoader[K comparable, V any](loader Loader[K, V], group *singleflight.Group) *SuppressedLoader[K, V] {
549+
if group == nil {
550+
group = &singleflight.Group{}
551+
}
542552

543-
group *singleflight.Group
553+
return &SuppressedLoader[K, V]{
554+
loader: loader,
555+
group: group,
556+
}
544557
}
545558

546559
// Load executes a custom item retrieval logic and returns the item that
@@ -559,7 +572,7 @@ func (l *SuppressedLoader[K, V]) Load(c *Cache[K, V], key K) *Item[K, V] {
559572
// the error that we return ourselves in the func below, which
560573
// is also nil
561574
res, _, _ := l.group.Do(strKey, func() (interface{}, error) {
562-
item := l.Loader.Load(c, key)
575+
item := l.loader.Load(c, key)
563576
if item == nil {
564577
return nil, nil
565578
}

cache_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,40 @@ func Test_LoaderFunc_Load(t *testing.T) {
945945
assert.True(t, called)
946946
}
947947

948+
func Test_NewSuppressedLoader(t *testing.T) {
949+
var called bool
950+
951+
loader := LoaderFunc[string, string](func(_ *Cache[string, string], _ string) *Item[string, string] {
952+
called = true
953+
return nil
954+
})
955+
956+
// uses the provided loader and group parameters
957+
group := &singleflight.Group{}
958+
959+
sl := NewSuppressedLoader[string, string](loader, group)
960+
require.NotNil(t, sl)
961+
require.NotNil(t, sl.loader)
962+
963+
sl.loader.Load(nil, "")
964+
965+
assert.True(t, called)
966+
assert.Equal(t, group, sl.group)
967+
968+
// uses the provided loader and automatically creates a new instance
969+
// of *singleflight.Group as nil parameter is passed
970+
called = false
971+
972+
sl = NewSuppressedLoader[string, string](loader, nil)
973+
require.NotNil(t, sl)
974+
require.NotNil(t, sl.loader)
975+
976+
sl.loader.Load(nil, "")
977+
978+
assert.True(t, called)
979+
assert.NotNil(t, group, sl.group)
980+
}
981+
948982
func Test_SuppressedLoader_Load(t *testing.T) {
949983
var (
950984
mu sync.Mutex
@@ -954,7 +988,7 @@ func Test_SuppressedLoader_Load(t *testing.T) {
954988
)
955989

956990
l := SuppressedLoader[string, string]{
957-
Loader: LoaderFunc[string, string](func(_ *Cache[string, string], _ string) *Item[string, string] {
991+
loader: LoaderFunc[string, string](func(_ *Cache[string, string], _ string) *Item[string, string] {
958992
mu.Lock()
959993
loadCalls++
960994
mu.Unlock()

0 commit comments

Comments
 (0)