|
|
@@ -0,0 +1,85 @@
|
|
|
+package com.tzld.piaoquan.ad.engine.commons.helper;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class CreativeKFinalDataHelper {
|
|
|
+
|
|
|
+ @Resource(name = "newAdRedisTemplate")
|
|
|
+ private RedisTemplate<String, String> newAdRedisTemplate;
|
|
|
+
|
|
|
+ private static final String CREATIVE_CODE_SET_KEY_FORMAT = "ad:addv:cost:current:creative:code:%s";
|
|
|
+ private static final String CREATIVE_K_KEY_FORMAT = "ad:addv:cost:k:%s";
|
|
|
+
|
|
|
+ private volatile static Map<String, Double> creativeKFinalMap = Collections.emptyMap();
|
|
|
+
|
|
|
+ // 服务启动时初始化数据
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ Map<String, Double> map = updateCreativeKFinalMap();
|
|
|
+ creativeKFinalMap = Collections.unmodifiableMap(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 每1分钟更新一次数据
|
|
|
+ @Scheduled(fixedRate = 60 * 1000)
|
|
|
+ public void scheduledUpdate() {
|
|
|
+ Map<String, Double> map = updateCreativeKFinalMap();
|
|
|
+ creativeKFinalMap = Collections.unmodifiableMap(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double getKFinal(String creativeCode) {
|
|
|
+ if (creativeCode != null && creativeKFinalMap != null) {
|
|
|
+ return creativeKFinalMap.get(creativeCode);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Double> updateCreativeKFinalMap() {
|
|
|
+ Map<String, Double> tmpMap = new HashMap<>();
|
|
|
+ try {
|
|
|
+ String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
|
+ String setKey = String.format(CREATIVE_CODE_SET_KEY_FORMAT, today);
|
|
|
+ Set<String> creativeCodes = newAdRedisTemplate.opsForSet().members(setKey);
|
|
|
+ if (CollectionUtils.isEmpty(creativeCodes)) {
|
|
|
+ log.info("creative kFinal: code set is empty for date={}", today);
|
|
|
+ return tmpMap;
|
|
|
+ }
|
|
|
+ for (String creativeCode : creativeCodes) {
|
|
|
+ try {
|
|
|
+ String kKey = String.format(CREATIVE_K_KEY_FORMAT, creativeCode);
|
|
|
+ String value = newAdRedisTemplate.opsForValue().get(kKey);
|
|
|
+ if (StringUtils.isNotBlank(value)) {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(value);
|
|
|
+ Double kFinal = jsonObject.getDouble("kFinal");
|
|
|
+ if (kFinal != null) {
|
|
|
+ tmpMap.put(creativeCode, kFinal);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("update creative kFinal error creativeCode={}", creativeCode, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("update creative kFinal map success size={}", tmpMap.size());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("update creative kFinal map error", e);
|
|
|
+ }
|
|
|
+ return tmpMap;
|
|
|
+ }
|
|
|
+}
|