Compare commits
No commits in common. "754c4899ada615c994bc24635ff099ef397ff4cd" and "bd849173d4d58f147525c1912d32ea8eda1facbd" have entirely different histories.
754c4899ad
...
bd849173d4
|
|
@ -7,7 +7,3 @@
|
|||
安装hz工具: go install github.com/cloudwego/hertz/cmd/hz@latest
|
||||
修改 idl/WeatherAndEarthquake.thrift 文件后,在根目录执行:
|
||||
hz update -idl ./idl/WeatherAndEarthquake.thrift 就会更新新加的内容
|
||||
|
||||
|
||||
## 安装 go-redis
|
||||
go get github.com/go-redis/redis/v8
|
||||
|
|
|
|||
|
|
@ -6,13 +6,10 @@ server:
|
|||
data:
|
||||
redis:
|
||||
addr: 127.0.0.1:6379
|
||||
password:
|
||||
password: Abc123..
|
||||
read_timeout: 0.2s
|
||||
write_timeout: 0.2s
|
||||
db: 0
|
||||
pool_size: 16
|
||||
min_idle_conns: 4
|
||||
expire: 3600
|
||||
db: 1
|
||||
|
||||
accu_weather:
|
||||
api_key: zpka_0e750f6b405446f389a3a7d954c780b6_63fd410f
|
||||
|
|
|
|||
3
go.mod
3
go.mod
|
|
@ -15,13 +15,10 @@ require (
|
|||
github.com/bytedance/gopkg v0.1.1 // indirect
|
||||
github.com/bytedance/sonic v1.14.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/gopkg v0.1.4 // 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/go-redis/redis/v8 v8.11.5 // indirect
|
||||
github.com/golang/protobuf v1.5.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
|
||||
github.com/nyaruka/phonenumbers v1.0.55 // indirect
|
||||
|
|
|
|||
6
go.sum
6
go.sum
|
|
@ -7,8 +7,6 @@ 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.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
|
||||
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/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/gopkg v0.1.4 h1:EoQiCG4sTonTPHxOGE0VlQs+sQR+Hsi2uN0qqwu8O50=
|
||||
|
|
@ -22,12 +20,8 @@ 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.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/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/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.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
|
|
|
|||
|
|
@ -14,9 +14,6 @@ type Redis struct {
|
|||
ReadTimeout time.Duration `yaml:"read_timeout"`
|
||||
WriteTimeout time.Duration `yaml:"write_timeout"`
|
||||
DB int `yaml:"db"`
|
||||
PoolSize int `yaml:"pool_size"`
|
||||
MinIdleConns int `yaml:"min_idle_conns"`
|
||||
Expire time.Duration `yaml:"expire"` // 数据保存时间,单位秒
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
|
|
|
|||
|
|
@ -1,46 +1,5 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
"weather_and_earthquake/internal/config"
|
||||
)
|
||||
import "weather_and_earthquake/internal/config"
|
||||
|
||||
var _redis *redis.Client
|
||||
var _expire time.Duration
|
||||
|
||||
// 初始化 redis 连接
|
||||
func InitWeatherCache(ctx context.Context, data *config.Redis) error {
|
||||
_redis = redis.NewClient(&redis.Options{
|
||||
Addr: data.Addr,
|
||||
Password: data.Password,
|
||||
DB: data.DB,
|
||||
ReadTimeout: data.ReadTimeout * time.Second,
|
||||
WriteTimeout: data.WriteTimeout * time.Second,
|
||||
PoolSize: data.PoolSize,
|
||||
MinIdleConns: data.MinIdleConns,
|
||||
})
|
||||
_expire = data.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
|
||||
}
|
||||
}
|
||||
var DataConf *config.Data
|
||||
|
|
|
|||
17
main.go
17
main.go
|
|
@ -3,7 +3,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
|
|
@ -62,11 +61,14 @@ func initialize() {
|
|||
|
||||
biz.AccuWeather = conf.AccuWeather
|
||||
middleware.Apps = apps
|
||||
err = data.InitWeatherCache(context.Background(), conf.Data.Redis)
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
data.DataConf = conf.Data
|
||||
|
||||
log.Println("LogLevel=", conf.Server.LogLevel)
|
||||
logs.SetFile("./logs/logs.log")
|
||||
logs.Warn("Logs warning")
|
||||
logs.Error("Logs error")
|
||||
logs.Info("Logs info")
|
||||
logs.Debug("Logs debug")
|
||||
switch conf.Server.LogLevel {
|
||||
case 1:
|
||||
logs.SetLevel(logs.LINFO)
|
||||
|
|
@ -77,11 +79,6 @@ func initialize() {
|
|||
default:
|
||||
logs.SetLevel(logs.LDEBUG)
|
||||
}
|
||||
logs.SetFile("./logs/logs.log")
|
||||
logs.Warn("Logs warning")
|
||||
logs.Error("Logs error")
|
||||
logs.Info("Logs info")
|
||||
logs.Debug("Logs debug")
|
||||
|
||||
hostPort := fmt.Sprintf("%s:%d", conf.Server.Addr, conf.Server.Port)
|
||||
_server = server.Default(server.WithHostPorts(hostPort))
|
||||
|
|
|
|||
Loading…
Reference in New Issue