Commit 6b62264c authored by 吴泽佳's avatar 吴泽佳
parents 15b271a6 5fce0cce
package com.tanpu.community.cache;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.function.Supplier;
@Service
public class LocalCache {
@Autowired
private CaffeineCacheManager cacheManager;
private Cache localCache;
@PostConstruct
public void init() {
localCache = cacheManager.getCache("local");
if (localCache == null) {
throw new BizException("local cache not found!");
}
}
public <T> T getObject(String key, Class<T> clz) {
return localCache.get(key, clz);
}
public<T> T getObject(String key, Supplier<T> func, Class<T> clz) {
T value = localCache.get(key, clz);
if (value != null) {
return value;
}
T ret = func.get();
if (ret != null) {
localCache.put(key, ret);
}
return ret;
}
}
...@@ -23,10 +23,11 @@ public class CacheConfig { ...@@ -23,10 +23,11 @@ public class CacheConfig {
} }
@Bean @Bean
public CacheManager cacheManager() { public CaffeineCacheManager caffeineCacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager(); CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCacheNames(Arrays.asList("local")); cacheManager.setCacheNames(Arrays.asList("local"));
cacheManager.setCacheSpecification("maximumSize=10,expireAfterWrite=10s"); // todo 配置化
cacheManager.setCacheSpecification("maximumSize=1000,expireAfterWrite=30s");
return cacheManager; return cacheManager;
} }
} }
...@@ -5,6 +5,7 @@ import com.tanpu.community.api.beans.resp.FileUploadResp; ...@@ -5,6 +5,7 @@ import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.manager.FileManager; import com.tanpu.community.manager.FileManager;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -16,6 +17,9 @@ public class FileController { ...@@ -16,6 +17,9 @@ public class FileController {
@Autowired @Autowired
private FileManager fileManager; private FileManager fileManager;
@Autowired
private CaffeineCacheManager localCache;
@PostMapping("/uploadFile") @PostMapping("/uploadFile")
@ResponseBody @ResponseBody
public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) { public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) {
...@@ -24,6 +28,11 @@ public class FileController { ...@@ -24,6 +28,11 @@ public class FileController {
@GetMapping("/test") @GetMapping("/test")
public String test() { public String test() {
localCache.getCache("local").put("999", "6666666");
System.out.println((String) localCache.getCache("local").get("999").get());
for (int i = 0; i < 30; i++) { for (int i = 0; i < 30; i++) {
System.out.println(fileManager.getId("" + i / 2)); System.out.println(fileManager.getId("" + i / 2));
} }
......
...@@ -27,6 +27,7 @@ import com.tanpu.community.feign.product.FeignForPublicFund; ...@@ -27,6 +27,7 @@ import com.tanpu.community.feign.product.FeignForPublicFund;
import com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom; import com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom;
import com.tanpu.community.feign.zhibo.FeignClientForZhibo; import com.tanpu.community.feign.zhibo.FeignClientForZhibo;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -54,7 +55,6 @@ public class BatchFeignCallService { ...@@ -54,7 +55,6 @@ public class BatchFeignCallService {
@Resource @Resource
private FeignClientForCourse feignForCourse; private FeignClientForCourse feignForCourse;
@Resource @Resource
private FeignForFund feignForFund; private FeignForFund feignForFund;
...@@ -67,6 +67,9 @@ public class BatchFeignCallService { ...@@ -67,6 +67,9 @@ public class BatchFeignCallService {
@Resource @Resource
private OSSFileService ossFileService; private OSSFileService ossFileService;
@Autowired
private FeignService feignService;
@Resource @Resource
private TopicService topicService; private TopicService topicService;
...@@ -189,17 +192,12 @@ public class BatchFeignCallService { ...@@ -189,17 +192,12 @@ public class BatchFeignCallService {
} }
if (!CollectionUtils.isEmpty(shortVideoIds)) { if (!CollectionUtils.isEmpty(shortVideoIds)) {
// 短视频列表 // 短视频列表
CommonResp<List<ShortVideoBaseInfoResp>> commonResp = List<ShortVideoBaseInfoResp> list = feignService.batchGetShortVideoBaseInfo(setToList(shortVideoIds));
feignClientForTanpuroom.getShortVideoBaseInfo(setToList(shortVideoIds)); shortVideoMap.putAll(list.stream().collect(Collectors.toMap(ShortVideoBaseInfoResp::getSourceId, item -> item)));
if (commonResp.isSuccess() && !CollectionUtils.isEmpty(commonResp.getData())) {
shortVideoMap.putAll(commonResp.getData().stream()
.collect(Collectors.toMap(ShortVideoBaseInfoResp::getSourceId, item -> item)));
}
} }
if (!CollectionUtils.isEmpty(curriculumIds)) { if (!CollectionUtils.isEmpty(curriculumIds)) {
// 课程列表 // 课程列表
List<ShortVideoBaseInfoResp> commonResp = List<ShortVideoBaseInfoResp> commonResp = feignService.getCurriculumByColumnRelId(setToList(curriculumIds));
feignClientForFatools.getCurriculumByColumnRelId(setToList(curriculumIds));
if (commonResp != null && !CollectionUtils.isEmpty(commonResp)) { if (commonResp != null && !CollectionUtils.isEmpty(commonResp)) {
curriculumMap.putAll(commonResp.stream() curriculumMap.putAll(commonResp.stream()
.collect(Collectors.toMap(ShortVideoBaseInfoResp::getColumnRelId, item -> item))); .collect(Collectors.toMap(ShortVideoBaseInfoResp::getColumnRelId, item -> item)));
......
...@@ -3,21 +3,100 @@ package com.tanpu.community.service; ...@@ -3,21 +3,100 @@ package com.tanpu.community.service;
import com.tanpu.common.api.CommonResp; import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException; import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp; import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp;
import com.tanpu.community.cache.LocalCache;
import com.tanpu.community.feign.course.FeignClientForCourse;
import com.tanpu.community.feign.fatools.FeignClientForFatools; import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.feign.product.FeignClientForProducts;
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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
@Service @Service
public class FeignService { public class FeignService {
@Autowired @Resource
private FeignClientForFatools fatools; private FeignClientForTanpuroom feignClientForTanpuroom;
@Resource
private FeignClientForZhibo feignClientForZhibo;
@Resource
private FeignClientForProducts feignForProduct;
@Resource
private FeignClientForCourse feignForCourse;
@Resource
private FeignForFund feignForFund;
@Resource
private FeignForPublicFund feignForPublicFund;
@Resource
private FeignClientForFatools feignClientForFatools;
@Resource
private LocalCache localCache;
public UserInfoResp getUserInfoById(String userId) { public UserInfoResp getUserInfoById(String userId) {
CommonResp<UserInfoResp> userInfoNewCommonResp = fatools.queryUsersListNew(userId); CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(userId);
if (userInfoNewCommonResp.isNotSuccess()) { if (userInfoNewCommonResp.isNotSuccess()) {
throw new BizException("内部接口调用失败"); throw new BizException("内部接口调用失败");
} }
return userInfoNewCommonResp.getData(); return userInfoNewCommonResp.getData();
} }
public List<ShortVideoBaseInfoResp> batchGetShortVideoBaseInfo(List<String> sourceIds) {
return batchExecute("batchGetShortVideoBaseInfo_", sourceIds, ShortVideoBaseInfoResp.class, ids -> {
CommonResp<List<ShortVideoBaseInfoResp>> resp = feignClientForTanpuroom.getShortVideoBaseInfo(ids);
if (resp.isSuccess()) {
return resp.getData();
} else {
return new ArrayList<>();
}
});
}
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) {
List<T> retList = new ArrayList<>();
List<String> feignIds = new ArrayList<>();
for (String key : keys) {
String cacheKey = keyPrefix + key;
T ret = localCache.getObject(cacheKey, clz);
if (ret != null) {
retList.add(ret);
} else {
feignIds.add(key);
}
if (!feignIds.isEmpty()) {
List<T> remoteList = func.apply(feignIds);
if (remoteList != null) {
retList.addAll(remoteList);
}
}
}
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