Closed renatomariscal closed 7 months ago
Perhaps SuppressedLoader might be something you're looking for?
🤦🏻♂️ Thanks @swithek, I had skimmed over the documentation, and had assumed by the name this would suppress the loader on some scenarios.
@swithek Can you please guide me on this? Thanks I must have missed something
Something like this should work:
func main() {
loader := ttlcache.LoaderFunc[string, string](
func(c *ttlcache.Cache[string, string], key string) *ttlcache.Item[string, string] {
item := c.Set("key from file", "value from file") // create/set an item
return item // pass it through the cache - it won't be set again
},
)
cache := ttlcache.New[string, string](
ttlcache.WithLoader[string, string](ttlcache.NewSuppressedLoader(loader, nil)),
)
item := cache.Get("key from file")
}
@swithek but the official (internal) constructor of the Item constructor, has a touch
on its initialization, won't it cause some trouble with the internal state to be missing that, such as by being considered expired right away?
https://github.com/jellydator/ttlcache/blob/6c6fce49fcd399ef4bb1f0b6cc14aee7c09ed4cf/item.go#L50
touch()
in that case only initialises the expiration date (if enabled), it doesn't do any additional processing and the item that is returned from the loader is immediately returned from the Get()
method as well:
https://github.com/jellydator/ttlcache/blob/v3/cache.go#L229
Thanks @swithek ! Via Set function. As every loader must insert the item to cache. It would be good to have a better name. Hdyt? We can probably improve our implementation later @renatomariscal
As every loader must insert the item to cache. It would be good to have a better name.
Can you explain this more?
The Loader function is called on each cache miss:
but there is cached signal that such key is already being loaded.
If the resource behind the loader is overwhelmed and slowing down, it will increase the parallelism on these scenarios, which increases the load, therefore making it worse (cascading failure).
This scenario is most likely on a cold-start.
Diagram showing 2 parallel loads of the same key, it could be
N
:If the cache would internally hold a sort of promise: