|
@@ -1,5 +1,8 @@
|
|
|
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.google.common.hash.Hashing;
|
|
|
import com.tzld.piaoquan.recommend.server.common.enums.AppTypeEnum;
|
|
@@ -12,7 +15,9 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
* @author dyp
|
|
@@ -24,15 +29,39 @@ public class AllowListStrategy implements FilterStrategy {
|
|
|
@Qualifier("longVideoRedisTemplate")
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
-
|
|
|
+ private LoadingCache<Integer, Set<String>> allowVideoCache = CacheBuilder.newBuilder()
|
|
|
+ .maximumSize(1000000)
|
|
|
+ .refreshAfterWrite(600, TimeUnit.SECONDS)
|
|
|
+ .expireAfterWrite(600, TimeUnit.SECONDS)
|
|
|
+ .expireAfterAccess(600, TimeUnit.SECONDS)
|
|
|
+ .build(new CacheLoader<Integer, Set<String>>() {
|
|
|
+ @Override
|
|
|
+ public Set<String> load(Integer idx) {
|
|
|
+ String key = VIDEO_ALLOW_LIST_BITMAP_KEY_SET_PREFIX + idx;
|
|
|
+ Set<String> result = redisTemplate.opsForSet().members(key);
|
|
|
+ if (CollectionUtils.isEmpty(result)) {
|
|
|
+ return Collections.emptySet();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
private static final String VIDEO_ALLOW_LIST_BITMAP_KEY_SET_PREFIX = "movie:videoid:allowSet:";
|
|
|
-
|
|
|
private static final String RELIGION_VIDEO_ALLOW_LIST_BITMAP_KEY = "mp:religion:allowlist:videoid:bitmap";
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+
|
|
|
+ for (int i = 0; i < 100; i++) {
|
|
|
+ int finalI = i;
|
|
|
+ allowVideoCache.getUnchecked(finalI);
|
|
|
+
|
|
|
+ }
|
|
|
+ log.info("allowVideoCache size {} ", allowVideoCache.size());
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
-
|
|
|
public List<Long> filter(FilterParam param) {
|
|
|
if (param == null
|
|
|
|| CollectionUtils.isEmpty(param.getVideoIds())) {
|
|
@@ -74,22 +103,19 @@ public class AllowListStrategy implements FilterStrategy {
|
|
|
}
|
|
|
|
|
|
private boolean isMemberOfVideoAllowList(Long videoId) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
if (Objects.isNull(videoId)) {
|
|
|
return false;
|
|
|
}
|
|
|
try {
|
|
|
- int newIdx = Math.abs(Hashing.murmur3_32().hashLong(videoId).asInt()) % 100;
|
|
|
- String newPrefix = VIDEO_ALLOW_LIST_BITMAP_KEY_SET_PREFIX + newIdx;
|
|
|
- Boolean result = redisTemplate.opsForSet().isMember(newPrefix, String.valueOf(videoId));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return result;
|
|
|
+ int idx = Math.abs(Hashing.murmur3_32().hashLong(videoId).asInt()) % 100;
|
|
|
+ Set<String> allowList = allowVideoCache.get(idx);
|
|
|
+ if (CollectionUtils.isEmpty(allowList)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return allowList.contains(String.valueOf(videoId));
|
|
|
} catch (Exception e) {
|
|
|
log.error("isMemberOfVideoAllowList error {}", videoId, e);
|
|
|
}
|