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 {
}
@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;
}
}
......@@ -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));
}
......
......@@ -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)));
......
......@@ -3,21 +3,100 @@ 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.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);
CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(userId);
if (userInfoNewCommonResp.isNotSuccess()) {
throw new BizException("内部接口调用失败");
}
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