Golang cache result from handler net/http [closed]
Make a global variable and store the cache there. Example:
var globalCache *cache.Cache
func main() {
globalCache = cache.New(5*time.Minute, 10*time.Minute)
http.HandleFunc("/", HelloHandler)
http.ListenAndServe(":8080", nil)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
// use globalCache
}
Update: atomic is not needed since cache.Cache makes locks internally. Thank you for correcting me.