@@ -63,6 +63,8 @@ public class CoreCacheService {
6363 private static final Logger logger = LoggerFactory .getLogger (CoreCacheService .class );
6464
6565 private static final String BID_WURL_ATTRIBUTE = "wurl" ;
66+ private static final String TRACE_INFO_SEPARATOR = "-" ;
67+ private static final int DATACENTER_REGION_LENGTH = 4 ;
6668
6769 private final HttpClient httpClient ;
6870 private final URL endpointUrl ;
@@ -78,13 +80,18 @@ public class CoreCacheService {
7880 private final MultiMap cacheHeaders ;
7981 private final Map <String , List <String >> debugHeaders ;
8082
83+ private final boolean appendTraceInfoToCacheId ;
84+ private final String datacenterRegion ;
85+
8186 public CoreCacheService (
8287 HttpClient httpClient ,
8388 URL endpointUrl ,
8489 String cachedAssetUrlTemplate ,
8590 long expectedCacheTimeMs ,
8691 String apiKey ,
8792 boolean isApiKeySecured ,
93+ boolean appendTraceInfoToCacheId ,
94+ String datacenterRegion ,
8895 VastModifier vastModifier ,
8996 EventsService eventsService ,
9097 Metrics metrics ,
@@ -107,6 +114,9 @@ public CoreCacheService(
107114 ? HttpUtil .headers ().add (HttpUtil .X_PBC_API_KEY_HEADER , Objects .requireNonNull (apiKey ))
108115 : HttpUtil .headers ();
109116 debugHeaders = HttpUtil .toDebugHeaders (cacheHeaders );
117+
118+ this .appendTraceInfoToCacheId = appendTraceInfoToCacheId ;
119+ this .datacenterRegion = normalizeDatacenterRegion (datacenterRegion );
110120 }
111121
112122 public String getEndpointHost () {
@@ -211,6 +221,7 @@ private List<CachedCreative> updatePutObjects(List<BidPutObject> bidPutObjects,
211221 .bidid (null )
212222 .bidder (null )
213223 .timestamp (null )
224+ .key (resolveCacheKey (accountId , putObject .getKey ()))
214225 .value (vastModifier .modifyVastXml (isEventsEnabled ,
215226 allowedBidders ,
216227 putObject ,
@@ -268,7 +279,8 @@ private Future<CacheServiceResult> doCacheOpenrtb(List<CacheBid> bids,
268279 final List <CachedCreative > cachedCreatives = Stream .concat (
269280 bids .stream ().map (cacheBid ->
270281 createJsonPutObjectOpenrtb (cacheBid , accountId , eventsContext )),
271- videoBids .stream ().map (videoBid -> createXmlPutObjectOpenrtb (videoBid , requestId , hbCacheId )))
282+ videoBids .stream ().map (videoBid ->
283+ createXmlPutObjectOpenrtb (videoBid , requestId , hbCacheId , accountId )))
272284 .collect (Collectors .toCollection (ArrayList ::new ));
273285
274286 if (cachedCreatives .isEmpty ()) {
@@ -385,26 +397,34 @@ private CachedCreative createJsonPutObjectOpenrtb(CacheBid cacheBid,
385397 bidObjectNode .put (BID_WURL_ATTRIBUTE , eventUrl );
386398 }
387399
400+ final String resolvedCacheKey = resolveCacheKey (accountId );
401+
388402 final BidPutObject payload = BidPutObject .builder ()
389403 .aid (eventsContext .getAuctionId ())
390404 .type ("json" )
405+ .key (resolvedCacheKey )
391406 .value (bidObjectNode )
392407 .ttlseconds (cacheBid .getTtl ())
393408 .build ();
394409
395410 return CachedCreative .of (payload , creativeSizeFromAdm (bid .getAdm ()));
396411 }
397412
398- private CachedCreative createXmlPutObjectOpenrtb (CacheBid cacheBid , String requestId , String hbCacheId ) {
413+ private CachedCreative createXmlPutObjectOpenrtb (CacheBid cacheBid ,
414+ String requestId ,
415+ String hbCacheId ,
416+ String accountId ) {
399417 final BidInfo bidInfo = cacheBid .getBidInfo ();
400418 final Bid bid = bidInfo .getBid ();
401419 final String vastXml = bid .getAdm ();
402420
403- final String customCacheKey = resolveCustomCacheKey (hbCacheId , bidInfo .getCategory ());
421+ final String resolvedCacheKey = resolveCacheKey (accountId , hbCacheId );
422+ final String customCacheKey = resolveCustomCacheKey (resolvedCacheKey , bidInfo .getCategory ());
404423
405424 final BidPutObject payload = BidPutObject .builder ()
406425 .aid (requestId )
407426 .type ("xml" )
427+ .key (customCacheKey != null ? customCacheKey : resolvedCacheKey )
408428 .value (vastXml != null ? new TextNode (vastXml ) : null )
409429 .ttlseconds (cacheBid .getTtl ())
410430 .key (customCacheKey )
@@ -553,4 +573,48 @@ private BidCacheRequest toBidCacheRequest(List<CachedCreative> cachedCreatives)
553573 .map (CachedCreative ::getPayload )
554574 .toList ());
555575 }
576+
577+ private String resolveCacheKey (String accountId ) {
578+ return resolveCacheKey (accountId , null );
579+ }
580+
581+ // hbCacheId is accepted here to have a backwards-compatibility with video category mapping
582+ private String resolveCacheKey (String accountId , String hbCacheId ) {
583+ if (!appendTraceInfoToCacheId ) {
584+ return hbCacheId ;
585+ }
586+
587+ // no need to have additional separator if datacenter name won't be added
588+ final boolean isDatacenterNamePopulated = StringUtils .isNotBlank (datacenterRegion );
589+ final int separatorCount = isDatacenterNamePopulated ? 2 : 1 ;
590+ final int accountIdLength = accountId .length ();
591+ final int traceInfoLength = isDatacenterNamePopulated
592+ ? accountIdLength + DATACENTER_REGION_LENGTH + separatorCount
593+ : accountIdLength + separatorCount ;
594+
595+ final String cacheKey = hbCacheId != null ? hbCacheId : idGenerator .generateId ();
596+ if (cacheKey != null && traceInfoLength < cacheKey .length ()) {
597+ final String substring = cacheKey .substring (0 , cacheKey .length () - traceInfoLength );
598+ return isDatacenterNamePopulated
599+ ? accountId + TRACE_INFO_SEPARATOR + datacenterRegion + TRACE_INFO_SEPARATOR + substring
600+ : accountId + TRACE_INFO_SEPARATOR + substring ;
601+ } else {
602+ return hbCacheId ;
603+ }
604+ }
605+
606+ private String normalizeDatacenterRegion (String datacenterRegion ) {
607+ if (datacenterRegion == null ) {
608+ return null ;
609+ }
610+
611+ return switch (datacenterRegion ) {
612+ case "eu-west" -> "ams3" ;
613+ case "eu-central" -> "fra2" ;
614+ case "us-east" -> "iad2" ;
615+ case "us-west" -> "sjc2" ;
616+ case "apac" -> "sin1" ;
617+ default -> datacenterRegion .length () > 4 ? datacenterRegion .substring (0 , 4 ) : datacenterRegion ;
618+ };
619+ }
556620}
0 commit comments