| 274 | } |
| 275 | |
| 276 | func GetAdsCode(positionIdentify string, isMobile bool) (code string) { |
| 277 | if beego.AppConfig.String("runmode") == "dev" { |
| 278 | beego.Debug("getAdsCode", positionIdentify, isMobile) |
| 279 | } |
| 280 | key := fmt.Sprintf("%v-%v", positionIdentify, isMobile) |
| 281 | pid, ok := positionCache.Load(key) |
| 282 | if !ok { |
| 283 | return |
| 284 | } |
| 285 | data, ok := adsCache.Load(pid.(int)) |
| 286 | if !ok { |
| 287 | return |
| 288 | } |
| 289 | var ads []AdsCont |
| 290 | nowSec := int(time.Now().Unix()) |
| 291 | for _, ad := range data.([]AdsCont) { |
| 292 | if ad.Start <= nowSec && ad.End >= nowSec { |
| 293 | ads = append(ads, ad) |
| 294 | } |
| 295 | } |
| 296 | lenAds := len(ads) |
| 297 | |
| 298 | if lenAds == 0 { |
| 299 | return |
| 300 | } else if lenAds == 1 { |
| 301 | return ads[0].Code |
| 302 | } |
| 303 | rand.Seed(time.Now().UnixNano()) |
| 304 | return ads[rand.Intn(lenAds)].Code |
| 305 | } |