-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
SuppressedLoader should allow setting group
#86
Comments
Yes, you are absolutely right! I'm just wondering what would be the best approach to fix this. I think to perform a lazy set of So I'd say we have two options:
|
I think I prefer the second option; however, I'd request The reason I think this is desirable is because I am following your suggestion here #74 (comment). I want to be able to do loader := NewSuppressedLoader[K, V](...)
cache.Get("key", ttlcache.WithLoader[K, V](loader)) sharing the same |
Would the inner loader wrapped by the suppressed loader be also the same in all these places? If so, you could just reuse the same suppressed loader, e.g., attach it to the service that uses the cache: type Service struct {
cache *ttlcache.Cache
loader ttlcache.Loader
}
func New() *Service {
return Service{
cache: ttlcache.New(),
loader: ttlcache.NewSuppressedLoader(...),
}
}
func (s *Service) Get1() {
item := s.cache.Get("key", ttlcache.WithLoader(s.loader))
}
func (s *Service) Get2() {
item := s.cache.Get("key", ttlcache.WithLoader(s.loader))
} |
I do something like this, so I don't believe I can share the loader: type Service struct {
cache *ttlcache.Cache
group *singleflight.Group
}
func (s *Service) Get(ctx context.Context, key K) V {
var loaded V
loader := func(cache *ttlcache.Cache, key K) *ttlcache.Item {
// 1. Make an API call, passing ctx.
// 2. Set loaded to the API result.
// 3. Update the cache if the API result can be cached; otherwise, return nil.
}
loaderFunc := ttlcache.LoaderFunc(loader)
suppressedLoader := SuppressedLoader{
Loader: loaderFunc,
group: group,
}
if cached := s.cache.Get(key, ttlcache.WithLoader(suppressedLoader)); cached != nil {
return cached.Value()
}
return loaded
} |
I see. In that case, the required changes are as follows:
func NewSuppressedLoader(loader Loader, group *singleflight.Group) *SuppressedLoader {
if group == nil {
group = &singleflight.Group{}
}
return &SuppressedLoader{
loader: loader,
group: group,
}
} Have I missed anything? |
No, that sounds great! 🙌🏼 |
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.
I just ran into this same issue! I love the SuppressedLoader, but it's not in the latest release. Any chance we can get a new release to include this fix? |
Yep, you can find it here. Sorry for the delay. |
Awesome, thanks for the quick response! |
SuppressedLoader looks useful! Unfortunately,
group
is private. So if I just create a SuppressedLoader from an existing Loader and try to use it, I get an "invalid memory address or nil pointer dereference" error.I have had to copy SuppressedLoader into my own code where I can set
group
myself in order to workaround this.Ideas
group
?group
to non-nil?The text was updated successfully, but these errors were encountered: