Commit 50d865b5 by Demid Merzlyakov

A9Cache: support for limiting the max number of items cached for a given slot id.

parent 075430dc
......@@ -157,14 +157,19 @@ class A9Cache {
log.error("Couldn't start a9 preload for slotId \(slotId) adType \(adType): failed to determine adSize!")
fatalError()
}
let maxCount = configManager.adConfig.a9MaxCachedPerPlacement
var uncappedCount = count
if let cacheItem = cacheItems[slotId] {
cacheItem.countToCache += count
uncappedCount = cacheItem.countToCache + count
cacheItem.countToCache = min(uncappedCount, maxCount)
}
else {
let newCacheItem = A9CacheItem(slotId: slotId, adSize: adSize, count: count, cache: self)
let newCacheItem = A9CacheItem(slotId: slotId, adSize: adSize, count: min(uncappedCount, maxCount), cache: self)
cacheItems[slotId] = newCacheItem
}
if uncappedCount > maxCount {
log.debug("Limit cached count for slotId \(slotId) to \(maxCount)")
}
}
private func preloadAllIfNeeded() {
......
......@@ -9,8 +9,11 @@
import UIKit
struct AdConfig: Codable {
private static let defaultA9MaxCachedPerPlacement: UInt = 2
var adsEnabled: Bool
var a9RefreshRate: Int
var a9MaxCachedPerPlacement: UInt
var placements: [AdPlacementName: AdPlacement]?
var nativePlacements: [AdPlacementName: NativeAdPlacement]?
......@@ -33,12 +36,14 @@ struct AdConfig: Codable {
static let adsEnabled = CodingKeys(stringValue: "ads_enabled")
static let a9RefreshRate = CodingKeys(stringValue: "a9_refresh_rate")
static let a9MaxCachedPerPlacement = CodingKeys(stringValue: "a9_max_cached_per_placement")
static let placements = CodingKeys(stringValue: "placements")
}
init() {
adsEnabled = true
a9RefreshRate = 0
a9MaxCachedPerPlacement = AdConfig.defaultA9MaxCachedPerPlacement
}
init(from decoder: Decoder) throws {
......@@ -46,7 +51,9 @@ struct AdConfig: Codable {
adsEnabled = try container.decode(Bool.self, forKey: .adsEnabled)
a9RefreshRate = try container.decode(Int.self, forKey: .a9RefreshRate)
a9MaxCachedPerPlacement = try container.decodeIfPresent(UInt.self, forKey: .a9MaxCachedPerPlacement) ??
AdConfig.defaultA9MaxCachedPerPlacement
if container.contains(.placements) {
let allPlacementsContainer = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .placements)
var placements = [AdPlacementName: AdPlacement](minimumCapacity: allPlacementsContainer.allKeys.count)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment