package site import ( "encoding/json" "errors" "git.tetele.net/tgo/redis" ) /** * 从redis获取分站数据库及ID信息 * 2020/06/08 * gz */ func GetSiteFromRedis(host string) (map[string]string, error) { var site map[string]string = make(map[string]string) info, err := redis.HGetString(SITE_KEY, host) if err != nil { return site, err } type siteStruct struct { Database string `json:"database"` SiteId string `json:"site_id"` } var siteInfo siteStruct err = json.Unmarshal([]byte(info), &siteInfo) if err != nil { return site, err } if siteInfo.Database != "" { site["database"] = siteInfo.Database } if siteInfo.SiteId != "" { site["site_id"] = siteInfo.SiteId } return site, nil } /** * 分站数据库及ID信息保存及redis * 2020/06/08 * gz */ func SetToRedis(host string, site map[string]string) error { if host == "" { return errors.New("没有域名") } if len(site) < 1 { return errors.New("没有站点信息") } siteStr, err := json.Marshal(site) if err != nil { return err } _, err = redis.HMSet(SITE_KEY, host, siteStr) return err } func GetAll() ([]map[string]string, error) { list, err := redis.HGetAll(SITE_KEY) if err != nil { return nil, err } var siteInfo map[string]string var all []map[string]string if len(list) > 0 { for key, item := range list { if (key+1)%2 == 0 { //只处理偶数位 siteInfo = make(map[string]string) err = json.Unmarshal(item, &siteInfo) if err != nil { continue } siteInfo["domainname"] = string(list[key-1]) all = append(all, siteInfo) } } } return all, err }