MCPcopy Create free account
hub / github.com/TruthHun/BookStack / GetAdsCode

Function GetAdsCode

models/ads.go:276–305  ·  view source on GitHub ↗
(positionIdentify string, isMobile bool)

Source from the content-addressed store, hash-verified

274}
275
276func 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}

Callers

nothing calls this directly

Calls 1

appendFunction · 0.50

Tested by

no test coverage detected