Spring Boot Web static File Cache processing method, springboot

Source: Internet
Author: User

Spring Boot Web static File Cache processing method, springboot

When using Spring Boot + Freemarker to develop Web projects, some static files are relatively large. If the access to the project on a PC is not affected, in particular, traffic access speed will be much slower and will consume a lot of traffic.

By capturing requests, you can find that each time you enter a page, you need to load static files. If there is no bad money, the company can put static files on the CDN to speed up access, or use Nginx to cache static files.

Today, we will introduce other Cache Optimization Methods to cache static files through Spring's cache mechanism, to configure static file caching in Spring Boot, you only need to add the following configuration to the configuration file:

# Resource cache time, in seconds spring. resources. cache-period = 604800 # enable gzipcompressed spring.resources.chain.gzip ped = true # enable cache spring. resources. chain. cache = false

Configuration Reference document: spring resources handling for https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/

After the cache configuration is added, after we access the page, the loaded static resources will be cached, and the second access will not re-request the download, we can see from the packet capture that it is indeed cached.

In the Size column, there is a from memory cache, the resources are cached in the browser's memory, and some files are cached in the disk, that is, from disk cache.

The goal of optimization is achieved, but there is a small problem that cannot be solved, that is, if my resource file changes, for example, the css file has been modified, after my server release, the user will still have a cache.

When the file changes in the best effect, or when the server program restarts, the user's request needs to download the latest resources of the server, and the cached content is used when the server is not restarted, this ensures that the user can view the latest content immediately after the change.

We can use the version number to solve this problem, that is, add a version number after the static resource, and change the version number when the resource changes, so that there will be no problem.

The usage is as follows:

<link rel="stylesheet" href="css/main-app.css?version=${version!}" rel="external nofollow" />

The usage is very simple. Where does the value of version come from?

We can set this value through code before starting:

System.setProperty("version", version);

This value can be passed in through the args of the main method and dynamically transmitted to the program in the script of the startup Item. the startup script can obtain the MD5 value of the program jar as the version number, in this way, when the program on the server is restarted, the version number changes and the cache becomes invalid.

Then you can get this value in the filter and set it to request to use it in each page.

String version = System.getProperty("version");req.setAttribute("version", version == null ? "1.0.0" : version);

In addition to the parameter passing method, you can also use custom packaging plug-ins to replace the version with the specific content during packaging.

The method described above is to generate a version to control file changes. In fact, Spring Mvc already provides the version management function for static files. There are two methods, one is to generate the version number through the resource's MD5, the file content has changed, and the MD5 has certainly changed. The other is to add the path of the version number before the resource.

MD5

Add the following configuration in the property file:

spring.resources.chain.strategy.content.enabled=truespring.resources.chain.strategy.content.paths=/**

Add Url Processing:

@ControllerAdvicepublic class ControllerConfig {  @Autowired  ResourceUrlProvider resourceUrlProvider;  @ModelAttribute("urls")  public ResourceUrlProvider urls() {    return this.resourceUrlProvider;  }}

The page is used as follows:

Copy codeThe Code is as follows:
<Link rel = "stylesheet" type = "text/css" href = "$ {urls. getForLookupPath ('/css/main-app.css')}" rel = "external nofollow">

After compilation, it will become the following content:

Copy codeThe Code is as follows:
<Link rel = "stylesheet" type = "text/css" href = "/css/main-app-4v371326bb93ce4b611853a309b69b33.css" rel = "external nofollow">

Version Number

Add the following configuration in the property file:

spring.resources.chain.strategy.fixed.enabled=truespring.resources.chain.strategy.fixed.paths=/js/**,/v1.0.0/**spring.resources.chain.strategy.fixed.version=v1.0.0

The page is used as follows:

<script type="text/javascript" src="${urls.getForLookupPath('/js/main.js')}"></script>

After compilation, it will become the following content:

<script type="text/javascript" src="/v1.0.0/js/main.js"></script>

No matter which method is used, it can achieve the effect, and the workload will not be too large. There is no end to optimization, and the work will be done.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.