1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package bootstrap
- import (
- "context"
- "designs/config"
- "designs/global"
- "github.com/go-redis/redis/v8"
- "go.uber.org/zap"
- "strconv"
- "strings"
- )
- func InitializeRedis() *redis.Client {
- client := redis.NewClient(&redis.Options{
- Addr: config.Get("redis.host") + ":" + config.Get("redis.port"),
- Password: config.Get("redis.password"), // no password set
- DB: config.GetInt("redis.db"), // use default DB
- //Addr: "localhost:6379", // Redis 服务器地址和端口
- //Password: "", // Redis 访问密码,如果没有可以为空字符串
- //DB: 0, // 使用的 Redis 数据库编号,默认为 0
- })
- _, err := client.Ping(context.Background()).Result()
- if err != nil {
- global.App.Log.Error("Redis connect ping failed, err:", zap.Any("err", err))
- return nil
- }
- return client
- }
- func UnInitializeRedis(client *redis.Client) {
- if client != nil {
- client.Close()
- }
- }
- /* 通过ip地址获取地址code */
- func FindAddresscodeByIp(ip string) int {
- pro, _ := FindAddressByIp(ip)
- code := 999
- res, err := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
- if err != nil {
- return code
- }
- for key, val := range res {
- code, _ := strconv.Atoi(val)
- if strings.Contains(pro, key) {
- return code
- }
- }
- return code
- }
- /* 通过hid获取地址code */
- func FindAddresscodeByHid(hid int) (int, error) {
- res, _ := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
- for _, val := range res {
- code, _ := strconv.Atoi(val)
- if code == hid {
- return hid, nil
- }
- }
- return 0, redis.Nil
- }
- /* 通过hid获取地址 */
- func FindAddressByHid(hid int) string {
- res, _ := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
- for key, val := range res {
- code, _ := strconv.Atoi(val)
- if code == hid {
- return key
- }
- }
- return ""
- }
|