Skip to content

Commit

Permalink
Support Default Bids Cache TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoxaAntoxic committed Nov 8, 2024
1 parent 33c828b commit 3163a32
Show file tree
Hide file tree
Showing 297 changed files with 865 additions and 70 deletions.
54 changes: 35 additions & 19 deletions src/main/java/org/prebid/server/auction/BidResponseCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.prebid.server.settings.model.AccountEventsConfig;
import org.prebid.server.settings.model.AccountTargetingConfig;
import org.prebid.server.settings.model.VideoStoredDataResult;
import org.prebid.server.spring.config.model.CacheDefaultTtlProperties;
import org.prebid.server.util.StreamUtil;
import org.prebid.server.vast.VastModifier;

Expand Down Expand Up @@ -139,6 +140,7 @@ public class BidResponseCreator {
private final Clock clock;
private final JacksonMapper mapper;
private final CacheTtl mediaTypeCacheTtl;
private final CacheDefaultTtlProperties cacheDefaultProperties;

private final String cacheHost;
private final String cachePath;
Expand All @@ -156,7 +158,8 @@ public BidResponseCreator(CoreCacheService coreCacheService,
int truncateAttrChars,
Clock clock,
JacksonMapper mapper,
CacheTtl mediaTypeCacheTtl) {
CacheTtl mediaTypeCacheTtl,
CacheDefaultTtlProperties cacheDefaultProperties) {

this.coreCacheService = Objects.requireNonNull(coreCacheService);
this.bidderCatalog = Objects.requireNonNull(bidderCatalog);
Expand All @@ -171,6 +174,7 @@ public BidResponseCreator(CoreCacheService coreCacheService,
this.clock = Objects.requireNonNull(clock);
this.mapper = Objects.requireNonNull(mapper);
this.mediaTypeCacheTtl = Objects.requireNonNull(mediaTypeCacheTtl);
this.cacheDefaultProperties = Objects.requireNonNull(cacheDefaultProperties);

cacheHost = Objects.requireNonNull(coreCacheService.getEndpointHost());
cachePath = Objects.requireNonNull(coreCacheService.getEndpointPath());
Expand Down Expand Up @@ -436,8 +440,8 @@ private BidInfo toBidInfo(Bid bid,
.bidType(type)
.bidder(bidder)
.correspondingImp(correspondingImp)
.ttl(resolveBannerTtl(bid, correspondingImp, cacheInfo, account))
.videoTtl(type == BidType.video ? resolveVideoTtl(bid, correspondingImp, cacheInfo, account) : null)
.ttl(resolveTtl(bid, type, correspondingImp, cacheInfo, account))
.vastTtl(type == BidType.video ? resolveVastTtl(bid, correspondingImp, cacheInfo, account) : null)
.category(categoryMappingResult.getCategory(bid))
.satisfiedPriority(categoryMappingResult.isBidSatisfiesPriority(bid))
.build();
Expand All @@ -457,31 +461,43 @@ private static Optional<Imp> correspondingImp(String impId, List<Imp> imps) {
.findFirst();
}

private Integer resolveBannerTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
private Integer resolveTtl(Bid bid, BidType type, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
final Integer bidTtl = bid.getExp();
final Integer impTtl = imp != null ? imp.getExp() : null;
final Integer requestTtl = cacheInfo.getCacheBidsTtl();

return ObjectUtils.firstNonNull(
bidTtl,
impTtl,
cacheInfo.getCacheBidsTtl(),
accountAuctionConfig != null ? accountAuctionConfig.getBannerCacheTtl() : null,
mediaTypeCacheTtl.getBannerCacheTtl());
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
final Integer accountTtl = accountAuctionConfig != null ? switch (type) {
case banner -> accountAuctionConfig.getBannerCacheTtl();
case video -> accountAuctionConfig.getVideoCacheTtl();
case audio, xNative -> null;
} : null;

final Integer mediaTypeTtl = switch (type) {
case banner -> mediaTypeCacheTtl.getBannerCacheTtl();
case video -> mediaTypeCacheTtl.getVideoCacheTtl();
case audio, xNative -> null;
};

final Integer defaultTtl = switch (type) {
case banner -> cacheDefaultProperties.getBannerTtl();
case video -> cacheDefaultProperties.getVideoTtl();
case audio -> cacheDefaultProperties.getAudioTtl();
case xNative -> cacheDefaultProperties.getNativeTtl();
};

return ObjectUtils.firstNonNull(bidTtl, impTtl, requestTtl, accountTtl, mediaTypeTtl, defaultTtl);
}

private Integer resolveVideoTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
private Integer resolveVastTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
final Integer bidTtl = bid.getExp();
final Integer impTtl = imp != null ? imp.getExp() : null;

return ObjectUtils.firstNonNull(
bidTtl,
impTtl,
bid.getExp(),
imp != null ? imp.getExp() : null,
cacheInfo.getCacheVideoBidsTtl(),
accountAuctionConfig != null ? accountAuctionConfig.getVideoCacheTtl() : null,
mediaTypeCacheTtl.getVideoCacheTtl());
mediaTypeCacheTtl.getVideoCacheTtl(),
cacheDefaultProperties.getVideoTtl());
}

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

final Integer ttl = Optional.ofNullable(cacheInfo)
.map(info -> ObjectUtils.max(cacheInfo.getTtl(), cacheInfo.getVideoTtl()))
.orElseGet(() -> ObjectUtils.max(bidInfo.getTtl(), bidInfo.getVideoTtl()));
.orElseGet(() -> ObjectUtils.max(bidInfo.getTtl(), bidInfo.getVastTtl()));

return bid.toBuilder()
.ext(updatedBidExt)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/prebid/server/auction/model/BidInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BidInfo {

Integer ttl;

Integer videoTtl;
Integer vastTtl;

public String getBidId() {
final ObjectNode extNode = bid != null ? bid.getExt() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private List<CacheBid> getCacheBids(List<BidInfo> bidInfos) {
private List<CacheBid> getVideoCacheBids(List<BidInfo> bidInfos) {
return bidInfos.stream()
.filter(bidInfo -> Objects.equals(bidInfo.getBidType(), BidType.video))
.map(bidInfo -> CacheBid.of(bidInfo, bidInfo.getVideoTtl()))
.map(bidInfo -> CacheBid.of(bidInfo, bidInfo.getVastTtl()))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import org.prebid.server.privacy.gdpr.TcfDefinerService;
import org.prebid.server.settings.ApplicationSettings;
import org.prebid.server.settings.model.BidValidationEnforcement;
import org.prebid.server.spring.config.model.CacheDefaultTtlProperties;
import org.prebid.server.spring.config.model.ExternalConversionProperties;
import org.prebid.server.spring.config.model.HttpClientCircuitBreakerProperties;
import org.prebid.server.spring.config.model.HttpClientProperties;
Expand Down Expand Up @@ -789,6 +790,16 @@ BidderErrorNotifier bidderErrorNotifier(
metrics);
}

@Bean
CacheDefaultTtlProperties cacheDefaultTtlProperties(
@Value("${cache.default-ttl-seconds.banner:300}") Integer bannerTtl,
@Value("${cache.default-ttl-seconds.video:1500}") Integer videoTtl,
@Value("${cache.default-ttl-seconds.audio:1500}") Integer audioTtl,
@Value("${cache.default-ttl-seconds.native:300}") Integer nativeTtl) {

return CacheDefaultTtlProperties.of(bannerTtl, videoTtl, audioTtl, nativeTtl);
}

@Bean
BidResponseCreator bidResponseCreator(
CoreCacheService coreCacheService,
Expand All @@ -804,7 +815,8 @@ BidResponseCreator bidResponseCreator(
Clock clock,
JacksonMapper mapper,
@Value("${cache.banner-ttl-seconds:#{null}}") Integer bannerCacheTtl,
@Value("${cache.video-ttl-seconds:#{null}}") Integer videoCacheTtl) {
@Value("${cache.video-ttl-seconds:#{null}}") Integer videoCacheTtl,
CacheDefaultTtlProperties cacheDefaultTtlProperties) {

return new BidResponseCreator(
coreCacheService,
Expand All @@ -819,7 +831,8 @@ BidResponseCreator bidResponseCreator(
truncateAttrChars,
clock,
mapper,
CacheTtl.of(bannerCacheTtl, videoCacheTtl));
CacheTtl.of(bannerCacheTtl, videoCacheTtl),
cacheDefaultTtlProperties);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.prebid.server.spring.config.model;

import lombok.Value;

@Value(staticConstructor = "of")
public class CacheDefaultTtlProperties {

Integer bannerTtl;

Integer videoTtl;

Integer audioTtl;

Integer nativeTtl;
}
Loading

0 comments on commit 3163a32

Please sign in to comment.