浏览代码

remove tair

丁云鹏 1 年之前
父节点
当前提交
b51f5139d3

+ 26 - 33
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/service/AbstractFeatureService.java

@@ -7,7 +7,6 @@ import com.google.common.reflect.TypeToken;
 import com.tzld.piaoquan.recommend.feature.util.CommonCollectionUtils;
 import com.tzld.piaoquan.recommend.feature.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -91,10 +90,10 @@ public abstract class AbstractFeatureService<K, V> {
 
     private V load(K key) {
         V feature = getFromCache(key);
-        if (feature == null) {
-            feature = getFromSource(key);
-            saveToCache(key, feature);
-        }
+//        if (feature == null) {
+//            feature = getFromSource(key);
+//            saveToCache(key, feature);
+//        }
         return feature;
     }
 
@@ -105,23 +104,18 @@ public abstract class AbstractFeatureService<K, V> {
     }
 
     private V getFromSource(K key) {
-        return null;
-//        String sourceKey = cacheKey(key);
-//        String value = tairTemplate.opsForValue().get(sourceKey);
-//        return JSONUtils.fromJson(value, typeToken, null);
+        String sourceKey = cacheKey(key);
+        String value = tairTemplate.opsForValue().get(sourceKey);
+        return JSONUtils.fromJson(value, typeToken, null);
     }
 
     private void saveToCache(K key, V value) {
         String cacheKey = cacheKey(key);
-        String cacheValue = value == null
-                ? emptyData
-                : JSONUtils.toJson(value);
-        long expire = value == null
-                ? emptyDataExpire
-                : defaultExpire;
-
-        // TODO 评估过期时间
-        redisTemplate.opsForValue().set(cacheKey, cacheValue, expire, TimeUnit.SECONDS);
+        if (value == null) {
+            redisTemplate.opsForValue().set(cacheKey, emptyData, emptyDataExpire, TimeUnit.SECONDS);
+        } else {
+            redisTemplate.opsForValue().set(cacheKey, JSONUtils.toJson(value), defaultExpire, TimeUnit.SECONDS);
+        }
     }
 
     private Map<K, V> loadAll(Iterable<? extends K> keyIte) {
@@ -145,11 +139,11 @@ public abstract class AbstractFeatureService<K, V> {
         }
 
         Map<K, V> sourceFeatureMap = null;
-        if (CollectionUtils.isNotEmpty(keys)) {
-            sourceFeatureMap = getFromSource(keys);
-            // TODO 可异步
-            saveToCache(keys, sourceFeatureMap);
-        }
+//        if (CollectionUtils.isNotEmpty(keys)) {
+//            sourceFeatureMap = getFromSource(keys);
+//            // TODO 可异步
+//            saveToCache(keys, sourceFeatureMap);
+//        }
 
 
         return CommonCollectionUtils.merge(cacheFeatureMap, sourceFeatureMap);
@@ -168,16 +162,15 @@ public abstract class AbstractFeatureService<K, V> {
     }
 
     private Map<K, V> getFromSource(List<K> keys) {
-        return Collections.emptyMap();
-//        List<String> redisKeys = CommonCollectionUtils.toList(keys, this::cacheKey);
-//        List<String> values = tairTemplate.opsForValue().multiGet(redisKeys);
-//        Map<K, V> result = new HashMap<>();
-//        for (int i = 0; i < keys.size(); i++) {
-//            if (values.get(i) != null) {
-//                result.put(keys.get(i), JSONUtils.fromJson(values.get(i), typeToken, null));
-//            }
-//        }
-//        return result;
+        List<String> redisKeys = CommonCollectionUtils.toList(keys, this::cacheKey);
+        List<String> values = tairTemplate.opsForValue().multiGet(redisKeys);
+        Map<K, V> result = new HashMap<>();
+        for (int i = 0; i < keys.size(); i++) {
+            if (values.get(i) != null) {
+                result.put(keys.get(i), JSONUtils.fromJson(values.get(i), typeToken, null));
+            }
+        }
+        return result;
     }
 
     private void saveToCache(List<K> keys, Map<K, V> map) {

+ 2 - 2
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/service/UserFeatureService.java

@@ -18,8 +18,8 @@ public class UserFeatureService extends AbstractFeatureService<String, UserFeatu
 
     public UserFeatureService() {
         super.emptyData = "{}";
-        super.emptyDataExpire = -1;
-        super.defaultExpire = -1;
+        super.emptyDataExpire = 0;
+        super.defaultExpire = 0;
         super.keyFunc = k -> String.format("user:%s", k);
         super.typeToken = new TypeToken<UserFeature>() {
         };

+ 2 - 2
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/service/VideoFeatureService.java

@@ -25,8 +25,8 @@ public class VideoFeatureService extends AbstractFeatureService<String, VideoFea
 
     public VideoFeatureService() {
         super.emptyData = "{}";
-        super.emptyDataExpire = -1;
-        super.defaultExpire = -1;
+        super.emptyDataExpire = 0;
+        super.defaultExpire = 0;
         super.keyFunc = k -> String.format("video:%s", k);
         super.typeToken = new TypeToken<VideoFeature>() {
         };