func setCaches(u utils.Utils, caches *sliceCache) { if caches == nil || len(*caches) < 1 { return } items := make([]utils.MemoryItem, len(*caches)) ttlMemory := utils.ConfigGetIntWithDefault(u, "TTL_MEMORY", 86400) ttlDefault := utils.ConfigGetTtlDefault(u) ttlRestricted := utils.ConfigGetIntWithDefault(u, "TTL_RESTRICTED", 60) for index, cache := range *caches { valueTtl := ttlDefault if cache.Count < 1 { valueTtl = ttlRestricted } items[index] = utils.MemoryItem{ Key: getCacheKey(cache), Value: fmt.Sprintf("%d;%d", cache.Count, time.Now().Unix()+valueTtl), Ttl: ttlMemory, } } if err := u.MemorySet(&items); err != nil { u.Errorf("u.MemorySet(%d) error %v", len(items), err) } }
func checkCachesForRefresh(u utils.Utils, caches *sliceCache) { newCaches := make(sliceCache, 0) ttlLeftThreshold := utils.ConfigGetIntWithDefault(u, "REFRESH_TTL_LEFT_THRESHOLD", 10) for _, c := range *caches { if c.TtlLeft > ttlLeftThreshold { continue } if c.Count < COUNT_INITIAL_VALUE { continue } if dataNeedRefresh == nil { newData := make(MapUrlServiceCount) dataNeedRefresh = &newData } DataAdd(dataNeedRefresh, c.Service, c.Url) (*dataNeedRefresh)[c.Url][c.Service] = c.Count // intentionally do not count unique url because if one url got flagged // multiple times, it should be refreshed anyway dataNeedRefreshCount++ // temporary mark the cached count as fresh to avoid other process // also trying to refresh it, we will take care of it later newCaches = append(newCaches, cache{Service: c.Service, Url: c.Url, Count: c.Count}) } setCaches(u, &newCaches) }
func scheduleRefreshIfNeeded(u utils.Utils) { if dataNeedRefresh == nil { return } if dataNeedRefreshCount < utils.ConfigGetIntWithDefault(u, "REFRESH_BATCH_SIZE", 20) { return } u.Delay(DELAY_HANDLER_NAME_REFRESH, *dataNeedRefresh) dataNeedRefresh = nil dataNeedRefreshCount = 0 }