简体   繁体   中英

Spring Boot cache with ContentVersionStrategy

I'm using Spring Boot in a web project, and I wanted to use the static content cache feature.

Using the ContentVersionStrategy with the following configuration in application.properties

spring.resources.chain.enabled=true
spring.resources.chain.cache=true    
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

And the resources like this in a Thymeleaf html

<script src="@{/js/app.js}"></script>

I get the MD5 hash appened to the filename, but if I make a change to that file, the content changes but the hash is still the same.

The only way to make hash "reload" on every request if the file content changes is setting:

spring.resources.chain.cache=false

But I think that miss the point of using this Spring feature. Is this a normal behaviour?

Spring docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

This is normal behaviour.

In a production context, you want the lookup cached. But during development, you probably don't (if you're iteratively editing the JavaScript file).

Setting spring.resources.chain.cache=true will create a chain of ResourceResolver implementations with a CachingResourceResolver that will delegate to the chain of ResourceResolvers, and cache the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM