32 lines
807 B
Go
32 lines
807 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"slogan-agent/styleagent/agent"
|
|
)
|
|
|
|
var weatherCache = agent.NewTTLCache(6 * time.Hour)
|
|
|
|
// GetWeather 地点 + 日期范围 → 天气结果(高德地理编码 + 和风 7 天预报,缓存 6 小时)
|
|
func GetWeather(ctx context.Context, location, startDate, endDate string) (*agent.WeatherResult, error) {
|
|
cityCode, err := agent.GetCityCode(ctx, location)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cacheKey := fmt.Sprintf("%s:%s:%s", cityCode, startDate, endDate)
|
|
if v, ok := weatherCache.Get(cacheKey); ok {
|
|
if result, ok := v.(*agent.WeatherResult); ok {
|
|
return result, nil
|
|
}
|
|
}
|
|
result, err := agent.GetDaily(ctx, cityCode, startDate, endDate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
weatherCache.Set(cacheKey, result)
|
|
return result, nil
|
|
}
|