When using Spring Boot + Freemarker to develop web projects, some static files are relatively large, and if they are accessed on a PC, the speed will be much slower when accessing on a mobile phone, especially when using traffic, and it will consume a lot of traffic.
By catching the request, you can find that every time you enter a page, you need to load a static file. If a company that is not short of money can place the static file on a CDN to speed up access, or use Nginx to cache static files.
Today I will introduce to you a different cache optimization method. Through Spring's cache mechanism, static files are cached. To configure static file cache in Spring Boot, you only need to add the following configuration to the configuration file:
# Resource cache time, unit seconds spring.resources.cache-period=604800 # Enable gzip compression spring.resources.chain.gzipped=true # Enable cache spring.resources.chain.cache=false
You can refer to the documentation for configuration: SPRING RESOURCES HANDLING section of https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/
After adding the cache configuration, after we access the page, the loaded static resources will be cached. During the second visit, we will no longer request the download again. By catching the packet, we can see that it has indeed been cached.
In the Size column, there is from memory cache. The resource is cached in the browser's memory, and some files are cached on the disk, that is, from disk cache.
The purpose of optimization is to achieve, but there is a small problem that has not been solved, that is, if my resource file changes, such as the css file is modified, and when my server releases it, there will still be a cache on the user.
The best effect is when the file changes or when the server program restarts, the user's request needs to download the latest resources on the server. When there is no restart, use the cached content, so that the user can see the latest content immediately after the change.
We can use the version number to solve this problem, which is to add a version number after the static resource, and change the version number when the resource changes, so there will be no problem.
How to use it is as follows:
<link rel="stylesheet" href="css/main-app.css?version=${version!}" rel="external nofollow" />The usage is very simple, the key is where does the version value come from?
We can set this value through the code before starting:
System.setProperty("version", version);This value can be passed in through the args of the main method and dynamically passed to the program in the script that starts the project. The startup script can obtain the MD5 value of the program jar as the version number. In this way, when the server program restarts, the version number changes and the cache becomes invalid.
Then get this value in the filter and set it to the request and you can use it in each page.
String version = System.getProperty("version");req.setAttribute("version", version == null ? "1.0.0" : version);In addition to this method of passing parameters, you can also use custom packaged plugins to replace version with specific content when packaging.
The above method is to control file changes by generating version by yourself. In fact, Spring Mvc has provided the version management function of static files. There are two ways. One is to generate the version number through MD5 of the resource. If the file content changes, MD5 will definitely change too. Another way is to prefix the resource with a version number.
MD5
Add the following configuration to the properties file:
spring.resources.chain.strategy.content.enabled=truespring.resources.chain.strategy.content.paths=/**
Processing of adding Url:
@ControllerAdvicepublic class ControllerConfig { @Autowired ResourceUrlProvider resourceUrlProvider; @ModelAttribute("urls") public ResourceUrlProvider urls() { return this.resourceUrlProvider; }}The use of the page is as follows:
The code copy 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:
The code copy is as follows:
<link rel="stylesheet" type="text/css" href="/css/main-app-4v371326bb93ce4b611853a309b69b33.css" rel="external nofollow" >
Version number
Add the following configuration to the properties 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 use of the page is 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 you use, you can achieve the effect and the workload will not be too large. Optimization will be endless and you will be done.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.