feat(商城): 代码优化

1. 优化首页接口响应速度
2. 优化商品详情接口响应速度
3. 优化分类接口响应速度
4. 优化注释
master
wayn 3 years ago
parent 388ba41f32
commit 0c43e2956b

@ -1,36 +0,0 @@
package com.wayn.common.util;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
*
*/
public class AsyncExecutorUtil {
private static Timer timer;
static {
timer = new Timer("timer-executor-thread");
}
public static void executor(Runnable r) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
executor.execute(r);
executor.shutdown();
}
public static void scheduled(TimerTask task, Date date) {
timer.scheduleAtFixedRate(task, date, 10000);
}
public static void timerDestroyed() {
timer.cancel();
}
}

@ -9,8 +9,8 @@ import java.util.concurrent.*;
* 线.
*
*/
public class Threads {
private static final Logger logger = LoggerFactory.getLogger(Threads.class);
public class ThreadUtil {
private static final Logger logger = LoggerFactory.getLogger(ThreadUtil.class);
/**
* sleep,

@ -11,11 +11,11 @@ import com.wayn.common.core.domain.shop.Keyword;
import com.wayn.common.core.domain.vo.SearchVO;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.core.service.shop.IKeywordService;
import com.wayn.common.util.AsyncExecutorUtil;
import com.wayn.common.util.R;
import com.wayn.data.elastic.manager.ElasticDocument;
import com.wayn.mobile.api.domain.SearchHistory;
import com.wayn.mobile.api.service.ISearchHistoryService;
import com.wayn.mobile.framework.manager.thread.AsyncManager;
import com.wayn.mobile.framework.security.util.MobileSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -35,10 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@ -135,9 +132,12 @@ public class SearchController extends BaseController {
returnGoodsList.add(goodsMap.get(goodsId));
}
if (CollectionUtils.isNotEmpty(goodsList)) {
AsyncExecutorUtil.executor(() -> {
searchHistory.setHasGoods(true);
iSearchHistoryService.save(searchHistory);
AsyncManager.me().execute(new TimerTask() {
@Override
public void run() {
searchHistory.setHasGoods(true);
iSearchHistoryService.save(searchHistory);
}
});
}
return R.success().add("goods", returnGoodsList);

@ -1,6 +1,7 @@
package com.wayn.mobile.framework.config;
import com.wayn.common.util.Threads;
import com.wayn.common.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -13,6 +14,7 @@ import java.util.concurrent.ThreadPoolExecutor;
/**
* 线使
**/
@Slf4j
@Configuration
public class ThreadPoolConfig {
// 核心线程池大小
@ -62,7 +64,7 @@ public class ThreadPoolConfig {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
Threads.printException(r, t);
ThreadUtil.printException(r, t);
}
};
}

@ -1,7 +1,7 @@
package com.wayn.mobile.framework.manager.thread;
import com.wayn.common.util.Threads;
import com.wayn.common.util.ThreadUtil;
import com.wayn.common.util.spring.SpringContextUtil;
import java.util.TimerTask;
@ -57,6 +57,6 @@ public class AsyncManager {
* 线
*/
public void shutdown() {
Threads.shutdownAndAwaitTermination(executor);
ThreadUtil.shutdownAndAwaitTermination(executor);
}
}

@ -1,7 +1,7 @@
package com.wayn.mobile.framework.manager.thread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wayn.common.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
@ -11,9 +11,9 @@ import javax.annotation.PreDestroy;
*
* @author ruoyi
*/
@Slf4j
@Component
public class ShutdownManager {
private static final Logger logger = LoggerFactory.getLogger("sys-user");
@PreDestroy
public void destroy() {
@ -25,10 +25,10 @@ public class ShutdownManager {
*/
private void shutdownAsyncManager() {
try {
logger.info("====关闭后台任务任务线程池====");
log.info(Constants.LOG_PREFIX + "AsyncManager shutdown" + Constants.LOG_PREFIX);
AsyncManager.me().shutdown();
} catch (Exception e) {
logger.error(e.getMessage(), e);
log.error(e.getMessage(), e);
}
}
}

@ -4,9 +4,9 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wayn.common.core.domain.shop.Member;
import com.wayn.common.core.service.shop.IMemberService;
import com.wayn.common.enums.UserStatusEnum;
import com.wayn.common.util.AsyncExecutorUtil;
import com.wayn.common.util.ip.IpUtils;
import com.wayn.mobile.framework.config.ThreadPoolConfig;
import com.wayn.mobile.framework.manager.thread.AsyncManager;
import com.wayn.mobile.framework.security.LoginUserDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.TimerTask;
@Slf4j
@Service
@ -41,13 +42,16 @@ public class UserDetailsServiceImpl implements UserDetailsService {
throw new DisabledException("登录用户:" + mobile + " 不存在");
}
// 记录最后一次登陆时间以及登陆IP
AsyncExecutorUtil.executor(() ->
AsyncManager.me().execute(new TimerTask() {
@Override
public void run() {
iMemberService.update()
.set("last_login_time", LocalDateTime.now())
.set("last_login_ip", IpUtils.getHostIp())
.eq("id", member.getId())
.update()
);
.update();
}
});
return new LoginUserDetail(member, Collections.emptySet());
}

Loading…
Cancel
Save