Auxiliary data with the cache so that LoaderFunc can be versatile
kosmas-valianos opened this issue · 3 comments
Defining a custom loader is great however its usability is pretty low as you have no access to any user_data
that most of the time you need in order to let's say do HTTP(s) requests, file reads etc. This could be solved by being able to add a whatever user_data
object when you are creating the ttlcache.Cache
object. Exposing this user_data
object to the loader along with the key will give the ability to create really powerful loader functions.
This auxiliary object should/could be available of course in the other callback functions as well: OnInsertion
, OnEviction
Can you provide an example that shows that having this feature in the library would be useful?
Since Loader
is an interface, you can already pass the user_data
into the implementation yourself:
type CustomLoader[K comparable, V any] struct {
client *http.Client
}
func (c *CustomLoader[K, V]) Load(c *ttlcache.Cache[K, V], key K) *ttlcache.Item[K, V] {
// use c.client ...
// return an item
}
cache := ttlcache.New[string, string](
ttlcache.WithLoader[string, string](&CustomLoader{
// client: ...
}),
)
Right of course as Loader
is an interface. It is even stated in the Readme
a custom or existing implementation of ttlcache.Loader can be used
👌