diff options
-rw-r--r-- | lib/httpcache/httpcache.go | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/lib/httpcache/httpcache.go b/lib/httpcache/httpcache.go index aded85c..50fb771 100644 --- a/lib/httpcache/httpcache.go +++ b/lib/httpcache/httpcache.go @@ -13,12 +13,12 @@ import ( var UserAgent string -type httpCacheEntry struct { +type CacheEntry struct { Body string Err error } -var httpCache = map[string]httpCacheEntry{} +var memCache = map[string]CacheEntry{} type httpStatusError struct { StatusCode int @@ -54,7 +54,7 @@ func Get(u string, hdr map[string]string) (string, error) { cacheKey += "|" + url.QueryEscape(k) + ":" + url.QueryEscape(hdr[k]) } - if cache, ok := httpCache[cacheKey]; ok { + if cache, ok := memCache[cacheKey]; ok { fmt.Printf("CACHE-GET %q\n", u) return cache.Body, cache.Err } @@ -64,17 +64,17 @@ func Get(u string, hdr map[string]string) (string, error) { cacheFile := filepath.Join(".http-cache", cacheKey) if bs, err := os.ReadFile(cacheFile); err == nil { fmt.Printf("CACHE-GET %q\n", u) - httpCache[cacheKey] = httpCacheEntry{Body: string(bs)} - return httpCache[cacheKey].Body, nil + memCache[cacheKey] = CacheEntry{Body: string(bs)} + return memCache[cacheKey].Body, nil } else if !os.IsNotExist(err) { - httpCache[cacheKey] = httpCacheEntry{Err: err} + memCache[cacheKey] = CacheEntry{Err: err} return "", err } fmt.Printf("GET %q...", u) req, err := http.NewRequest(http.MethodGet, u, nil) if err != nil { - httpCache[cacheKey] = httpCacheEntry{Err: err} + memCache[cacheKey] = CacheEntry{Err: err} return "", err } req.Header.Set("User-Agent", UserAgent) @@ -84,27 +84,27 @@ func Get(u string, hdr map[string]string) (string, error) { resp, err := http.DefaultClient.Do(req) if err != nil { fmt.Printf(" err\n") - httpCache[cacheKey] = httpCacheEntry{Err: err} + memCache[cacheKey] = CacheEntry{Err: err} return "", err } if resp.StatusCode != http.StatusOK { fmt.Printf(" err\n") - httpCache[cacheKey] = httpCacheEntry{Err: err} + memCache[cacheKey] = CacheEntry{Err: err} return "", &httpStatusError{StatusCode: resp.StatusCode, Status: resp.Status} } bs, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf(" err\n") - httpCache[cacheKey] = httpCacheEntry{Err: err} - httpCache[cacheKey] = httpCacheEntry{Err: err} + memCache[cacheKey] = CacheEntry{Err: err} + memCache[cacheKey] = CacheEntry{Err: err} return "", err } fmt.Printf(" ok\n") if err := os.WriteFile(cacheFile, bs, 0666); err != nil { return "", err } - httpCache[cacheKey] = httpCacheEntry{Body: string(bs)} - return httpCache[cacheKey].Body, nil + memCache[cacheKey] = CacheEntry{Body: string(bs)} + return memCache[cacheKey].Body, nil } func GetJSON(u string, hdr map[string]string, out any) error { |