Fixed cache data violation bug

This commit is contained in:
2025-06-18 08:44:52 +02:00
parent 9e419ec7bf
commit 87605024c7
5 changed files with 37 additions and 21 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.vscode/

View File

@@ -47,6 +47,19 @@ func fmtWind(windSpeed string, isImperial bool) string {
return fmt.Sprintf("%.1f km/h", (parsedSpeed * 3.6)) return fmt.Sprintf("%.1f km/h", (parsedSpeed * 3.6))
} }
func deepCopyForecast(original types.Forecast) types.Forecast {
// Copy the outer structure
fc_copy := original
// Allocate enough space
fc_copy.Forecast = make([]types.ForecastEntity, len(original.Forecast))
// Copy inner structure
copy(fc_copy.Forecast, original.Forecast)
return fc_copy
}
func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Weather], vars *types.Variables) { func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Weather], vars *types.Variables) {
if req.Method != http.MethodGet { if req.Method != http.MethodGet {
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed) jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
@@ -202,15 +215,19 @@ func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[
cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive) cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive)
if found { if found {
forecast := deepCopyForecast(cachedValue)
// Format forecast object and then return it // Format forecast object and then return it
for idx := range cachedValue.Forecast { for idx := range forecast.Forecast {
cachedValue.Forecast[idx].Min = fmtTemperature(cachedValue.Forecast[idx].Min, isImperial) val := &forecast.Forecast[idx]
cachedValue.Forecast[idx].Max = fmtTemperature(cachedValue.Forecast[idx].Max, isImperial)
cachedValue.Forecast[idx].FeelsLike = fmtTemperature(cachedValue.Forecast[idx].FeelsLike, isImperial) val.Min = fmtTemperature(val.Min, isImperial)
cachedValue.Forecast[idx].Wind.Speed = fmtWind(cachedValue.Forecast[idx].Wind.Speed, isImperial) val.Max = fmtTemperature(val.Max, isImperial)
val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial)
val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial)
} }
jsonValue(res, cachedValue) jsonValue(res, forecast)
} else { } else {
// Get city coordinates // Get city coordinates
city, err := model.GetCoordinates(cityName, vars.Token) city, err := model.GetCoordinates(cityName, vars.Token)
@@ -227,18 +244,16 @@ func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[
} }
// Add result to cache // Add result to cache
cache.AddEntry(forecast, cityName) cache.AddEntry(deepCopyForecast(forecast), cityName)
// *****************
// FIXME: formatting 'forecast' alters cached value
// *****************
// Format forecast object and then return it // Format forecast object and then return it
for idx := range forecast.Forecast { for idx := range forecast.Forecast {
forecast.Forecast[idx].Min = fmtTemperature(forecast.Forecast[idx].Min, isImperial) val := &forecast.Forecast[idx]
forecast.Forecast[idx].Max = fmtTemperature(forecast.Forecast[idx].Max, isImperial)
forecast.Forecast[idx].FeelsLike = fmtTemperature(forecast.Forecast[idx].FeelsLike, isImperial) val.Min = fmtTemperature(val.Min, isImperial)
forecast.Forecast[idx].Wind.Speed = fmtWind(forecast.Forecast[idx].Wind.Speed, isImperial) val.Max = fmtTemperature(val.Max, isImperial)
val.FeelsLike = fmtTemperature(val.FeelsLike, isImperial)
val.Wind.Speed = fmtWind(val.Wind.Speed, isImperial)
} }
jsonValue(res, forecast) jsonValue(res, forecast)

View File

@@ -43,7 +43,7 @@ type forecastRes struct {
func getForecastEntity(dailyForecast dailyRes) types.ForecastEntity { func getForecastEntity(dailyForecast dailyRes) types.ForecastEntity {
// Format UNIX timestamp as 'YYYY-MM-DD' // Format UNIX timestamp as 'YYYY-MM-DD'
utcTime := time.Unix(int64(dailyForecast.Timestamp), 0) utcTime := time.Unix(int64(dailyForecast.Timestamp), 0)
weatherDate := &types.ZephyrDate{Time: utcTime.UTC()} weatherDate := &types.ZephyrDate{Date: utcTime.UTC()}
// Set condition accordingly to weather description // Set condition accordingly to weather description
var condition string var condition string

View File

@@ -86,7 +86,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
// Format UNIX timestamp as 'YYYY-MM-DD' // Format UNIX timestamp as 'YYYY-MM-DD'
utcTime := time.Unix(int64(weather.Current.Timestamp), 0) utcTime := time.Unix(int64(weather.Current.Timestamp), 0)
weatherDate := &types.ZephyrDate{Time: utcTime.UTC()} weatherDate := &types.ZephyrDate{Date: utcTime.UTC()}
// Set condition accordingly to weather description // Set condition accordingly to weather description
var condition string var condition string

View File

@@ -6,7 +6,7 @@ import (
) )
type ZephyrDate struct { type ZephyrDate struct {
time.Time Date time.Time
} }
func (date *ZephyrDate) UnmarshalJSON(b []byte) error { func (date *ZephyrDate) UnmarshalJSON(b []byte) error {
@@ -16,7 +16,7 @@ func (date *ZephyrDate) UnmarshalJSON(b []byte) error {
} }
var err error var err error
date.Time, err = time.Parse("Monday, 2006/01/02", s) date.Date, err = time.Parse("Monday, 2006/01/02", s)
if err != nil { if err != nil {
return err return err
} }
@@ -25,11 +25,11 @@ func (date *ZephyrDate) UnmarshalJSON(b []byte) error {
} }
func (date *ZephyrDate) MarshalJSON() ([]byte, error) { func (date *ZephyrDate) MarshalJSON() ([]byte, error) {
if date.Time.IsZero() { if date.Date.IsZero() {
return []byte("\"\""), nil return []byte("\"\""), nil
} }
fmtDate := date.Time.Format("Monday, 2006/01/02") fmtDate := date.Date.Format("Monday, 2006/01/02")
return []byte("\"" + fmtDate + "\""), nil return []byte("\"" + fmtDate + "\""), nil
} }