页面缓存,url缓存,对象缓存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c_royi/article/details/83119942

页面缓存,url缓存,

配置文件需要配置项

applicayion.property

#static
spring.resources.add-mappings=true
spring.resources.cache-period= 3600
spring.resources.chain.cache=true 
spring.resources.chain.enabled=true
spring.resources.chain.gzipped=true
spring.resources.chain.html-application-cache=true
spring.resources.static-locations=classpath:/static/

1.页面缓存

1.1将静态化页面缓存在浏览器,到页面过期后再访问后端加载

  1. 将页面静态化
  2. 修改后端获取接口
  3. 修改application.properties

1.2使用配置

controller层返回html文件的方法

	@Autowired
	ApplicationContext applicationContext;
	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;
//  produces配置返回类型
    @RequestMapping(value="/to_list", produces="text/html")
    @ResponseBody
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
    
    	SpringWebContext ctx = new SpringWebContext(request,response,
    			request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
    	//手动渲染
    	String html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
    	if(!StringUtils.isEmpty(html)) {
    		String key = "html";
    		//将html缓存在redis中
    		redisService.set(key, html);
    	}
    	return html;
    }

url缓存

将带参数的静态化页面缓存返回给浏览器。

	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;
	
	@Autowired
	ApplicationContext applicationContext;

    @RequestMapping(value="/to_detail2/{goodsId}",produces="text/html")
    @ResponseBody
    public String detail2(HttpServletRequest request, HttpServletResponse response, Model model,
    		@PathVariable("goodsId")long goodsId) {
    	//取缓存
    	String key = "html"+goodsId;
    	String html = redisService.get(key, String.class);
    	if(!StringUtils.isEmpty(html)) {
    		return html;
    	}
    	//使用goodsId获取页面所需要的参数
    	GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
    	model.addAttribute("goods", goods);
    	
    	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(key, html);
    	}
    	return html;
    }

对象缓存

url:对象缓存url

猜你喜欢

转载自blog.csdn.net/c_royi/article/details/83119942
今日推荐