0%

实现创意轮播

实现创意轮播

广告投放时有时候需要进行创意轮播,系统将平分各创意展现量,便于广告主比较各创意投放效果,可以使用redis来实现这个需求。

创意是与投放单元绑定的,我们可以使用uid:deal来作为redis的key,那存储的内容是什么呢?由于要进行创意轮播,那就是存储创意id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 添加曝光
private void addExpose(String uid, String deal,long creativeId){
String key = uid+":"+deal;
jedis.lpush(key, String.valueOf(creativeId));
}

// 获取最后一次播放的创意
private Long getLastCreative(String uid, String deal) {
String key = uid+":"+deal;

// 取最后一条
String creativeId = jedis.lindex(key, 0);

if (creativeId == null || creativeId.isEmpty()) {
return null;
} else {
return Long.parseLong(creativeId);
}
}

此时假设该投放单元有三条创意,id分别为100,101,102

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
RedisController redisController = new RedisController();
// 模拟三条创意
List<Long> creativeIds = new ArrayList<>();
creativeIds.add(100L);
creativeIds.add(101L);
creativeIds.add(102L);

Long lastCreative = redisController.getLastCreative("uid", "dealId");
System.out.println(lastCreative);
if(lastCreative == null){ // 第一次曝光
Long crativeId = creativeIds.get(0);
// 曝光操作 doSomething
redisController.addExpose("uid","dealId",crativeId);
} else { // 不是第一次曝光
int lastIndex = 0;
for(int i = 0;i<creativeIds.size();i++){
if(creativeIds.get(i).equals(lastCreative)){
lastIndex = i;
break;
}
}
int index = (lastIndex + 1) % creativeIds.size();
Long crativeId = creativeIds.get(index);
// 曝光操作 doSomething
redisController.addExpose("uid","dealId",crativeId);
}

欢迎关注我的其它发布渠道