Comments (9)
Yep, you can find it here. Sorry for the delay.
from ttlcache.
No, that sounds great! 🙌🏼
from ttlcache.
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 group
, a new mutex would have to be introduced as well, which might degrade the performance for no real benefit.
So I'd say we have two options:
- Expose
group
, as you said. There would be no breaking changes, but as a user I would expect the library to set this field itself, since there are nosingleflight.Group
options that I could configure. - Create a
NewSuppressedLoader
func that would setgroup
and return a fully working loader. In this case, theLoader
field could also be unexported and set inside the func (a parameter would be accepted for this), although that's a breaking change. On the other hand, since you are the first one to report this, maybe this change, albeit potentially breaking, might not affect that many users, if any at all. This way the design of the package wouldn't be negatively affected by pieces of code that would be left to be fixed in the next major release.
from ttlcache.
I think I prefer the second option; however, I'd request NewSuppressedLoader
to take *singleflight.Group
as an option. If the *singleflight.Group
is nil, NewSuppressedLoader
could create one; otherwise, it could use the provided value.
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 *singleflight.Group
between SuppressedLoaders. WDYT?
from ttlcache.
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))
}
from ttlcache.
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
}
from ttlcache.
I see. In that case, the required changes are as follows:
- Transform the embedded
Loader
into an unexportedloader Loader
field. - Create a
NewSuppressedLoader(loader Loader, group *singleflight.Group)
func. Ifgroup
is nil, it is automatically created:
func NewSuppressedLoader(loader Loader, group *singleflight.Group) *SuppressedLoader {
if group == nil {
group = &singleflight.Group{}
}
return &SuppressedLoader{
loader: loader,
group: group,
}
}
Have I missed anything?
from ttlcache.
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?
from ttlcache.
Awesome, thanks for the quick response!
from ttlcache.
Related Issues (20)
- Way to get expired elements. HOT 2
- v3.0.1 doesn't have cache.GetOrSet method while it's documented in README.md of master branch HOT 5
- v3.0.1 doesn't have cache.Has method while it's documented in README.md of master branch HOT 2
- Question: about memory exceeding the limit HOT 1
- Is it thread safe? HOT 1
- Optimize getOpts allocation in Get() method using sync.Pool HOT 1
- Set without expanding the Expire time HOT 3
- `Range` panics when cache is empty HOT 1
- Feature request: Dedup same-key loader HOT 8
- Protect Range from empty cache HOT 1
- Add `GetContext()` method and `LoaderContext` interface HOT 1
- Lock-per-key functionality when using custom loader functions HOT 2
- Strange issue with OnEviction/OnInsertion hooks
- Proposal: Present cache items in order of oldest to newest
- SetUnsafe like IsExpiredUnsafe HOT 2
- Add an option for cache to not expire until background refresh completes HOT 5
- Mention DefaultTTL and NoTTL in Set() docs HOT 4
- Ensure that GetOrSet and GetAndDelete are properly locked HOT 2
- How to get cache value without "Automatic expiration time extension"? HOT 1
- Add a method to check if a key exists HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ttlcache.