Fixed a race condition and improved cache interface.

This commit is contained in:
2025-08-18 10:29:18 +02:00
parent f44c671052
commit a4ddc43579
3 changed files with 24 additions and 28 deletions

View File

@@ -320,13 +320,13 @@ func GetForecast(
}
}
func GetMoon(res http.ResponseWriter, req *http.Request, cache *types.CacheEntity[types.Moon], vars *types.Variables) {
func GetMoon(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Moon], vars *types.Variables) {
if req.Method != http.MethodGet {
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
return
}
cachedValue, found := cache.GetEntry(vars.TimeToLive)
cachedValue, found := cache.GetEntry(fmtKey("moon"), vars.TimeToLive)
if found {
// Format moon object and then return it
cachedValue.Percentage = fmt.Sprintf("%s%%", cachedValue.Percentage)
@@ -341,7 +341,7 @@ func GetMoon(res http.ResponseWriter, req *http.Request, cache *types.CacheEntit
}
// Add result to cache
cache.AddEntry(moon)
cache.AddEntry(moon, fmtKey("moon"))
// Format moon object and then return it
moon.Percentage = fmt.Sprintf("%s%%", moon.Percentage)