|
@@ -1,8 +1,5 @@
|
|
|
package com.tzld.piaoquan.recommend.server.service;
|
|
|
|
|
|
-import com.google.common.cache.CacheBuilder;
|
|
|
-import com.google.common.cache.CacheLoader;
|
|
|
-import com.google.common.cache.LoadingCache;
|
|
|
import com.tzld.piaoquan.recommend.server.model.Video;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
@@ -32,35 +29,17 @@ public class PreViewedService {
|
|
|
|
|
|
public static final String KEY_PRE_VIEWED_FORMAT = "previewed:videos:%s:%s";
|
|
|
|
|
|
- private LoadingCache<String, Set<Long>> preViewedCache = CacheBuilder.newBuilder()
|
|
|
- .maximumSize(100000) // TODO 评估容量
|
|
|
- .refreshAfterWrite(10, TimeUnit.SECONDS)
|
|
|
- .expireAfterWrite(10, TimeUnit.SECONDS)
|
|
|
- .expireAfterAccess(10, TimeUnit.SECONDS)
|
|
|
- .build(new CacheLoader<String, Set<Long>>() {
|
|
|
- @Override
|
|
|
- public Set<Long> load(String key) throws Exception {
|
|
|
- Set<Long> result = new HashSet<>();
|
|
|
- Cursor<String> cursor = redisTemplate.opsForSet().scan(key, ScanOptions.NONE);
|
|
|
- try {
|
|
|
- while (cursor != null && cursor.hasNext()) {
|
|
|
- Long videoId = NumberUtils.toLong(cursor.next(), 0);
|
|
|
- result.add(videoId);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("sScan", e);
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (cursor != null && !cursor.isClosed()) {
|
|
|
- cursor.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("sScan", e);
|
|
|
- }
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
- });
|
|
|
+// private LoadingCache<String, Set<Long>> preViewedCache = CacheBuilder.newBuilder()
|
|
|
+// .maximumSize(100000) // TODO 评估容量
|
|
|
+// .refreshAfterWrite(10, TimeUnit.SECONDS)
|
|
|
+// .expireAfterWrite(10, TimeUnit.SECONDS)
|
|
|
+// .expireAfterAccess(10, TimeUnit.SECONDS)
|
|
|
+// .build(new CacheLoader<String, Set<Long>>() {
|
|
|
+// @Override
|
|
|
+// public Set<Long> load(String key) {
|
|
|
+// return getVideoIdsFromCache(key);
|
|
|
+// }
|
|
|
+// });
|
|
|
|
|
|
public void updateCache(int appType, String mid, List<Video> videos) {
|
|
|
if (StringUtils.isBlank(mid)
|
|
@@ -83,11 +62,36 @@ public class PreViewedService {
|
|
|
}
|
|
|
String key = String.format(KEY_PRE_VIEWED_FORMAT, appType, mid);
|
|
|
try {
|
|
|
- return preViewedCache.get(key);
|
|
|
+
|
|
|
+ //return preViewedCache.get(key);
|
|
|
+ // 预曝光要求实时性,不适合本地缓存
|
|
|
+ return getVideoIdsFromCache(key);
|
|
|
} catch (Exception e) {
|
|
|
log.error("get pre viewed cache error! key={}", key, e);
|
|
|
}
|
|
|
return Collections.emptySet();
|
|
|
}
|
|
|
|
|
|
+ private Set<Long> getVideoIdsFromCache(String key) {
|
|
|
+ Set<Long> result = new HashSet<>();
|
|
|
+ Cursor<String> cursor = redisTemplate.opsForSet().scan(key, ScanOptions.NONE);
|
|
|
+ try {
|
|
|
+ while (cursor != null && cursor.hasNext()) {
|
|
|
+ Long videoId = NumberUtils.toLong(cursor.next(), 0);
|
|
|
+ result.add(videoId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("sScan", e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (cursor != null && !cursor.isClosed()) {
|
|
|
+ cursor.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("sScan", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|