Skip to content

Commit 21d4875

Browse files
Support Default Bids Cache TTL (#3543)
1 parent 9cb9a83 commit 21d4875

File tree

300 files changed

+1258
-177
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

300 files changed

+1258
-177
lines changed

docs/config-app.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ See [metrics documentation](metrics.md) for complete list of metrics submitted a
278278
for particular publisher account. Overrides `cache.banner-ttl-seconds` property.
279279
- `cache.account.<ACCOUNT>.video-ttl-seconds` - how long (in seconds) video creative will be available in Cache Service
280280
for particular publisher account. Overrides `cache.video-ttl-seconds` property.
281+
- `cache.default-ttl-seconds.{banner, video, audio, native}` - a default value how long (in seconds) a creative of the specific type will be available in Cache Service
281282

282283
## Application settings (account configuration, stored ad unit configurations, stored requests)
283284
Preconfigured application settings can be obtained from multiple data sources consequently:

src/main/java/org/prebid/server/auction/BidResponseCreator.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import org.prebid.server.settings.model.AccountEventsConfig;
9696
import org.prebid.server.settings.model.AccountTargetingConfig;
9797
import org.prebid.server.settings.model.VideoStoredDataResult;
98+
import org.prebid.server.spring.config.model.CacheDefaultTtlProperties;
9899
import org.prebid.server.util.StreamUtil;
99100
import org.prebid.server.vast.VastModifier;
100101

@@ -139,6 +140,7 @@ public class BidResponseCreator {
139140
private final Clock clock;
140141
private final JacksonMapper mapper;
141142
private final CacheTtl mediaTypeCacheTtl;
143+
private final CacheDefaultTtlProperties cacheDefaultProperties;
142144

143145
private final String cacheHost;
144146
private final String cachePath;
@@ -156,7 +158,8 @@ public BidResponseCreator(CoreCacheService coreCacheService,
156158
int truncateAttrChars,
157159
Clock clock,
158160
JacksonMapper mapper,
159-
CacheTtl mediaTypeCacheTtl) {
161+
CacheTtl mediaTypeCacheTtl,
162+
CacheDefaultTtlProperties cacheDefaultProperties) {
160163

161164
this.coreCacheService = Objects.requireNonNull(coreCacheService);
162165
this.bidderCatalog = Objects.requireNonNull(bidderCatalog);
@@ -171,6 +174,7 @@ public BidResponseCreator(CoreCacheService coreCacheService,
171174
this.clock = Objects.requireNonNull(clock);
172175
this.mapper = Objects.requireNonNull(mapper);
173176
this.mediaTypeCacheTtl = Objects.requireNonNull(mediaTypeCacheTtl);
177+
this.cacheDefaultProperties = Objects.requireNonNull(cacheDefaultProperties);
174178

175179
cacheHost = Objects.requireNonNull(coreCacheService.getEndpointHost());
176180
cachePath = Objects.requireNonNull(coreCacheService.getEndpointPath());
@@ -436,8 +440,8 @@ private BidInfo toBidInfo(Bid bid,
436440
.bidType(type)
437441
.bidder(bidder)
438442
.correspondingImp(correspondingImp)
439-
.ttl(resolveBannerTtl(bid, correspondingImp, cacheInfo, account))
440-
.videoTtl(type == BidType.video ? resolveVideoTtl(bid, correspondingImp, cacheInfo, account) : null)
443+
.ttl(resolveTtl(bid, type, correspondingImp, cacheInfo, account))
444+
.vastTtl(type == BidType.video ? resolveVastTtl(bid, correspondingImp, cacheInfo, account) : null)
441445
.category(categoryMappingResult.getCategory(bid))
442446
.satisfiedPriority(categoryMappingResult.isBidSatisfiesPriority(bid))
443447
.build();
@@ -457,31 +461,43 @@ private static Optional<Imp> correspondingImp(String impId, List<Imp> imps) {
457461
.findFirst();
458462
}
459463

460-
private Integer resolveBannerTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
461-
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
464+
private Integer resolveTtl(Bid bid, BidType type, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
462465
final Integer bidTtl = bid.getExp();
463466
final Integer impTtl = imp != null ? imp.getExp() : null;
467+
final Integer requestTtl = cacheInfo.getCacheBidsTtl();
464468

465-
return ObjectUtils.firstNonNull(
466-
bidTtl,
467-
impTtl,
468-
cacheInfo.getCacheBidsTtl(),
469-
accountAuctionConfig != null ? accountAuctionConfig.getBannerCacheTtl() : null,
470-
mediaTypeCacheTtl.getBannerCacheTtl());
469+
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
470+
final Integer accountTtl = accountAuctionConfig != null ? switch (type) {
471+
case banner -> accountAuctionConfig.getBannerCacheTtl();
472+
case video -> accountAuctionConfig.getVideoCacheTtl();
473+
case audio, xNative -> null;
474+
} : null;
475+
476+
final Integer mediaTypeTtl = switch (type) {
477+
case banner -> mediaTypeCacheTtl.getBannerCacheTtl();
478+
case video -> mediaTypeCacheTtl.getVideoCacheTtl();
479+
case audio, xNative -> null;
480+
};
471481

482+
final Integer defaultTtl = switch (type) {
483+
case banner -> cacheDefaultProperties.getBannerTtl();
484+
case video -> cacheDefaultProperties.getVideoTtl();
485+
case audio -> cacheDefaultProperties.getAudioTtl();
486+
case xNative -> cacheDefaultProperties.getNativeTtl();
487+
};
488+
489+
return ObjectUtils.firstNonNull(bidTtl, impTtl, requestTtl, accountTtl, mediaTypeTtl, defaultTtl);
472490
}
473491

474-
private Integer resolveVideoTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
492+
private Integer resolveVastTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
475493
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
476-
final Integer bidTtl = bid.getExp();
477-
final Integer impTtl = imp != null ? imp.getExp() : null;
478-
479494
return ObjectUtils.firstNonNull(
480-
bidTtl,
481-
impTtl,
495+
bid.getExp(),
496+
imp != null ? imp.getExp() : null,
482497
cacheInfo.getCacheVideoBidsTtl(),
483498
accountAuctionConfig != null ? accountAuctionConfig.getVideoCacheTtl() : null,
484-
mediaTypeCacheTtl.getVideoCacheTtl());
499+
mediaTypeCacheTtl.getVideoCacheTtl(),
500+
cacheDefaultProperties.getVideoTtl());
485501
}
486502

487503
private Future<List<BidderResponse>> invokeProcessedBidderResponseHooks(List<BidderResponse> bidderResponses,
@@ -1369,7 +1385,7 @@ private Bid toBid(BidInfo bidInfo,
13691385

13701386
final Integer ttl = Optional.ofNullable(cacheInfo)
13711387
.map(info -> ObjectUtils.max(cacheInfo.getTtl(), cacheInfo.getVideoTtl()))
1372-
.orElseGet(() -> ObjectUtils.max(bidInfo.getTtl(), bidInfo.getVideoTtl()));
1388+
.orElseGet(() -> ObjectUtils.max(bidInfo.getTtl(), bidInfo.getVastTtl()));
13731389

13741390
return bid.toBuilder()
13751391
.ext(updatedBidExt)

src/main/java/org/prebid/server/auction/model/BidInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class BidInfo {
3333

3434
Integer ttl;
3535

36-
Integer videoTtl;
36+
Integer vastTtl;
3737

3838
public String getBidId() {
3939
final ObjectNode extNode = bid != null ? bid.getExt() : null;

src/main/java/org/prebid/server/cache/CoreCacheService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private List<CacheBid> getCacheBids(List<BidInfo> bidInfos) {
250250
private List<CacheBid> getVideoCacheBids(List<BidInfo> bidInfos) {
251251
return bidInfos.stream()
252252
.filter(bidInfo -> Objects.equals(bidInfo.getBidType(), BidType.video))
253-
.map(bidInfo -> CacheBid.of(bidInfo, bidInfo.getVideoTtl()))
253+
.map(bidInfo -> CacheBid.of(bidInfo, bidInfo.getVastTtl()))
254254
.toList();
255255
}
256256

src/main/java/org/prebid/server/spring/config/ServiceConfiguration.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import org.prebid.server.privacy.gdpr.TcfDefinerService;
111111
import org.prebid.server.settings.ApplicationSettings;
112112
import org.prebid.server.settings.model.BidValidationEnforcement;
113+
import org.prebid.server.spring.config.model.CacheDefaultTtlProperties;
113114
import org.prebid.server.spring.config.model.ExternalConversionProperties;
114115
import org.prebid.server.spring.config.model.HttpClientCircuitBreakerProperties;
115116
import org.prebid.server.spring.config.model.HttpClientProperties;
@@ -796,6 +797,16 @@ BidderErrorNotifier bidderErrorNotifier(
796797
metrics);
797798
}
798799

800+
@Bean
801+
CacheDefaultTtlProperties cacheDefaultTtlProperties(
802+
@Value("${cache.default-ttl-seconds.banner:300}") Integer bannerTtl,
803+
@Value("${cache.default-ttl-seconds.video:1500}") Integer videoTtl,
804+
@Value("${cache.default-ttl-seconds.audio:1500}") Integer audioTtl,
805+
@Value("${cache.default-ttl-seconds.native:300}") Integer nativeTtl) {
806+
807+
return CacheDefaultTtlProperties.of(bannerTtl, videoTtl, audioTtl, nativeTtl);
808+
}
809+
799810
@Bean
800811
BidResponseCreator bidResponseCreator(
801812
CoreCacheService coreCacheService,
@@ -811,7 +822,8 @@ BidResponseCreator bidResponseCreator(
811822
Clock clock,
812823
JacksonMapper mapper,
813824
@Value("${cache.banner-ttl-seconds:#{null}}") Integer bannerCacheTtl,
814-
@Value("${cache.video-ttl-seconds:#{null}}") Integer videoCacheTtl) {
825+
@Value("${cache.video-ttl-seconds:#{null}}") Integer videoCacheTtl,
826+
CacheDefaultTtlProperties cacheDefaultTtlProperties) {
815827

816828
return new BidResponseCreator(
817829
coreCacheService,
@@ -826,7 +838,8 @@ BidResponseCreator bidResponseCreator(
826838
truncateAttrChars,
827839
clock,
828840
mapper,
829-
CacheTtl.of(bannerCacheTtl, videoCacheTtl));
841+
CacheTtl.of(bannerCacheTtl, videoCacheTtl),
842+
cacheDefaultTtlProperties);
830843
}
831844

832845
@Bean
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.prebid.server.spring.config.model;
2+
3+
import lombok.Value;
4+
5+
@Value(staticConstructor = "of")
6+
public class CacheDefaultTtlProperties {
7+
8+
Integer bannerTtl;
9+
10+
Integer videoTtl;
11+
12+
Integer audioTtl;
13+
14+
Integer nativeTtl;
15+
}

0 commit comments

Comments
 (0)