spring.resources.cache.cachecontrol.max-age=3600 spring.resources.cache.cachecontrol.no-cache=false标红原因
时间: 2025-05-08 08:31:04 浏览: 9
### Spring Boot 资源缓存配置详解
#### 配置文件中的资源缓存设置
在 `application.properties` 或者 `application.yml` 文件中可以定义静态资源的缓存行为。对于 `spring.resources.cache` 的配置,主要涉及两个重要参数:`max-age` 和 `no-cache`。
#### 参数解释
- **Max-Age**
此选项用于指定浏览器应该缓存响应的最大时间长度(以秒为单位)。当设置了这个值之后,在这段时间内客户端会直接使用本地缓存而不会再次请求服务器验证资源是否有更新[^1]。
```properties
# application.properties 示例
spring.resources.chain.cache=true
spring.resources.chain.strategy.content.enabled=true
spring.resources.cache.period=3600 # 设置最大缓存时间为一小时
```
```yaml
# application.yml 示例
spring:
resources:
chain:
cache: true
strategy:
content:
enabled: true
cache:
period: 3600 # 设置最大缓存时间为一小时
```
- **No-Cache**
该标志指示浏览器和中间代理不应对任何部分进行缓存处理;每次访问都应向原始服务器发起新的HTTP GET 请求来获取最新的版本。这通常适用于那些频繁变化的内容页面或是登录状态相关的URL路径下加载的数据[^2]。
```properties
# application.properties 中禁用缓存的例子
spring.mvc.static-path-pattern=/static/**
spring.resources.cache.use-last-modified=false
spring.resources.cache.cachecontrol.no-cache=true
```
```yaml
# application.yml 中禁用缓存的例子
spring:
mvc:
static-path-pattern: /static/**
resources:
cache:
use-last-modified: false
cachecontrol:
no-cache: true
```
通过上述两种方式可以在不同场景下灵活控制Web应用中静态资源如何被客户端所缓存,从而达到优化性能以及确保数据最新性的目的。
阅读全文
相关推荐

















