Commit 30e71043 authored by 张辰's avatar 张辰

add log for local cache in feignservice

parent 5fce0cce
......@@ -32,6 +32,10 @@ public class LocalCache {
return localCache.get(key, clz);
}
public <T> void putObject(String key, T value) {
localCache.put(key, value);
}
public<T> T getObject(String key, Supplier<T> func, Class<T> clz) {
T value = localCache.get(key, clz);
......
......@@ -197,7 +197,8 @@ public class BatchFeignCallService {
}
if (!CollectionUtils.isEmpty(curriculumIds)) {
// 课程列表
List<ShortVideoBaseInfoResp> commonResp = feignService.getCurriculumByColumnRelId(setToList(curriculumIds));
List<ShortVideoBaseInfoResp> commonResp =
feignClientForFatools.getCurriculumByColumnRelId(setToList(curriculumIds));
if (commonResp != null && !CollectionUtils.isEmpty(commonResp)) {
curriculumMap.putAll(commonResp.stream()
.collect(Collectors.toMap(ShortVideoBaseInfoResp::getColumnRelId, item -> item)));
......
package com.tanpu.community.service;
import com.alibaba.fastjson.JSON;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
......@@ -12,6 +13,8 @@ import com.tanpu.community.feign.product.FeignForFund;
import com.tanpu.community.feign.product.FeignForPublicFund;
import com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom;
import com.tanpu.community.feign.zhibo.FeignClientForZhibo;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
......@@ -23,6 +26,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
@Slf4j
@Service
public class FeignService {
......@@ -61,7 +65,8 @@ public class FeignService {
}
public List<ShortVideoBaseInfoResp> batchGetShortVideoBaseInfo(List<String> sourceIds) {
return batchExecute("batchGetShortVideoBaseInfo_", sourceIds, ShortVideoBaseInfoResp.class, ids -> {
return batchExecute("batchGetShortVideoBaseInfo_", sourceIds, ShortVideoBaseInfoResp.class,
ShortVideoBaseInfoResp::getSourceId, ids -> {
CommonResp<List<ShortVideoBaseInfoResp>> resp = feignClientForTanpuroom.getShortVideoBaseInfo(ids);
if (resp.isSuccess()) {
return resp.getData();
......@@ -71,13 +76,11 @@ public class FeignService {
});
}
public List<ShortVideoBaseInfoResp> getCurriculumByColumnRelId(List<String> curriculumIds) {
return batchExecute("getCurriculumByColumnRelId_", curriculumIds, ShortVideoBaseInfoResp.class, ids -> {
return feignClientForFatools.getCurriculumByColumnRelId(ids);
});
}
private <T> List<T> batchExecute(String keyPrefix, List<String> keys, Class<T> clz, Function<List<String>, List<T>> func) {
private <T> List<T> batchExecute(String keyPrefix,
List<String> keys,
Class<T> clz,
Function<T, String> getKeyFunc,
Function<List<String>, List<T>> feignFunc) {
List<T> retList = new ArrayList<>();
List<String> feignIds = new ArrayList<>();
for (String key : keys) {
......@@ -91,12 +94,16 @@ public class FeignService {
}
if (!feignIds.isEmpty()) {
List<T> remoteList = func.apply(feignIds);
List<T> remoteList = feignFunc.apply(feignIds);
if (remoteList != null) {
retList.addAll(remoteList);
retList.forEach(r -> {
localCache.putObject(keyPrefix + getKeyFunc.apply(r), r);
});
}
}
}
log.info("batchExecute {} : {} -> {}", keyPrefix, JSON.toJSONString(keys), JSON.toJSONString(retList));
return retList;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment