Spring Boot Sping Boot Cache CSS和JS

lpwwtiir  于 2023-05-22  发布在  Spring
关注(0)|答案(1)|浏览(135)

我用的是春 Boot 3号和百里香叶
我想缓存css,js和img

@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/js/**", "/resources/css/**", "/resources/img/**")
                .addResourceLocations("/resources/js/", "/resources/css/", "/resources/img/")
                .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
    }
}

我试着

@EnableWebMvc
@Configuration

但要找个404
只有

@Configuration

不缓存

ar7v8xwq

ar7v8xwq1#

你能试试这样的吗?

@Bean
public WebMvcConfigurerAdapter webConfigurer () {
      return new WebMvcConfigurerAdapter() {
          @Override
          public void addResourceHandlers (ResourceHandlerRegistry registry) {
              registry.addResourceHandler("/resources/js/**")
                      .addResourceLocations("/resources/js/")
                      .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
              registry.addResourceHandler("/resources/img/**")
              .addResourceLocations("/resources/img/")
              .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
              registry.addResourceHandler("/resources/css/**")
              .addResourceLocations("/resources/css/")
              .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
          }
      };
}

此外,SpringBoot的最新版本有一些properties来管理这个:

spring.resources.cache.cachecontrol.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
spring.resources.cache.cachecontrol.cache-public= # Indicate that any cache may store the response.
spring.resources.cache.cachecontrol.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
spring.resources.cache.cachecontrol.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
spring.resources.cache.cachecontrol.no-store= # Indicate to not cache the response in any case.
spring.resources.cache.cachecontrol.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
spring.resources.cache.cachecontrol.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
spring.resources.cache.cachecontrol.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.

您可以尝试使用上述任何properties来实现您的解决方案。

相关问题