feat(商城): 代码优化

master
wayn 1 year ago
parent 1777b1ccd6
commit 19567df1de

@ -0,0 +1,25 @@
package com.wayn.common.core.domain.shop.vo;
import com.wayn.common.core.domain.shop.Banner;
import com.wayn.common.core.domain.shop.Diamond;
import com.wayn.common.core.domain.shop.Goods;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
/**
* @author: waynaqua
* @date: 2023/11/8 22:36
*/
@Data
public class HomeIndexResponseVO implements Serializable {
@Serial
private static final long serialVersionUID = -14732478530341760L;
private List<Banner> bannerList;
private List<Diamond> categoryList;
private List<Goods> newGoodsList;
private List<Goods> hotGoodsList;
}

@ -29,6 +29,7 @@ public class CacheConfig implements CachingConfigurer {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
connectionFactory.setValidateConnection(true);
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setHashKeySerializer(keySerializer());
@ -38,23 +39,6 @@ public class CacheConfig implements CachingConfigurer {
return redisTemplate;
}
@Bean
public LettuceClientConfigurationBuilderCustomizer lettuceCustomizer() {
return builder -> {
if (SystemUtils.IS_OS_LINUX) {
builder.clientOptions(ClientOptions.builder()
.socketOptions(SocketOptions.builder()
.keepAlive(SocketOptions.KeepAliveOptions.builder()
.enable(true)
.idle(Duration.ofMinutes(3))
.count(3)
.interval(Duration.ofSeconds(10))
.build())
.build())
.build());
}
};
}
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();

@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("home")
public class HomeController extends BaseController {
private IHomeService IHomeService;
private IHomeService iHomeService;
/**
*
@ -25,7 +25,7 @@ public class HomeController extends BaseController {
*/
@GetMapping("index")
public R index() {
return IHomeService.index();
return R.success(iHomeService.index());
}
/**
@ -35,7 +35,7 @@ public class HomeController extends BaseController {
*/
@GetMapping("mallConfig")
public R mallConfig() {
return IHomeService.mallConfig();
return iHomeService.mallConfig();
}
/**
@ -46,7 +46,7 @@ public class HomeController extends BaseController {
@GetMapping("goodsList")
public R getGoodsList() {
Page<Goods> page = getPage();
return IHomeService.listGoodsPage(page);
return iHomeService.listGoodsPage(page);
}
}

@ -2,6 +2,7 @@ package com.wayn.mobile.api.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.vo.HomeIndexResponseVO;
import com.wayn.common.util.R;
public interface IHomeService {
@ -13,7 +14,7 @@ public interface IHomeService {
* @return r
* @see <a href="https://www.cnblogs.com/cjsblog/p/9267163.html">https://www.cnblogs.com/cjsblog/p/9267163.html</a>
*/
R index();
HomeIndexResponseVO index();
/**
*

@ -4,101 +4,66 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.constant.Constants;
import com.wayn.common.core.domain.shop.Banner;
import com.wayn.common.core.domain.shop.Diamond;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.vo.HomeIndexResponseVO;
import com.wayn.common.core.service.shop.IBannerService;
import com.wayn.common.core.service.shop.ICategoryService;
import com.wayn.common.core.service.shop.IDiamondService;
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.apache.commons.collections4.MapUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import static com.wayn.data.redis.constant.CacheConstants.SHOP_HOME_INDEX_HASH;
import static com.wayn.data.redis.constant.CacheConstants.SHOP_HOME_INDEX_HASH_EXPIRATION_FIELD;
@Slf4j
@Service
@CacheConfig(keyGenerator = "cacheKeyGenerator")
@AllArgsConstructor
public class IHomeServiceImpl implements IHomeService {
private IBannerService iBannerService;
private ICategoryService iCategoryService;
private IGoodsService iGoodsService;
private RedisCache redisCache;
private RedisTemplate<String, Object> redisTemplate;
private IDiamondService iDiamondService;
private ThreadPoolTaskExecutor commonThreadPoolTaskExecutor;
@Cacheable(value = "home_index_cache#600", unless = "#result == null")
@Override
public R index() {
R success = R.success();
Map<String, Object> shopHomeIndexHash = redisCache.getCacheMap(SHOP_HOME_INDEX_HASH);
// 当缓存中存在数据,并且过期时间不为空而且小于等于过期时间则直接从缓存中取出数据
long nowTime = System.currentTimeMillis();
if (MapUtils.isNotEmpty(shopHomeIndexHash) && shopHomeIndexHash.containsKey(SHOP_HOME_INDEX_HASH_EXPIRATION_FIELD)) {
long time = (long) shopHomeIndexHash.get(SHOP_HOME_INDEX_HASH_EXPIRATION_FIELD);
if ((nowTime - time) <= Constants.ONE_DAY) {
shopHomeIndexHash.forEach(success::add);
log.info("getHomeIndexDataCompletableFuture:{}", success);
return success;
}
}
public HomeIndexResponseVO index() {
HomeIndexResponseVO responseVO = new HomeIndexResponseVO();
try {
List<CompletableFuture<Void>> list = new ArrayList<>(4);
CompletableFuture<Void> f1 = CompletableFuture.supplyAsync(
() -> iBannerService.list(Wrappers.lambdaQuery(Banner.class).eq(Banner::getStatus, 0).orderByAsc(Banner::getSort)), commonThreadPoolTaskExecutor)
.thenAccept(data -> {
String key = "bannerList";
redisCache.setCacheMapValue(SHOP_HOME_INDEX_HASH, key, data);
log.info("bannerList:{}", data);
success.add(key, data);
});
.thenAccept(responseVO::setBannerList);
CompletableFuture<Void> f2 = CompletableFuture.supplyAsync(
() -> iDiamondService.list(Wrappers.lambdaQuery(Diamond.class).orderByAsc(Diamond::getSort).last("limit 10")), commonThreadPoolTaskExecutor)
.thenAccept(data -> {
String key = "categoryList";
redisCache.setCacheMapValue(SHOP_HOME_INDEX_HASH, key, data);
log.info("categoryList:{}", data);
success.add(key, data);
});
.thenAccept(responseVO::setCategoryList);
CompletableFuture<Void> f3 = CompletableFuture.supplyAsync(
() -> iGoodsService.selectHomeIndexGoods(Goods.builder().isNew(true).build()), commonThreadPoolTaskExecutor)
.thenAccept(data -> {
String key = "newGoodsList";
redisCache.setCacheMapValue(SHOP_HOME_INDEX_HASH, key, data);
log.info("newGoodsList:{}", data);
success.add(key, data);
});
.thenAccept(responseVO::setNewGoodsList);
CompletableFuture<Void> f4 = CompletableFuture.supplyAsync(
() -> iGoodsService.selectHomeIndexGoods(Goods.builder().isHot(true).build()), commonThreadPoolTaskExecutor)
.thenAccept(data -> {
String key = "hotGoodsList";
redisCache.setCacheMapValue(SHOP_HOME_INDEX_HASH, key, data);
log.info("hotGoodsList:{}", data);
success.add(key, data);
});
.thenAccept(responseVO::setHotGoodsList);
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).join();
// 通过hash的field设置过期时间防止过期时间设置失败导致缓存无法删除
redisCache.setCacheMapValue(SHOP_HOME_INDEX_HASH, SHOP_HOME_INDEX_HASH_EXPIRATION_FIELD, nowTime);
return success;
return responseVO;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null ;
}
@Override

Loading…
Cancel
Save