美文网首页我爱编程
SpringBoot 缓存&资源优化

SpringBoot 缓存&资源优化

作者: FantJ | 来源:发表于2018-06-11 16:41 被阅读655次

页面缓存

1. freemarker 的页面静态化

application.properties 配置实现浏览器缓存

# SPRING RESOURCES HANDLING ([ResourceProperties](https://github.com/spring-projects/spring-boot/tree/v1.5.4.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java))
spring.resources.add-mappings=true # Enable default resource handling.
spring.resources.cache-period= # Cache period for the resources served by the resource handler, in seconds.
spring.resources.chain.cache=true # Enable caching in the Resource chain.
spring.resources.chain.enabled= # Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
spring.resources.chain.gzipped=false # Enable resolution of already gzipped resources.
spring.resources.chain.html-application-cache=false # Enable HTML5 application cache manifest rewriting.
spring.resources.chain.strategy.content.enabled=false # Enable the content Version Strategy.
spring.resources.chain.strategy.content.paths=/** # Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.fixed.enabled=false # Enable the fixed Version Strategy.
spring.resources.chain.strategy.fixed.paths=/** # Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.fixed.version= # Version string to use for the Version Strategy.
spring.resources.static-locations=classpath:/static/

这段配置是用来启用资源缓存处理。
借鉴官方文档:https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/

controller中实现页面静态化


/**
 * Created by Fant.J.
 */
@RestController
public class HelloController {

    @Autowired
    private Configuration configuration;


    @GetMapping("/hello")
    public String demo(Map<String, Object> map) {
        map.put("name", "demo");
        freeMarkerContent(map,"hello");
        return "hello";
    }

    private void freeMarkerContent(Map<String,Object> root,String ftl){
        try {
            Template temp = configuration.getTemplate(ftl+".ftl");
            //以classpath下面的static目录作为静态页面的存储目录,同时命名生成的静态html文件名称
            String path=this.getClass().getResource("/").getPath()+"templates/"+ftl+".html";
            Writer file = new FileWriter(path);
            temp.process(root, file);
            file.flush();
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. thymeleaf 的页面静态化

核心代码的不同:

        SpringWebContext ctx = new SpringWebContext(request,response,
                request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
        String html = thymeleafViewResolver.getTemplateEngine().process("hello", ctx);

对象缓存

利用Redis实现对象的缓存:

后面我会添加SpringBoot 对 Redis 的集成开发。

静态资源优化

1. JS/CSS压缩

取消空格。

2. 多个JS/CSS组合

打包成一个js/css,减少请求次数

3.CDN访问

js/css打包放到cdn上做提速

相关文章

  • SpringBoot 缓存&资源优化

    页面缓存 1. freemarker 的页面静态化 application.properties 配置实现浏览器缓...

  • 网站优化

    网站优化 前端优化合并js,css,图片等资源,减少http请求次数开启http缓存使用CDN缓存资源和静态页面,...

  • AMP是如何提升性能?

    AMP简介 Google推出移动网页加速项目AMPAMP介绍 AMP是如何提升性能? 以下的优化点是AMP页面能被...

  • HTTP缓存、cookies、访问控制、发展、消息

    3月25日 周一 HTTP缓存 ·加速资源 更好地利用缓存资源,可以提高网站的性能和响应速度。为了优化缓存,过期时...

  • 如何优化UITableView?

    优化UITableView常用的方式有:Cell重用、缓存Cell高度、Cell数据资源缓存、渲染、减少视图数目、...

  • 5分钟看懂系列:HTTP缓存机制详解

    什么是HTTP缓存 HTTP 缓存可以说是HTTP性能优化中简单高效的一种优化方式了,缓存是一种保存资源副本并在下...

  • http缓存和浏览器缓存

    缓存是性能优化中简单高效的一种优化方式,优秀的缓存策略可以缩短网页请求资源的距离、减少延迟、减少宽带(缓存可重复利...

  • 浅谈前端js面试--运行环境(性能优化)

    优化策略 多使用内存、缓存或其他方法 减少CPU计算、减少网络 入手 加载页面和静态资源 页面渲染 加载资源优化 ...

  • H5应用程序缓存 - Cache manifest

    一、作用 离线浏览 - 根据文件规则把资源缓存在本地,脱机依然能够访问资源,联网会直接使用缓存在本地的文件。优化加...

  • springboot的缓存

    ### springboot中的缓存 #### 如何启用缓存: 1. springboot中如果想要启用缓存,...

网友评论

    本文标题:SpringBoot 缓存&资源优化

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