github.com/prebid/prebid-server@v0.275.0/adservertargeting/reqcache.go (about) 1 package adservertargeting 2 3 import ( 4 "encoding/json" 5 6 "github.com/buger/jsonparser" 7 "github.com/prebid/openrtb/v19/openrtb2" 8 ) 9 10 type requestCache struct { 11 resolvedReq json.RawMessage 12 impsData []json.RawMessage 13 } 14 15 func (reqImpCache *requestCache) GetReqJson() []byte { 16 return reqImpCache.resolvedReq 17 } 18 19 func (reqImpCache *requestCache) GetImpsData() ([]json.RawMessage, error) { 20 if len(reqImpCache.impsData) == 0 { 21 imps, _, _, err := jsonparser.Get(reqImpCache.resolvedReq, "imp") 22 if err != nil { 23 return nil, err 24 } 25 var impsData []json.RawMessage 26 27 err = json.Unmarshal(imps, &impsData) 28 if err != nil { 29 return nil, err 30 } 31 reqImpCache.impsData = impsData 32 } 33 return reqImpCache.impsData, nil 34 } 35 36 type bidsCache struct { 37 // bidder name is another layer to avoid collisions in case bid ids from different bidders will be the same 38 // map[bidder name] map [bid id] bid data 39 bids map[string]map[string][]byte 40 } 41 42 func (bidsCache *bidsCache) GetBid(bidderName, bidId string, bid openrtb2.Bid) ([]byte, error) { 43 44 _, seatBidExists := bidsCache.bids[bidderName] 45 if !seatBidExists { 46 impToBid := make(map[string][]byte, 0) 47 bidsCache.bids[bidderName] = impToBid 48 } 49 _, bidExists := bidsCache.bids[bidderName][bidId] 50 if !bidExists { 51 bidBytes, err := json.Marshal(bid) 52 if err != nil { 53 return nil, err 54 } 55 bidsCache.bids[bidderName][bidId] = bidBytes 56 } 57 return bidsCache.bids[bidderName][bidId], nil 58 }