package cache import ( "encoding/json" "fmt" "strings" . "app.yhyue.com/moapp/jybase/common" . "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/redis" ) const ( threeDay = 172800 oneDay = 86400 AllSubPushCacheSize = 200 ) type SubPushList struct { Id string `json:"_id"` Title string `json:"title"` Area string `json:"area"` BuyerClass string `json:"buyerClass"` Subtype string `json:"subtype"` Industry string `json:"industry"` PublishTime int64 `json:"publishTime"` Ca_index int64 `json:"ca_index"` Ca_date int64 `json:"ca_date"` Ca_isvisit int `json:"ca_isvisit"` Ca_isvip int `json:"ca_isvip"` Ca_type int `json:"ca_type"` Ca_fileExists bool `json:"ca_fileExists"` MatchKeys []string `json:"matchKeys"` Budget interface{} `json:"budget"` BidAmount interface{} `json:"bidAmount"` Collection int `json:"collection"` Buyer string `json:"buyer"` ProjectName string `json:"projectName"` Winner string `json:"winner"` BidOpenTime int64 `json:"bidOpenTime"` Source int64 `json:"source"` } type SubPush struct { Date string Datas []*SubPushList Count int64 } type PushCa struct { Date int64 InfoId string Visit int Keys []string Type int Isvip int FileExists bool Source int64 Info *map[string]interface{} } type Cache struct { Prefix string UserId string Ymd string } func NewEntnicheCache(userId, ymd string) *Cache { return &Cache{ Prefix: "entnichepush", UserId: userId, Ymd: ymd, } } func NewSubscribeCache(userId, ymd string) *Cache { return &Cache{ Prefix: "subpush", UserId: userId, Ymd: ymd, } } func NewMemberCache(userId, ymd string) *Cache { return &Cache{ Prefix: "memberpush", UserId: userId, Ymd: ymd, } } // func NewSubPushList(pushCas []*PushCa) []*SubPushList { subPushList := []*SubPushList{} for _, p := range pushCas { info := p.Info area := ObjToString((*info)["area"]) if area == "A" { area = "全国" } industry := ObjToString((*info)["s_subscopeclass"]) scs := strings.Split(industry, ",") if len(scs) > 0 { industry = scs[0] if industry != "" { iss := strings.Split(industry, "_") if len(iss) > 0 { industry = iss[0] } } } infotype := ObjToString((*info)["subtype"]) if infotype == "" { infotype = ObjToString((*info)["toptype"]) } _id := p.InfoId if _id == "" { _id = ObjToString((*info)["_id"]) } subPushList = append(subPushList, &SubPushList{ Id: EncodeArticleId2ByCheck(_id), Title: ObjToString((*info)["title"]), Area: area, BuyerClass: ObjToString((*info)["buyerclass"]), Subtype: infotype, Industry: industry, PublishTime: Int64All((*info)["publishtime"]), Ca_date: p.Date, Ca_isvisit: p.Visit, Ca_isvip: p.Isvip, Ca_type: p.Type, Ca_fileExists: p.FileExists, MatchKeys: p.Keys, Budget: (*info)["budget"], BidAmount: (*info)["bidamount"], Buyer: ObjToString((*info)["buyer"]), ProjectName: ObjToString((*info)["projectname"]), Winner: ObjToString((*info)["s_winner"]), BidOpenTime: Int64All((*info)["bidopentime"]), Source: p.Source, }) } return subPushList } //从pushcache_2_b中取 func (c *Cache) GetTodayCache(userId string) (*SubPush, error) { pc_a, err := redis.GetNewBytes("pushcache_2_b", c.todayKey(userId)) if err != nil { return nil, err } if pc_a == nil { return nil, nil } var p *SubPush if err := json.Unmarshal(*pc_a, &p); err != nil { return nil, err } return p, nil } //往pushcache_2_b中放 func (c *Cache) PutTodayCache(userId string, pc_a *SubPush) { redis.Put("pushcache_2_b", c.todayKey(userId), pc_a, oneDay) } //获取redis key func (c *Cache) todayKey(userId string) string { return fmt.Sprintf("%s_%s", c.Prefix, userId) } func (c *Cache) allKey(userId string) string { return fmt.Sprintf("all_%s_%s", c.Prefix, userId) } //查看全部列表缓存 func (c *Cache) PutAllCache(userId string, datas *SubPush) { redis.Put("pushcache_2_a", c.allKey(userId), datas, oneDay) } func (c *Cache) GetAllCache(userId string) (*SubPush, error) { return c.GetCache("pushcache_2_a", c.allKey(userId)) } func (c *Cache) GetCache(code, key string) (*SubPush, error) { pc_a, err := redis.GetNewBytes(code, key) if err != nil { return nil, err } if pc_a == nil { return nil, nil } var p *SubPush if err := json.Unmarshal(*pc_a, &p); err != nil { return nil, err } return p, nil } func (c *Cache) DoCache(subPushList []*SubPushList) { subPush_datas := []*SubPushList{} for i := len(subPushList) - 1; i >= 0; i-- { subPush_datas = append(subPush_datas, subPushList[i]) } //更新redis subPush, err := c.GetTodayCache(c.UserId) if err == nil && subPush != nil && len(subPush.Datas) > 0 { if subPush.Date != c.Ymd { subPush = &SubPush{ Date: c.Ymd, Datas: subPush_datas, Count: int64(len(subPush_datas)), } } else { subPush.Datas = append(subPush_datas, subPush.Datas...) subPush.Count = subPush.Count + int64(len(subPush_datas)) } c.PutTodayCache(c.UserId, subPush) } //全部列表缓存 allCache, err := c.GetAllCache(c.UserId) if err == nil && allCache != nil && len(allCache.Datas) > 0 { allCache.Datas = append(subPush_datas, allCache.Datas...) if len(allCache.Datas) > AllSubPushCacheSize { allCache.Datas = allCache.Datas[:AllSubPushCacheSize] } allCache.Count = allCache.Count + int64(len(subPush_datas)) c.PutAllCache(c.UserId, allCache) } }