[Spring cloud 一步步实现广告系统] 17. 根据流量类型查询广告

  • 时间:
  • 浏览:2
  • 来源:大发pk10_pk10漏洞_大发pk10漏洞

广告检索服务

功能介绍

媒体方(手机APP打开的展示广告,走在路上看后的大屏幕广告等等)

请求数据对象实现

从上图有人可不还可不可不都可否看出,在媒体方向有人的广告检索系统发起请求的之后 ,请求中会有也不的请求参数信息,有人分为了另三个多多每种,有人来编码封装这哪几个参数对象信息以及有人请求有一种的信息。Let's code.

  • 创建广告检索请求接口

/**
 * ISearch for 请求接口,
 * 根据广告请求对象,获取广告响应信息
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@FunctionalInterface
public interface ISearch {

    /**
     * 根据请求返回广告结果
     */
    SearchResponse fetchAds(SearchRequest request);
}
  • 创建SearchRequest,含高三每种:mediaId,RequestInfo,FeatureInfo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchRequest {

    //媒体方请求标示
    private String mediaId;
    //请求基本信息
    private RequestInfo requestInfo;
    //匹配信息
    private FeatureInfo featureInfo;


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RequestInfo {
        private String requestId;

        private List<AdSlot> adSlots;
        private App app;
        private Geo geo;
        private Device device;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FeatureInfo {

        private KeywordFeature keywordFeature;
        private DistrictFeature districtFeature;
        private HobbyFeatrue hobbyFeatrue;

        private FeatureRelation relation = FeatureRelation.AND;
    }
}

某些的对象有人可不还可不可不都可否去github传送门 & gitee传送门 下载源码。

检索响应对象定义
/**
 * SearchResponse for 检索API响应对象
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchResponse {

    //另三个多多广告位,可不还可不可不都可否展示多个广告
    //Map key为广告位 AdSlot#adSlotCode
    public Map<String, List<Creative>> adSlotRelationAds = new HashMap<>();

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Creative {

        private Long adId;
        private String adUrl;
        private Integer width;
        private Integer height;
        private Integer type;
        private Integer materialType;

        //展示监控url
        private List<String> showMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
        //点击监控url
        private List<String> clickMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
    }

    /**
     * 有人的检索服务针对的是内存中的索引检索,越来越有人就前要另三个多多转换最好的办法
     */
    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();
    }
}
根据流量类型广告过滤

流量类型有一种属于推广单元下的类目,有也不种类贴片广告,开屏广告等等,那些类型前要同步到媒体方,媒体方会根据不同的流量类型发起不同的广告请求,有人前要先定义另三个多多流量类型的信息类。

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;
    }
}

从上述类型的数字,有人可不还可不可不都可否看出是2的倍数,这是为了使用位运算提升性能。

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;
    }

无所怎样,有人全部都是 前要根据positionType进行数据查询过滤,有人在之后 的com.sxzhongf.ad.index.adunit.AdUnitIndexAwareImpl中去掉 另三个多多最好的办法来实现过滤:

/**
     * 过滤当前否是发生满足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;
    }

    /**
     * 根据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;
    }
  • 实现Search服务接口

上述有人准备了一系列的查询最好的办法,全部都是 为了根据流量类型查询广告单元信息,有人现在之后 之后 刚开始实现有人的查询接口,查询接口中,有人可不还可不可不都可否获取到媒体方的请求对象信息,它含高一系列查询所前要的过滤参数:

/**
 * SearchImpl for 实现search 服务
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@Service
@Slf4j
public class SearchImpl implements ISearch {
    @Override
    public 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关系
        FeatureRelation featureRelation = request.getFeatureInfo().getRelation();


        //构造响应对象
        SearchResponse response = new SearchResponse();
        Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();

        for (AdSlot adSlot : adSlotList) {
            Set<Long> targetUnitIdSet;
            //根据流量类型从缓存中获取 初始 广告信息
            Set<Long> adUnitIdSet = IndexDataTableUtils.of(
                    AdUnitIndexAwareImpl.class
            ).match(adSlot.getPositionType());
        }
        return null;
    }
}