Closed Michael-Girma closed 1 month ago
The values returned from the loadFunc
are sent to the setChannel
instead of being directly set in the cache. The values in setChannel
are iterated over in a separate goroutine and then set to the cache.
// Unable to find in cache, try to load it from load function
object, err = c.loadFunc(key)
if err != nil {
return object, err
}
// Then, put it back in cache
c.setChannel <- &loadableKeyValue{key, object}
Therefore, you need to wait a bit before calling the second Get
operation.
cacheClient := gocache.New(6*time.Hour, 12*time.Hour)
cacheStore := store.NewGoCache(cacheClient, &store.Options{Expiration: 6 * time.Hour})
mockCache := cache.NewLoadable(
func(cacheApiKey interface{}) (interface{}, error) {
fmt.Printf("Not in cache. Going to Datasource")
// mock database response
return "value", nil
},
cacheStore)
mockCache.Get("Key")
time.Sleep(time.Second / 10)
mockCache.Get("Key")
Thank you for clarifying! time.Sleep made it work!
I used gocache from github.com/patrickmn/go-cache and created a store. I then created a new loadable from that store with a LoadFunction to fetch the value if it's not in the cache already. The function seems to work fine but upon retrieval of the data element, it is not automatically stored in the store. Here is a small snippet for further investigation:
Steps for Reproduction
Expected behavior: Expected output would only have a single log
Actual behavior: Multiple logs indicating that fetched value isn't saved
Platforms: uname -a: Linux 6.8.0-36-generic #36-Ubuntu SMP PREEMPT_DYNAMIC Mon Jun 10 10:49:14 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
Include browser, operating system and respective versions
Versions: