refactor(商城): 支付宝支付优化

master
wayn 3 years ago
parent bb264c2f8e
commit bf2af7577d

@ -90,6 +90,17 @@ public class RedisCache {
return operation.get(key);
}
/**
* key
*
* @param keys key
* @return keyvalue
*/
public <T> List<T> mGetCacheObject(Collection<String> keys) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.multiGet(keys);
}
/**
*
*

@ -6,16 +6,6 @@ import com.wayn.common.util.R;
public interface IHomeService {
/**
* bannerListcategory ListnewGoodsListhotGoodsList <br>
* Future便 <br>
*
*
* @return r
* @see <a href="https://www.cnblogs.com/cjsblog/p/9267163.html">https://www.cnblogs.com/cjsblog/p/9267163.html</a>
*/
R getHomeIndexData();
/**
* bannerListcategory ListnewGoodsListhotGoodsList <br>
* CompletableFuture

@ -13,115 +13,76 @@ import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import com.wayn.data.redis.manager.RedisCache;
import com.wayn.mobile.api.service.IHomeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@AllArgsConstructor
public class IHomeServiceImpl implements IHomeService {
private static final String INDEX_DATA = "shop_index_data";
private static final String SHOP_INDEX_BANNER_LIST = "shop_index_banner_list";
private static final String SHOP_INDEX_CATEGORY_LIST = "shop_index_category_list";
private static final String SHOP_INDEX_GOODS_NEW_LIST = "shop_index_goods_new_list";
private static final String SHOP_INDEX_GOODS_HOT_LIST = "shop_index_goods_hot_list";
@Autowired
private IBannerService iBannerService;
@Autowired
private ICategoryService iCategoryService;
@Autowired
private IGoodsService iGoodsService;
@Autowired
private RedisCache redisCache;
@Autowired
private IDiamondService iDiamondService;
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
public R getHomeIndexData() {
if (redisCache.existsKey(INDEX_DATA)) {
return redisCache.getCacheObject(INDEX_DATA);
}
R success = R.success();
Callable<List<Banner>> bannerCall = () -> iBannerService.list(new QueryWrapper<Banner>().eq("status", 0).orderByAsc("sort"));
Callable<List<Diamond>> diamondCall = () -> iDiamondService.list(new QueryWrapper<Diamond>()
.orderByAsc("sort")
.last("limit 10"));
Callable<List<Goods>> newGoodsCall = () -> iGoodsService.list(new QueryWrapper<Goods>()
.eq("is_new", true)
.eq("is_on_sale", true)
.orderByAsc("create_time")
.last("limit 6"));
Callable<List<Goods>> hotGoodsCall = () -> iGoodsService.list(new QueryWrapper<Goods>()
.eq("is_hot", true)
.eq("is_on_sale", true)
.orderByAsc("create_time")
.last("limit 6"));
FutureTask<List<Banner>> bannerTask = new FutureTask<>(bannerCall);
FutureTask<List<Diamond>> diamondTask = new FutureTask<>(diamondCall);
FutureTask<List<Goods>> newGoodsTask = new FutureTask<>(newGoodsCall);
FutureTask<List<Goods>> hotGoodsTask = new FutureTask<>(hotGoodsCall);
threadPoolTaskExecutor.submit(bannerTask);
threadPoolTaskExecutor.submit(diamondTask);
threadPoolTaskExecutor.submit(newGoodsTask);
threadPoolTaskExecutor.submit(hotGoodsTask);
try {
success.add("bannerList", bannerTask.get());
success.add("categoryList", diamondTask.get());
success.add("newGoodsList", newGoodsTask.get());
success.add("hotGoodsList", hotGoodsTask.get());
redisCache.setCacheObject(INDEX_DATA, success, 10, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException e) {
log.error(e.getMessage(), e);
} finally {
threadPoolTaskExecutor.shutdown();
}
return success;
}
@Override
public R getHomeIndexDataCompletableFuture() {
if (redisCache.existsKey(INDEX_DATA)) {
return redisCache.getCacheObject(INDEX_DATA);
}
R success = R.success();
List<CompletableFuture<Void>> list = new ArrayList<>();
List<CompletableFuture<Void>> list = new ArrayList<>(4);
CompletableFuture<Void> f1 = CompletableFuture.supplyAsync(() -> iBannerService.list(new QueryWrapper<Banner>()
.eq("status", 0)
.orderByAsc("sort")))
.thenAccept(data -> success.add("bannerList", data));
.eq("status", 0)
.orderByAsc("sort")), threadPoolTaskExecutor)
.thenAccept(data -> {
redisCache.setCacheObject(SHOP_INDEX_BANNER_LIST, data, 3600, TimeUnit.MINUTES);
success.add("bannerList", data);
});
CompletableFuture<Void> f2 = CompletableFuture.supplyAsync(() -> iDiamondService.list(new QueryWrapper<Diamond>()
.orderByAsc("sort")
.last("limit 10")))
.thenAccept(data -> success.add("categoryList", data));
.orderByAsc("sort")
.last("limit 10")), threadPoolTaskExecutor)
.thenAccept(data -> {
redisCache.setCacheObject(SHOP_INDEX_CATEGORY_LIST, data, 3600, TimeUnit.MINUTES);
success.add("categoryList", data);
});
CompletableFuture<Void> f3 = CompletableFuture.supplyAsync(() -> iGoodsService.list(new QueryWrapper<Goods>()
.eq("is_new", true)
.eq("is_on_sale", true)
.orderByAsc("create_time")
.last("limit 6")))
.thenAccept(data -> success.add("newGoodsList", data));
.eq("is_new", true)
.eq("is_on_sale", true)
.orderByAsc("create_time")
.last("limit 6")), threadPoolTaskExecutor)
.thenAccept(data -> {
redisCache.setCacheObject(SHOP_INDEX_GOODS_NEW_LIST, data, 3600, TimeUnit.MINUTES);
success.add("newGoodsList", data);
});
CompletableFuture<Void> f4 = CompletableFuture.supplyAsync(() -> iGoodsService.list(new QueryWrapper<Goods>()
.eq("is_hot", true)
.eq("is_on_sale", true)
.orderByAsc("create_time")
.last("limit 6")))
.thenAccept(data -> success.add("hotGoodsList", data));
.eq("is_hot", true)
.eq("is_on_sale", true)
.orderByAsc("create_time")
.last("limit 6")), threadPoolTaskExecutor)
.thenAccept(data -> {
redisCache.setCacheObject(SHOP_INDEX_GOODS_HOT_LIST, data, 3600, TimeUnit.MINUTES);
success.add("hotGoodsList", data);
});
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).join();
redisCache.setCacheObject(INDEX_DATA, success, 3600, TimeUnit.MINUTES);
return success;
}

@ -16,13 +16,13 @@ import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class ThreadPoolConfig {
// 核心线程池大小
private int corePoolSize = 200;
private int corePoolSize = 10;
// 最大可创建的线程数
private int maxPoolSize = 400;
private int maxPoolSize = 100;
// 队列最大长度
private int queueCapacity = 1000;
private int queueCapacity = 200;
// 线程池维护线程所允许的空闲时间
private int keepAliveSeconds = 300;
@ -34,6 +34,7 @@ public class ThreadPoolConfig {
executor.setCorePoolSize(corePoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveSeconds);
executor.setThreadNamePrefix("homeIndexThread-");
// 线程池对拒绝任务(无线程可用)的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;

Loading…
Cancel
Save