[Spring cloud 一步步实现广告系统] 17. 根据流量类型查询广告
廣告檢索服務(wù)
功能介紹
媒體方(手機APP打開的展示廣告,走在路上看到的大屏幕廣告等等)
請求數(shù)據(jù)對象實現(xiàn)
從上圖我們可以看出,在媒體方向我們的廣告檢索系統(tǒng)發(fā)起請求的時候,請求中會有很多的請求參數(shù)信息,他們分為了三個部分,我們來編碼封裝這幾個參數(shù)對象信息以及我們請求本身的信息。Let's code.
- 創(chuàng)建廣告檢索請求接口
- 創(chuàng)建SearchRequest,包含三部分:mediaId,RequestInfo,FeatureInfo
其他的對象大家可以去github傳送門 & gitee傳送門 下載源碼。
檢索響應(yīng)對象定義
/*** SearchResponse for 檢索API響應(yīng)對象** @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>*/ @Data @Builder @NoArgsConstructor @AllArgsConstructor public class SearchResponse {//一個廣告位,可以展示多個廣告//Map key為廣告位 AdSlot#adSlotCodepublic Map<String, List<Creative>> adSlotRelationAds = new HashMap<>();@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic static class Creative {private Long adId;private String adUrl;private Integer width;private Integer height;private Integer type;private Integer materialType;//展示監(jiān)控urlprivate List<String> showMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");//點擊監(jiān)控urlprivate List<String> clickMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");}/*** 我們的檢索服務(wù)針對的是內(nèi)存中的索引檢索,那么我們就需要一個轉(zhuǎn)換方法*/public static Creative convert(CreativeIndexObject object) {return Creative.builder().adId(object.getAdId()).adUrl(object.getAdUrl()).width(object.getWidth()).height(object.getHeight()).type(object.getType()).materialType(object.getMaterialType()).build();} }根據(jù)流量類型廣告過濾
流量類型本身屬于推廣單元下的類目,有很多種類貼片廣告,開屏廣告等等,這些類型需要同步到媒體方,媒體方會根據(jù)不同的流量類型發(fā)起不同的廣告請求,我們需要先定義一個流量類型的信息類。
public class AdUnitConstants {public static class PositionType{//App啟動時展示的、展示時間短暫的全屏化廣告形式。private static final int KAIPING = 1;//電影開始之前的廣告private static final int TIEPIAN = 2;//電影播放中途廣告private static final int TIEPIAN_MIDDLE = 4;//暫停視頻時候播放的廣告private static final int TIEPIAN_PAUSE = 8;//視頻播放完private static final int TIEPIAN_POST = 16;} }從上述類型的數(shù)字,我們可以看出是2的倍數(shù),這是為了使用位運算提升性能。
在com.sxzhongf.ad.index.adunit.AdUnitIndexObject中,我們添加類型校驗方法:
public static boolean isAdSlotType(int adSlotType, int positionType) {switch (adSlotType) {case AdUnitConstants.PositionType.KAIPING:return isKaiPing(positionType);case AdUnitConstants.PositionType.TIEPIAN:return isTiePian(positionType);case AdUnitConstants.PositionType.TIEPIAN_MIDDLE:return isTiePianMiddle(positionType);case AdUnitConstants.PositionType.TIEPIAN_PAUSE:return isTiePianPause(positionType);case AdUnitConstants.PositionType.TIEPIAN_POST:return isTiePianPost(positionType);default:return false;}}/*** 與運算,低位取等,高位補零。* 如果 > 0,則為開屏*/private static boolean isKaiPing(int positionType) {return (positionType & AdUnitConstants.PositionType.KAIPING) > 0;}private static boolean isTiePianMiddle(int positionType) {return (positionType & AdUnitConstants.PositionType.TIEPIAN_MIDDLE) > 0;}private static boolean isTiePianPause(int positionType) {return (positionType & AdUnitConstants.PositionType.TIEPIAN_PAUSE) > 0;}private static boolean isTiePianPost(int positionType) {return (positionType & AdUnitConstants.PositionType.TIEPIAN_POST) > 0;}private static boolean isTiePian(int positionType) {return (positionType & AdUnitConstants.PositionType.TIEPIAN) > 0;}無所如何,我們都是需要根據(jù)positionType進行數(shù)據(jù)查詢過濾,我們在之前的com.sxzhongf.ad.index.adunit.AdUnitIndexAwareImpl中添加2個方法來實現(xiàn)過濾:
/*** 過濾當前是否存在滿足positionType的UnitIds*/public Set<Long> match(Integer positionType) {Set<Long> adUnitIds = new HashSet<>();objectMap.forEach((k, v) -> {if (AdUnitIndexObject.isAdSlotType(positionType, v.getPositionType())) {adUnitIds.add(k);}});return adUnitIds;}/*** 根據(jù)UnitIds查詢AdUnit list*/public List<AdUnitIndexObject> fetch(Collection<Long> adUnitIds) {if (CollectionUtils.isEmpty(adUnitIds)) {return Collections.EMPTY_LIST;}List<AdUnitIndexObject> result = new ArrayList<>();adUnitIds.forEach(id -> {AdUnitIndexObject object = get(id);if (null == object) {log.error("AdUnitIndexObject does not found:{}", id);return;}result.add(object);});return result;}- 實現(xiàn)Search服務(wù)接口
上述我們準備了一系列的查詢方法,都是為了根據(jù)流量類型查詢廣告單元信息,我們現(xiàn)在開始實現(xiàn)我們的查詢接口,查詢接口中,我們可以獲取到媒體方的請求對象信息,它帶有一系列查詢所需要的過濾參數(shù):
/*** SearchImpl for 實現(xiàn)search 服務(wù)** @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>*/ @Service @Slf4j public class SearchImpl implements ISearch {@Overridepublic SearchResponse fetchAds(SearchRequest request) {//獲取請求廣告位信息List<AdSlot> adSlotList = request.getRequestInfo().getAdSlots();//獲取三個Feature信息KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue();DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();//Feature關(guān)系FeatureRelation featureRelation = request.getFeatureInfo().getRelation();//構(gòu)造響應(yīng)對象SearchResponse response = new SearchResponse();Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();for (AdSlot adSlot : adSlotList) {Set<Long> targetUnitIdSet;//根據(jù)流量類型從緩存中獲取 初始 廣告信息Set<Long> adUnitIdSet = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).match(adSlot.getPositionType());}return null;} }轉(zhuǎn)載于:https://www.cnblogs.com/zhangpan1244/p/11343026.html
總結(jié)
以上是生活随笔為你收集整理的[Spring cloud 一步步实现广告系统] 17. 根据流量类型查询广告的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 闭包函数 装饰器 迭代器
- 下一篇: jQuery 轮播图