在 Spring Boot 中按内容类型缓存静态文件
Cache static files by content type in Spring Boot
我正在尝试在 Spring Boot 中针对特定静态文件类型设置缓存 header。
在目录 src/main/resources/static 中,有几个不同文件类型的子目录:
src/main/resources/static/font --> *.otf
src/main/resources/static/lib --> *.js
src/main/resources/static/images --> *.png, *.jpg
有没有办法在 Spring 配置中按文件类型放置缓存 header?
*.otf 365 days
*.png 30 days
*.jpg 7 days
Spring 版本是 5.2.3 和 Spring Boot 2.2.4 - Spring Boot 是否有可能处理它并使其无法工作?
尝试过
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
final CacheControl oneYearPublic =
CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic();
// it does not "work" with "/static/fonts/"
registry
.addResourceHandler("/fonts/{filename:\w+\.otf}")
.setCacheControl(oneYearPublic);
}
但我得到了奇怪的结果。使用 DevTools 的网络选项卡检查时,我得到这些 headers:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
但是当我直接去 URL 时,我得到 404
http://localhost/fonts/1952RHEINMETALL.otf
没有任何配置我得到“no-store”Cache-Controlheader.
我已经找到了解决这个问题的有效方法。检查 GitHub 回购 https://github.com/alexsomai/cache-static-assets.
这是一个应该有效的配置示例:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
Objects.requireNonNull(registry);
// could use either '/**/images/{filename:\w+\.png}' or '/**/images/*.png'
registry.addResourceHandler("/**/images/{filename:\w+\.png}")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS));
registry.addResourceHandler("/**/images/*.jpg")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(2, TimeUnit.DAYS));
registry.addResourceHandler("/**/lib/*.js")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(3, TimeUnit.DAYS));
}
}
您可以根据文件类型和缓存持续时间轻松地根据需要进行调整。
作为关键要点,请确保 添加 addResourceLocations
函数(没有这个函数,您会得到 404)。另外,如果您正在使用 Spring 启动,则您 不需要 @EnableWebMvc
,因为它最初发布在这个示例 中。
我正在尝试在 Spring Boot 中针对特定静态文件类型设置缓存 header。 在目录 src/main/resources/static 中,有几个不同文件类型的子目录:
src/main/resources/static/font --> *.otf
src/main/resources/static/lib --> *.js
src/main/resources/static/images --> *.png, *.jpg
有没有办法在 Spring 配置中按文件类型放置缓存 header?
*.otf 365 days
*.png 30 days
*.jpg 7 days
Spring 版本是 5.2.3 和 Spring Boot 2.2.4 - Spring Boot 是否有可能处理它并使其无法工作?
尝试过
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
final CacheControl oneYearPublic =
CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic();
// it does not "work" with "/static/fonts/"
registry
.addResourceHandler("/fonts/{filename:\w+\.otf}")
.setCacheControl(oneYearPublic);
}
但我得到了奇怪的结果。使用 DevTools 的网络选项卡检查时,我得到这些 headers:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
但是当我直接去 URL 时,我得到 404
http://localhost/fonts/1952RHEINMETALL.otf
没有任何配置我得到“no-store”Cache-Controlheader.
我已经找到了解决这个问题的有效方法。检查 GitHub 回购 https://github.com/alexsomai/cache-static-assets.
这是一个应该有效的配置示例:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
Objects.requireNonNull(registry);
// could use either '/**/images/{filename:\w+\.png}' or '/**/images/*.png'
registry.addResourceHandler("/**/images/{filename:\w+\.png}")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS));
registry.addResourceHandler("/**/images/*.jpg")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(2, TimeUnit.DAYS));
registry.addResourceHandler("/**/lib/*.js")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(3, TimeUnit.DAYS));
}
}
您可以根据文件类型和缓存持续时间轻松地根据需要进行调整。
作为关键要点,请确保 添加 addResourceLocations
函数(没有这个函数,您会得到 404)。另外,如果您正在使用 Spring 启动,则您 不需要 @EnableWebMvc
,因为它最初发布在这个示例