浏览代码

model def

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

+ 18 - 16
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/service/AbstractFeatureService.java

@@ -51,8 +51,8 @@ public abstract class AbstractFeatureService<K, V> {
     public AbstractFeatureService() {
         this.emptyData = "{}";
         // 单位秒
-        this.emptyDataExpire = 60;
-        this.defaultExpire = 7200;
+        this.emptyDataExpire = -1;
+        this.defaultExpire = -1;
         this.keyFunc = k -> String.valueOf(k);
 
         initLocalCache(defaultMaximumSize, defaultRefreshAfterWrite, defaultExpireAfterWrite, defaultExpireAfterAccess);
@@ -81,7 +81,7 @@ public abstract class AbstractFeatureService<K, V> {
     protected V get(K key) {
         assert cache != null;
         try {
-            return cache.get(key);
+            return cache.getUnchecked(key);
         } catch (Exception e) {
             log.error("get local cache error", e);
         }
@@ -93,7 +93,6 @@ public abstract class AbstractFeatureService<K, V> {
         V feature = getFromCache(key);
         if (feature == null) {
             feature = getFromSource(key);
-            // TODO 可异步
             saveToCache(key, feature);
         }
         return feature;
@@ -106,9 +105,10 @@ public abstract class AbstractFeatureService<K, V> {
     }
 
     private V getFromSource(K key) {
-        String sourceKey = cacheKey(key);
-        String value = tairTemplate.opsForValue().get(sourceKey);
-        return JSONUtils.fromJson(value, typeToken, null);
+        return null;
+//        String sourceKey = cacheKey(key);
+//        String value = tairTemplate.opsForValue().get(sourceKey);
+//        return JSONUtils.fromJson(value, typeToken, null);
     }
 
     private void saveToCache(K key, V value) {
@@ -119,6 +119,7 @@ public abstract class AbstractFeatureService<K, V> {
         long expire = value == null
                 ? emptyDataExpire
                 : defaultExpire;
+
         // TODO 评估过期时间
         redisTemplate.opsForValue().set(cacheKey, cacheValue, expire, TimeUnit.SECONDS);
     }
@@ -167,15 +168,16 @@ public abstract class AbstractFeatureService<K, V> {
     }
 
     private Map<K, V> getFromSource(List<K> keys) {
-        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;
+        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;
     }
 
     private void saveToCache(List<K> keys, Map<K, V> map) {

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

@@ -5,7 +5,6 @@ import com.tzld.piaoquan.recommend.feature.model.UserFeature;
 import com.tzld.piaoquan.recommend.feature.model.common.Result;
 import com.tzld.piaoquan.recommend.feature.model.feature.GetUserFeatureRequest;
 import com.tzld.piaoquan.recommend.feature.model.feature.GetUserFeatureResponse;
-import com.tzld.piaoquan.recommend.feature.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
@@ -19,8 +18,8 @@ public class UserFeatureService extends AbstractFeatureService<String, UserFeatu
 
     public UserFeatureService() {
         super.emptyData = "{}";
-        super.emptyDataExpire = 60;
-        super.defaultExpire = 7200;
+        super.emptyDataExpire = -1;
+        super.defaultExpire = -1;
         super.keyFunc = k -> String.format("user:%s", k);
         super.typeToken = new TypeToken<UserFeature>() {
         };
@@ -49,12 +48,13 @@ public class UserFeatureService extends AbstractFeatureService<String, UserFeatu
         return builder.build();
     }
 
-    public void saveUserFeature(String jsonValue) {
-
-        UserFeature userFeature = JSONUtils.fromJson(jsonValue, new TypeToken<UserFeature>() {
-        }, null);
-        super.save(userFeature.getUid(), userFeature);
-    }
+//    public void saveUserFeature(String jsonValue) {
+//
+//
+//        UserFeature userFeature = JSONUtils.fromJson(jsonValue, new TypeToken<UserFeature>() {
+//        }, null);
+//        super.save(userFeature.getUid(), userFeature);
+//    }
 
 
 }

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

@@ -8,7 +8,6 @@ import com.tzld.piaoquan.recommend.feature.model.feature.GetAllVideoFeatureRespo
 import com.tzld.piaoquan.recommend.feature.model.feature.GetVideoFeatureRequest;
 import com.tzld.piaoquan.recommend.feature.model.feature.GetVideoFeatureResponse;
 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.MapUtils;
 import org.apache.commons.lang.StringUtils;
@@ -26,8 +25,8 @@ public class VideoFeatureService extends AbstractFeatureService<String, VideoFea
 
     public VideoFeatureService() {
         super.emptyData = "{}";
-        super.emptyDataExpire = 60;
-        super.defaultExpire = 7200;
+        super.emptyDataExpire = -1;
+        super.defaultExpire = -1;
         super.keyFunc = k -> String.format("video:%s", k);
         super.typeToken = new TypeToken<VideoFeature>() {
         };
@@ -57,12 +56,12 @@ public class VideoFeatureService extends AbstractFeatureService<String, VideoFea
         return builder.build();
     }
 
-    public void saveVideoFeature(String jsonValue) {
-
-        VideoFeature videoFeature = JSONUtils.fromJson(jsonValue, new TypeToken<VideoFeature>() {
-        }, null);
-        super.save(videoFeature.getVideoId(), videoFeature);
-    }
+//    public void saveVideoFeature(String jsonValue) {
+//
+//        VideoFeature videoFeature = JSONUtils.fromJson(jsonValue, new TypeToken<VideoFeature>() {
+//        }, null);
+//        super.save(videoFeature.getVideoId(), videoFeature);
+//    }
 
     public GetAllVideoFeatureResponse getAllVideoFeature(GetAllVideoFeatureRequest request) {
         // TODO