diff --git a/community-service/src/main/java/com/tanpu/community/cache/LocalCache.java b/community-service/src/main/java/com/tanpu/community/cache/LocalCache.java new file mode 100644 index 0000000000000000000000000000000000000000..ca2c283fa46365c71863f3cad430a09f52b31d8a --- /dev/null +++ b/community-service/src/main/java/com/tanpu/community/cache/LocalCache.java @@ -0,0 +1,48 @@ +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; + } +} diff --git a/community-service/src/main/java/com/tanpu/community/config/CacheConfig.java b/community-service/src/main/java/com/tanpu/community/config/CacheConfig.java index a18910602e50ec1fa360d272399aa5250d1e244b..54b2f4a5ce1e3f48c2c5e6eada8fd52b170ccdfa 100644 --- a/community-service/src/main/java/com/tanpu/community/config/CacheConfig.java +++ b/community-service/src/main/java/com/tanpu/community/config/CacheConfig.java @@ -23,10 +23,11 @@ public class CacheConfig { } @Bean - public CacheManager cacheManager() { + public CaffeineCacheManager caffeineCacheManager() { CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCacheNames(Arrays.asList("local")); - cacheManager.setCacheSpecification("maximumSize=10,expireAfterWrite=10s"); + // todo é…置化 + cacheManager.setCacheSpecification("maximumSize=1000,expireAfterWrite=30s"); return cacheManager; } } diff --git a/community-service/src/main/java/com/tanpu/community/controller/FileController.java b/community-service/src/main/java/com/tanpu/community/controller/FileController.java index 7c6325df2ca2cfa134188df0d8c9ef0c71403e4d..01fa071e336622fd58aa3aad95636ad53661d1a0 100644 --- a/community-service/src/main/java/com/tanpu/community/controller/FileController.java +++ b/community-service/src/main/java/com/tanpu/community/controller/FileController.java @@ -5,6 +5,7 @@ import com.tanpu.community.api.beans.resp.FileUploadResp; import com.tanpu.community.manager.FileManager; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -16,6 +17,9 @@ public class FileController { @Autowired private FileManager fileManager; + @Autowired + private CaffeineCacheManager localCache; + @PostMapping("/uploadFile") @ResponseBody public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) { @@ -24,6 +28,11 @@ public class FileController { @GetMapping("/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++) { System.out.println(fileManager.getId("" + i / 2)); } diff --git a/community-service/src/main/java/com/tanpu/community/service/BatchFeignCallService.java b/community-service/src/main/java/com/tanpu/community/service/BatchFeignCallService.java index 85b9118d2582be5b9afa506eb9d67c2513543dc5..4050368f75a1bc64820cf14b75c89bd4c430b1c7 100644 --- a/community-service/src/main/java/com/tanpu/community/service/BatchFeignCallService.java +++ b/community-service/src/main/java/com/tanpu/community/service/BatchFeignCallService.java @@ -27,6 +27,7 @@ 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.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -54,7 +55,6 @@ public class BatchFeignCallService { @Resource private FeignClientForCourse feignForCourse; - @Resource private FeignForFund feignForFund; @@ -67,6 +67,9 @@ public class BatchFeignCallService { @Resource private OSSFileService ossFileService; + @Autowired + private FeignService feignService; + @Resource private TopicService topicService; @@ -189,17 +192,12 @@ public class BatchFeignCallService { } if (!CollectionUtils.isEmpty(shortVideoIds)) { // çŸè§†é¢‘列表 - CommonResp<List<ShortVideoBaseInfoResp>> commonResp = - feignClientForTanpuroom.getShortVideoBaseInfo(setToList(shortVideoIds)); - if (commonResp.isSuccess() && !CollectionUtils.isEmpty(commonResp.getData())) { - shortVideoMap.putAll(commonResp.getData().stream() - .collect(Collectors.toMap(ShortVideoBaseInfoResp::getSourceId, item -> item))); - } + List<ShortVideoBaseInfoResp> list = feignService.batchGetShortVideoBaseInfo(setToList(shortVideoIds)); + shortVideoMap.putAll(list.stream().collect(Collectors.toMap(ShortVideoBaseInfoResp::getSourceId, item -> item))); } if (!CollectionUtils.isEmpty(curriculumIds)) { // 课程列表 - List<ShortVideoBaseInfoResp> commonResp = - feignClientForFatools.getCurriculumByColumnRelId(setToList(curriculumIds)); + List<ShortVideoBaseInfoResp> commonResp = feignService.getCurriculumByColumnRelId(setToList(curriculumIds)); if (commonResp != null && !CollectionUtils.isEmpty(commonResp)) { curriculumMap.putAll(commonResp.stream() .collect(Collectors.toMap(ShortVideoBaseInfoResp::getColumnRelId, item -> item))); diff --git a/community-service/src/main/java/com/tanpu/community/service/FeignService.java b/community-service/src/main/java/com/tanpu/community/service/FeignService.java index e61f7d325b516d20794a397bcc541f9e104ae718..2a34e5a69f5f47621b0d3d31548bffcb6e2c853a 100644 --- a/community-service/src/main/java/com/tanpu/community/service/FeignService.java +++ b/community-service/src/main/java/com/tanpu/community/service/FeignService.java @@ -3,15 +3,55 @@ package com.tanpu.community.service; import com.tanpu.common.api.CommonResp; import com.tanpu.common.exception.BizException; import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp; +import com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp; +import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNew; +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.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.cache.Cache; +import org.springframework.cache.caffeine.CaffeineCacheManager; 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 public class FeignService { - @Autowired - private FeignClientForFatools fatools; + @Resource + 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) { CommonResp<UserInfoResp> userInfoNewCommonResp = fatools.queryUsersListNew(userId); @@ -20,4 +60,44 @@ public class FeignService { } 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; + } }