|
@@ -1,21 +1,27 @@
|
|
|
package com.tzld.piaoquan.recommend.server.service.filter.strategy;
|
|
|
|
|
|
+import com.google.common.cache.CacheBuilder;
|
|
|
+import com.google.common.cache.CacheLoader;
|
|
|
+import com.google.common.cache.LoadingCache;
|
|
|
import com.google.common.collect.Lists;
|
|
|
+import com.tzld.piaoquan.recommend.server.repository.WxVideoTagRel;
|
|
|
+import com.tzld.piaoquan.recommend.server.repository.WxVideoTagRelRepository;
|
|
|
import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
|
|
|
import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
|
|
|
+import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.commons.lang.math.NumberUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author dyp
|
|
@@ -26,44 +32,38 @@ public class TagStrategy implements FilterStrategy {
|
|
|
@Autowired
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private WxVideoTagRelRepository repository;
|
|
|
+
|
|
|
@Value("${video.filter.tagids:}")
|
|
|
private String videoFilterTagIds;
|
|
|
|
|
|
- private Map<Long, Set<Long>> videoTagCache = new ConcurrentHashMap<>();
|
|
|
+ // 内存持久保存不淘汰
|
|
|
+ private LoadingCache<Long, Set<Long>> videoTagCache = CacheBuilder.newBuilder()
|
|
|
+ .maximumSize(100)
|
|
|
+ .refreshAfterWrite(60, TimeUnit.SECONDS)
|
|
|
+ .expireAfterWrite(60, TimeUnit.SECONDS)
|
|
|
+ .expireAfterAccess(60, TimeUnit.SECONDS)
|
|
|
+ .build(new CacheLoader<Long, Set<Long>>() {
|
|
|
+ @Override
|
|
|
+ public Set<Long> load(Long tagId) {
|
|
|
+ List<WxVideoTagRel> rels = repository.findAllByTagId(tagId);
|
|
|
+ return CommonCollectionUtils.toSet(rels, WxVideoTagRel::getVideoId);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
- initCacheByValue();
|
|
|
- }
|
|
|
-
|
|
|
- @Scheduled(cron = "0 0 0/1 * * ? ")
|
|
|
- public void cornInit() {
|
|
|
- initCacheByValue();
|
|
|
- }
|
|
|
-
|
|
|
- public void initCacheByValue() {
|
|
|
- long l = System.currentTimeMillis();
|
|
|
- if (org.apache.commons.lang.StringUtils.isNotBlank(videoFilterTagIds)) {
|
|
|
+ if (StringUtils.isNotBlank(videoFilterTagIds)) {
|
|
|
Map<Long, Set<Long>> tmp = new ConcurrentHashMap<>();
|
|
|
String[] tags = videoFilterTagIds.split(",");
|
|
|
for (String tag : tags) {
|
|
|
if (StringUtils.isBlank(tag)) {
|
|
|
continue;
|
|
|
}
|
|
|
- Long tagId = Long.valueOf(tag);
|
|
|
- WxVideoTagRelExample wxVideoTagRelExample = new WxVideoTagRelExample();
|
|
|
- wxVideoTagRelExample.createCriteria().andTagIdEqualTo(tagId);
|
|
|
- List<WxVideoTagRel> wxVideoTagRels = wxVideoTagRelMapper.selectByExample(wxVideoTagRelExample);
|
|
|
- tmp.put(tagId,
|
|
|
- wxVideoTagRels.stream().map(WxVideoTagRel::getVideoId).collect(Collectors.toSet()));
|
|
|
+ videoTagCache.get(NumberUtils.toLong(tag));
|
|
|
}
|
|
|
- videoTagCache = tmp;
|
|
|
- } else {
|
|
|
- videoTagCache = new ConcurrentHashMap<>();
|
|
|
}
|
|
|
- long useTime = System.currentTimeMillis() - l;
|
|
|
- log.info("update filter video tags data success,all data size:" +
|
|
|
- videoTagCache.keySet().size() + ",time:" + useTime);
|
|
|
}
|
|
|
|
|
|
@Override
|