goburrow / cache

Mango Cache 🥭 - Partial implementation of Guava Cache in Go (golang).
BSD 3-Clause "New" or "Revised" License
584 stars 48 forks source link

Should reloader load in a go routine or not? #32

Open juvenn opened 2 years ago

juvenn commented 2 years ago
// refreshAsync reloads value in a go routine or using custom executor if defined.
func (c *localCache) refreshAsync(en *entry) bool {
    if en.setLoading(true) {
        // Only do refresh if it isn't running.
        if c.reloader == nil {
            go c.refresh(en)
        } else {
            c.reload(en)
        }
        return true
    }
    return false
}

Inspecting the code, I find that the loader refresh in a new go routine, while reloader reload in the current routine. Why the difference? Should we define the loader load in new go routine, or current routine?

nqv commented 2 years ago

reloader is a user-defined way to refresh cache entries, e.g. using a pool to avoid creating a new goroutine for each refresh. loader is a cache loader function, must be synchronous.