|
@@ -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) {
|