优化商品类型的方案

一、优化商品类型的方案选择
1.后台管理-redis缓存
2.商城主页-页面静态化
二、为什么要对商品类型进行优化(面试)
三、代码实现
1.搭建公共的服务模块

2.测试 - eureka中有服务
四、Redis公共服务
1.添加Redis的依赖
2.导入Redis的工具类
3.完成Redis的存取值的接口
@RestController
public class RedisController implements RedisClient{

/**
 * 缓存数据
 * @param key
 * @param value
 * @return
 */
@PostMapping("/redis")
public AjaxResult set(@RequestParam("key")String key,@RequestParam("value") String value){
    try {
        RedisUtils.INSTANCE.set(key,value);
        return AjaxResult.me().setSuccess(true).setMessage("保存成功!");
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
    }

}

/**
 * 获取缓存数据
 * @param key
 * @return
 */
@GetMapping("/redis")
public AjaxResult get(@RequestParam("key")String key){
    try {
        String value = RedisUtils.INSTANCE.get(key);
        return AjaxResult.me().setSuccess(true).setMessage("成功").setRestObj(value);
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage("系统异常!"+e.getMessage());
    }
}

}

4.完成Feign的客户端接口
(1)添加openfeign的依赖
(2)写客户端接口和返回托底数据的factory
@FeignClient(value = “COMMON-SERVICE”,fallbackFactory = RedisFallBackFactory.class)
public interface RedisClient {

/**
 * 缓存数据
 * @param key
 * @param value
 * @return
 */
@PostMapping("/redis")
public AjaxResult set(@RequestParam("key") String key, @RequestParam("value")String value);

/**
 * 获取缓存数据
 * @param key
 * @return
 */
@GetMapping("/redis")
public AjaxResult get(@RequestParam("key")String key);

}

@Component
public class RedisFallBackFactory implements FallbackFactory {
@Override
public RedisClient create(Throwable throwable) {
return new RedisClient() {
@Override
public AjaxResult set(String key, String value) {
return AjaxResult.me().setSuccess(false).setMessage(“系统异常”);
}

        @Override
        public AjaxResult get(String key) {
            return AjaxResult.me().setSuccess(false).setMessage("系统异常");
        }
    };
}

}

5.在商品服务中 (查询商品列表的接口中做更改)
(1)先从缓存中查询
(2)如果没有,在查询数据库,同步到缓存中
(3)返回数据
@Override
public List loadTypeTree() {
//从redis中获取数据
AjaxResult result = redisClient.get(“productTypes”);
String productTypesJsonStr = (String) result.getRestObj();
List productTypes = JSON.parseArray(productTypesJsonStr, ProductType.class);
//判断是否有值
if(productTypes==null||productTypes.size()<=0){
//没有则查询数据库,并将数据缓存到redis中
productTypes = loop();
redisClient.set(“productTypes”,JSON.toJSONString(productTypes));
}
//返回数据
return productTypes;
}

6.测试,修改bug
(1)调用公共接口400错误,请求头太长
(2)在公共服务中配置tomcat请求头的大小–自己查
五、商城主页面静态化
1.准备模板

运行项目-搭建一个前端服务器
npm install -g live-server
live-server --port=8081

2.编写公共的页面静态化的接口
(1)添加velocity的依赖
(2)导入工具包
(3)写接口
(4)写feign的客户端
3.写一个接口(本身这个接口需要后台管理页面的一个按钮来调用),我们直接通过postman来调用
/**

  • 生成主页面
  • 先根据product.type.vm生成一个product.type.vm.html
  • 再根据home.vm生成主页面

*/
@Override
public void genHomePage() {

//第一步 : 生成product.type.vm.html
Map<String,Object> map = new HashMap<>();

String templatePath = "E:\\ideaProject\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\template\\product.type.vm";
String targetPath = "E:\\ideaProject\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\template\\product.type.vm.html";
//model 就是List 存放所有的商品类型
List<ProductType> productTypes = loadTypeTree();
map.put("model",productTypes);
map.put("templatePath",templatePath);
map.put("targetPath",targetPath);
staticPageClient.genStaticPage(map);

//第二步 : 生成home.html
map = new HashMap<>();
templatePath = "E:\\ideaProject\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\template\\home.vm";
targetPath = "E:\\ideaProject\\aigou-web-parent\\aigou-web-home\\home.html";
//model 中要有一个数据是staticRoot
Map<String,String> model = new HashMap<>();
model.put("staticRoot","E:\\ideaProject\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\");
map.put("model",model);
map.put("templatePath",templatePath);
map.put("targetPath",targetPath);

staticPageClient.genStaticPage(map);

}

4.测试–静态资源(css,js,image)路径不对

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值