karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.28k stars 119 forks source link

About Fetch #32

Closed rambutan-soft closed 4 years ago

rambutan-soft commented 5 years ago

item, err := cache.Fetch("user:4", time.Minute 10, func() (interface{}, error) { //code to fetch the data incase of a miss //should return the data to cache and the error, if any }) the function inside Fetch, currently cant accept parameters, can we let the function use key as parameter, so we can rebuild cache if not find value by key, something like that: item, err := cache.Fetch("user:4", time.Minute 10, func("user:4") (interface{}, error) { //get value from db by id=user:4 //return value from db }) Nice to have this function.

karlseguin commented 5 years ago

Is there a reason you can't just close over the value?

key := "user:4"
item, err := cache.Fetch(key, time.Minute * 10, func() (interface{}, error) {
  //get value from db by id=key
})