美文网首页
2018-12-20 页面优化技术-页面缓存、URL缓存

2018-12-20 页面优化技术-页面缓存、URL缓存

作者: 培根好吃 | 来源:发表于2018-12-20 21:58 被阅读0次

1.页面缓存+URL缓存

页面缓存 与URL缓存 区别: URL缓存是指当进行redis缓存时,在页面缓存基础上加入了路径上的参数(goodsId)

package com.ryan.miaosha.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import com.alibaba.druid.util.StringUtils;
import com.ryan.miaosha.dao.MiaoshaUser;
import com.ryan.miaosha.domain.Goods;
import com.ryan.miaosha.redis.BasePrefix;
import com.ryan.miaosha.redis.GoodsKey;
import com.ryan.miaosha.redis.RedisService;
import com.ryan.miaosha.service.GoodsService;
import com.ryan.miaosha.service.MiaoshaService;
import com.ryan.miaosha.vo.GoodsVo;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    RedisService redisService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;
    
    @RequestMapping(value="/to_list",produces="text/html")
    @ResponseBody
    String list(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user) {        
        model.addAttribute("user", user);
        
        //判断redis缓存中是否存在商品列表html页面
        String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
        if(!StringUtils.isEmpty(html)) {
            return html;
        }
        List<GoodsVo> goodsList=goodsService.listGoodsVo();
        model.addAttribute("goodsList", goodsList);
        //不存在html页面,那么我们自己手动渲染
        WebContext wc=new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goods_list", wc);
        //判断手动渲染的html有没有,有则放入redis中,用于下一次使用
        if(!StringUtils.isEmpty(html)) {
            redisService.set(GoodsKey.getGoodsList, "", html);
        }
        return html;
        /*
         * GoodsKey  它的过期时间是60s,不能让缓存时间太长,不然用户一直看到的是同一个画面;1分钟刚刚好
         * 
         * public class GoodsKey extends BasePrefix{
            private GoodsKey(int expiredSeconds,String prefix) {
                super(expiredSeconds,prefix);
            }
            public static GoodsKey getGoodsList=new GoodsKey(60,"gl");
        }*/
    }
    
    @RequestMapping(value="/to_detail/{goodsId}",produces="text/html")
    @ResponseBody
    String detail(HttpServletRequest request,HttpServletResponse response,Model model,
            MiaoshaUser user,@PathVariable("goodsId") long goodsId) {           
        model.addAttribute("user", user);
        
        //判断redis缓存中是否存在商品列表html页面
        //区别在于加了goodsId
        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 startDate = goods.getStartDate().getTime();
        long endDate = goods.getEndDate().getTime();
        long currentTimeMillis = System.currentTimeMillis();
        
        int miaoshaStatus=0;
        int remainSeconds=0;
        if(currentTimeMillis<startDate) {//秒杀还没开始 进入倒计时
            miaoshaStatus=0;
            remainSeconds=(int) ((startDate-currentTimeMillis)/1000);
        } else if(currentTimeMillis>endDate) {//秒杀已经结束
            miaoshaStatus=2;
            remainSeconds=-1;
        }else {//秒杀正在进行中
            miaoshaStatus=1;
            remainSeconds=0;
        }
        model.addAttribute("miaoshaStatus", miaoshaStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        
        //不存在html页面,那么我们自己手动渲染
        WebContext wc=new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goods_detail", wc);
        //判断手动渲染的html有没有,有则放入redis中,用于下一次使用
        if(!StringUtils.isEmpty(html)) {
            //区别在于加了goodsId
            redisService.set(GoodsKey.getGoodsDetail, ""+goodsId, html);
        }
        return html;
    }
}

相关文章

  • 2018-12-20 页面优化技术-页面缓存、URL缓存

    1.页面缓存+URL缓存 页面缓存 与URL缓存 区别: URL缓存是指当进行redis缓存时,在页面缓存基...

  • 页面优化技术

    1. 页面缓存+URL缓存+对象缓存 1.1页面缓存 例如缓存商品列表页面,先从redis缓存里面拿取页面,如果没...

  • 高并发解决方案

    1、页面缓存 2、URL缓存 3、对象缓存 4、页面静态化 5、接口优化 redis预减库存减少数据库访问系统初始...

  • 秒杀:页面优化

    缓存 页面缓存 页面缓存的有效期比较短,因为页面具有即时性 取缓存 手动渲染模版 结果输出 URL缓存 与页面缓存...

  • 2018-01-17

    1,一个页面从输入 URL 到页面加载完的过程中都发生了什么事情? url 查看缓存 若有缓存 直接拿缓存里面...

  • php-面试第三篇

    42、PHP缓存技术有哪些?1)、全页面静态化缓存2)、页面部分缓存3)、数据缓存4)、查询缓存5)、按内容变更进...

  • rails缓存技术

    三种缓存技术:页面,动作和片段。 Rails 默认支持片段缓存。如果想使用页面缓存和动作缓存,要在 Gemfile...

  • 文件缓存(模板缓存)

    文件缓存(模板缓存) 从页面片段缓存到 facebook 的 BigPipe 技术 将页面划分成一个个小块 利用 ...

  • 通过vite-plugin-pwa配置了解pwa

    前提:多页面vite项目给native提供h5页面,设置离线缓存优化体验 实现service worker离线缓存...

  • vue日记:动态keep-alive问题

    需要实现的效果: A页面进入B页面,缓存A页面组件 A页面进入其他页面,不缓存A页...

网友评论

      本文标题:2018-12-20 页面优化技术-页面缓存、URL缓存

      本文链接:https://www.haomeiwen.com/subject/iwnukqtx.html