实现创意轮播
广告投放时有时候需要进行创意轮播,系统将平分各创意展现量,便于广告主比较各创意投放效果,可以使用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); 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); redisController.addExpose("uid","dealId",crativeId); }
|