feat: 天气数据通过redis缓存

This commit is contained in:
pengyinjie 2025-11-03 15:38:27 +08:00
parent bd849173d4
commit fdfca62b75
6 changed files with 65 additions and 5 deletions

View File

@ -6,4 +6,8 @@
框架是 Hertz: https://www.cloudwego.io/zh/docs/hertz/getting-started/ 框架是 Hertz: https://www.cloudwego.io/zh/docs/hertz/getting-started/
安装hz工具: go install github.com/cloudwego/hertz/cmd/hz@latest 安装hz工具: go install github.com/cloudwego/hertz/cmd/hz@latest
修改 idl/WeatherAndEarthquake.thrift 文件后,在根目录执行: 修改 idl/WeatherAndEarthquake.thrift 文件后,在根目录执行:
hz update -idl ./idl/WeatherAndEarthquake.thrift 就会更新新加的内容 hz update -idl ./idl/WeatherAndEarthquake.thrift 就会更新新加的内容
## 安装 go-redis
go get github.com/go-redis/redis/v8

View File

@ -6,10 +6,13 @@ server:
data: data:
redis: redis:
addr: 127.0.0.1:6379 addr: 127.0.0.1:6379
password: Abc123.. password:
read_timeout: 0.2s read_timeout: 0.2s
write_timeout: 0.2s write_timeout: 0.2s
db: 1 db: 0
pool_size: 16
min_idle_conns: 4
expire: 3600
accu_weather: accu_weather:
api_key: zpka_0e750f6b405446f389a3a7d954c780b6_63fd410f api_key: zpka_0e750f6b405446f389a3a7d954c780b6_63fd410f

3
go.mod
View File

@ -15,10 +15,13 @@ require (
github.com/bytedance/gopkg v0.1.1 // indirect github.com/bytedance/gopkg v0.1.1 // indirect
github.com/bytedance/sonic v1.14.0 // indirect github.com/bytedance/sonic v1.14.0 // indirect
github.com/bytedance/sonic/loader v0.3.0 // indirect github.com/bytedance/sonic/loader v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cloudwego/gopkg v0.1.4 // indirect github.com/cloudwego/gopkg v0.1.4 // indirect
github.com/cloudwego/netpoll v0.7.0 // indirect github.com/cloudwego/netpoll v0.7.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/golang/protobuf v1.5.0 // indirect github.com/golang/protobuf v1.5.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/nyaruka/phonenumbers v1.0.55 // indirect github.com/nyaruka/phonenumbers v1.0.55 // indirect

6
go.sum
View File

@ -7,6 +7,8 @@ github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxl
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA= github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/gopkg v0.1.4 h1:EoQiCG4sTonTPHxOGE0VlQs+sQR+Hsi2uN0qqwu8O50= github.com/cloudwego/gopkg v0.1.4 h1:EoQiCG4sTonTPHxOGE0VlQs+sQR+Hsi2uN0qqwu8O50=
@ -20,8 +22,12 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=

View File

@ -14,6 +14,9 @@ type Redis struct {
ReadTimeout time.Duration `yaml:"read_timeout"` ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"` WriteTimeout time.Duration `yaml:"write_timeout"`
DB int `yaml:"db"` DB int `yaml:"db"`
PoolSize int `yaml:"pool_size"`
MinIdleConns int `yaml:"min_idle_conns"`
Expire time.Duration `yaml:"expire"` // 数据保存时间,单位秒
} }
type Data struct { type Data struct {

View File

@ -1,5 +1,46 @@
package data package data
import "weather_and_earthquake/internal/config" import (
"context"
"github.com/go-redis/redis/v8"
"time"
"weather_and_earthquake/internal/config"
)
var DataConf *config.Data var _redis *redis.Client
var _expire time.Duration
// 初始化 redis 连接
func InitWeatherCache(ctx context.Context, data *config.Data) error {
_redis = redis.NewClient(&redis.Options{
Addr: data.Redis.Addr,
Password: data.Redis.Password,
DB: data.Redis.DB,
ReadTimeout: data.Redis.ReadTimeout * time.Second,
WriteTimeout: data.Redis.WriteTimeout * time.Second,
PoolSize: data.Redis.PoolSize,
MinIdleConns: data.Redis.MinIdleConns,
})
_expire = data.Redis.Expire
_, err := _redis.Ping(ctx).Result()
return err
}
// 设置某个地方的天气情况默认保留1小时数据
func SetWeatherCondition(ctx context.Context, key, condition string) error {
_, err := _redis.SetNX(ctx, key, condition, _expire*time.Second).Result()
return err
}
// 查找某个地方的天气情况
func GetWeatherCondition(ctx context.Context, key string) (string, error) {
value, err := _redis.Get(ctx, key).Result()
if err == nil {
return value, nil
} else if err == redis.Nil {
return "", nil
} else {
return "", err
}
}