Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Optimization Spring是否只引导GZIP静态资源一次?_Optimization_Spring Boot_Gzip - Fatal编程技术网

Optimization Spring是否只引导GZIP静态资源一次?

Optimization Spring是否只引导GZIP静态资源一次?,optimization,spring-boot,gzip,Optimization,Spring Boot,Gzip,我已经为spring boot嵌入式服务器打开了gzip。我关心的是SpringBoot如何处理静态资源的gzip。由于这些都没有改变,spring boot(或底层嵌入式服务器)是否运行gzip算法一次,然后缓存结果?对每个静态资源请求运行gzip算法似乎是在浪费处理能力。您必须使用SpringResourceResolver,特别是在缓存方面。您的配置应符合以下要求: @Configuration @EnableWebMvc @EnableCaching public class MvcCo

我已经为spring boot嵌入式服务器打开了gzip。我关心的是SpringBoot如何处理静态资源的gzip。由于这些都没有改变,spring boot(或底层嵌入式服务器)是否运行gzip算法一次,然后缓存结果?对每个静态资源请求运行gzip算法似乎是在浪费处理能力。

您必须使用Spring
ResourceResolver
,特别是在缓存方面。您的配置应符合以下要求:

@Configuration
@EnableWebMvc
@EnableCaching
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private CacheManager cacheManager;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resources/**")
          .addResourceLocations("/resources/")
          .setCachePeriod(3600)  // Browser cache
          .resourceChain(true)
          .addResolver(new CachingResourceResolver(cacheManager, "resourceCache"))
          .addResolver(new GzipResourceResolver())
          .addResolver(new PathResourceResolver());
    }
}

重要的是,必须为应用程序配置CacheManager,因此请查看以获取更多信息(可能最适合使用咖啡因的本地缓存)。

GzipResourceResolver在资源目录中查找
*.gz
文件,您必须在构建时准备这些文件。我认为,它不会动态地gzip资源,这会解决您的问题

主要文件并未直接涵盖以下内容:

您可以通过以下方式进行配置:

1) 在构建时,创建所有静态文件的gzip副本,例如:

gzip --keep --best -r src/main/resources/public
2) 通过添加如下类来配置spring:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/**")
          .addResourceLocations("/public/")
          .resourceChain(true) // cache resource lookups
          .addResolver(new GzipResourceResolver())
          .addResolver(new PathResourceResolver());
    }
}

你测试过这个吗?我认为这行不通;看看我的答案。