|
@@ -1,5 +1,7 @@
|
|
|
package com.tzld.longarticle.recommend.server.service.recommend.filter.strategy;
|
|
|
|
|
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
|
+import com.tzld.longarticle.recommend.server.common.CommonThreadPoolExecutor;
|
|
|
import com.tzld.longarticle.recommend.server.mapper.aigc.AigcBaseMapper;
|
|
|
import com.tzld.longarticle.recommend.server.model.dto.Content;
|
|
|
import com.tzld.longarticle.recommend.server.model.entity.aigc.PublishContent;
|
|
@@ -8,11 +10,13 @@ import com.tzld.longarticle.recommend.server.service.recommend.filter.FilterResu
|
|
|
import com.tzld.longarticle.recommend.server.service.recommend.filter.FilterStrategy;
|
|
|
import com.tzld.longarticle.recommend.server.util.DateUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
@@ -22,19 +26,41 @@ public class TodayPublishStrategy implements FilterStrategy {
|
|
|
@Autowired
|
|
|
private AigcBaseMapper aigcBaseMapper;
|
|
|
|
|
|
+ private final static ExecutorService pool = new CommonThreadPoolExecutor(
|
|
|
+ 8,
|
|
|
+ 8,
|
|
|
+ 0L, TimeUnit.SECONDS,
|
|
|
+ new LinkedBlockingQueue<>(100),
|
|
|
+ new ThreadFactoryBuilder().setNameFormat("TodayPublishStrategy-%d").build(),
|
|
|
+ new ThreadPoolExecutor.AbortPolicy());
|
|
|
+
|
|
|
@Override
|
|
|
public FilterResult filter(FilterParam param) {
|
|
|
+ Long start = System.currentTimeMillis();
|
|
|
FilterResult filterResult = new FilterResult();
|
|
|
- List<String> result = new ArrayList<>(param.getContents().size());
|
|
|
+ List<String> result = Collections.synchronizedList(param.getContents().stream().map(Content::getId).collect(Collectors.toList()));
|
|
|
Long todayStart = DateUtils.getTodayStart();
|
|
|
List<PublishContent> todayPublishContentList = aigcBaseMapper.getTodayPublishContentList(param.getAccountId(), todayStart);
|
|
|
- List<String> todayPublishContentIdList = todayPublishContentList.stream().map(PublishContent::getId).collect(Collectors.toList());
|
|
|
- for (Content content : param.getContents()) {
|
|
|
- if (!todayPublishContentIdList.contains(content.getId())) {
|
|
|
- result.add(content.getId());
|
|
|
+ if (CollectionUtils.isNotEmpty(todayPublishContentList)) {
|
|
|
+ List<String> todayPublishContentIdList = todayPublishContentList.stream().map(PublishContent::getId).collect(Collectors.toList());
|
|
|
+ CountDownLatch cdl = new CountDownLatch(todayPublishContentIdList.size());
|
|
|
+ for (String todayPublishContentId : todayPublishContentIdList) {
|
|
|
+ pool.submit(() -> {
|
|
|
+ try {
|
|
|
+ result.remove(todayPublishContentId);
|
|
|
+ } finally {
|
|
|
+ cdl.countDown();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ cdl.await();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("TodayPublishStrategy filter error", e);
|
|
|
}
|
|
|
}
|
|
|
filterResult.setContentIds(result);
|
|
|
+ log.info("TodayPublishStrategy filter cost time:{}", System.currentTimeMillis() - start);
|
|
|
return filterResult;
|
|
|
}
|
|
|
|