zoukankan      html  css  js  c++  java
  • springboot页面缓存和url缓存实例

    @Autowired
        RedisService redisService;
        
        @Autowired
        GoodsService goodsService;
        
        @Autowired
        ThymeleafViewResolver thymeleafViewResolver;
        
        @Autowired
        ApplicationContext applicationContext;
        
        /**
         * 商品列表
         * */
        @RequestMapping(value="/to_list", produces="text/html")
        @ResponseBody
        public String list(HttpServletRequest request, HttpServletResponse response, Model model,MiaoshaUser user) {
            model.addAttribute("user", user);
            //取缓存
            String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
            if(!StringUtils.isEmpty(html)) {
                return html;
            }
            List<GoodsVo> goodsList = goodsService.listGoodsVo();
            model.addAttribute("goodsList", goodsList);
    //         return "goods_list";
            SpringWebContext ctx = new SpringWebContext(request,response,
                    request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
            //手动渲染
            html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
            if(!StringUtils.isEmpty(html)) {
                redisService.set(GoodsKey.getGoodsList, "", html);// 渲染成功更新缓存
            }
            return html;
        }
    @RequestMapping(value="/to_detail2/{goodsId}",produces="text/html")
        @ResponseBody
        public String detail2(HttpServletRequest request, HttpServletResponse response, Model model,MiaoshaUser user,
                @PathVariable("goodsId")long goodsId) {
            model.addAttribute("user", user);
            
            //取缓存
            String html = redisService.get(GoodsKey.getGoodsDetail, ""+goodsId, String.class);
            if(!StringUtils.isEmpty(html)) {
                return html;
            }
            //手动渲染
            GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
            model.addAttribute("goods", goods);
            
            long startAt = goods.getStartDate().getTime();
            long endAt = goods.getEndDate().getTime();
            long now = System.currentTimeMillis();
            
            int miaoshaStatus = 0;
            int remainSeconds = 0;
            if(now < startAt ) {//秒杀还没开始,倒计时
                miaoshaStatus = 0;
                remainSeconds = (int)((startAt - now )/1000);
            }else  if(now > endAt){//秒杀已经结束
                miaoshaStatus = 2;
                remainSeconds = -1;
            }else {//秒杀进行中
                miaoshaStatus = 1;
                remainSeconds = 0;
            }
            model.addAttribute("miaoshaStatus", miaoshaStatus);
            model.addAttribute("remainSeconds", remainSeconds);
    //        return "goods_detail";
            
            SpringWebContext ctx = new SpringWebContext(request,response,
                    request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
            html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", ctx);
            if(!StringUtils.isEmpty(html)) {
                redisService.set(GoodsKey.getGoodsDetail, ""+goodsId, html);
            }
            return html;
        }
  • 相关阅读:
    sys:1: RuntimeWarning: coroutine 'Launcher.killChrome' was never awaited
    python 引用对象相等,还是用list.extend()
    python 3.7.4 垃圾,一整天安装那个mitmproxy ,不行,卡在一个破库叫urwid ,说是os.path()为空,换3.8.2安装成功,垃圾3.7.4迟早要完
    python @staticmethod 注解,静态方法,可以省略类里那个self参数
    spring boot 记一次花了两天还是没有解决的奇怪bug(失去所有响应,post不到了,啥反应也没有了)
    Yum常用命令
    Centos安装与配置
    遍历hashmap的6种方法
    Java定时调度
    ElasticSearch的应用
  • 原文地址:https://www.cnblogs.com/shamo89/p/9749576.html
Copyright © 2011-2022 走看看