如何有选择地禁用 Spring Boot 的缓存(manifest.appcache)

Jim*_*imB 5 html5-appcache spring-boot

从这个问题可以看出,Spring Security 管理 Spring Boot 的缓存。从 Spring Boot文档中,它展示了如何使用以下命令设置资源缓存:

spring.resources.cache-period= # cache timeouts in headers sent to browser
Run Code Online (Sandbox Code Playgroud)

cache-period对于 Spring Boot 的所有预定义静态位置(即/css**/js/**/images/**)来说非常有用,但我还生成了一个manifest.appcache用于离线下载我的静态资产的文件,并且由于上述所有 Spring Security/Boot 都会使用 manifest.appcache 发送回缓存标头

"method": "GET",
"path": "/manifest.appcache",
"response": {
    "X-Application-Context": "application:local,flyway,oracle,kerberos:8080",
    "Expires": "Tue, 06 Oct 2015 16:59:39 GMT",
    "Cache-Control": "max-age=31556926, must-revalidate",
    "status": "304"
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何添加排除项manifest.appcache。无论我的标头如何,IE 和 Chrome 似乎都会对 appcache 进行“正确的操作”,但 FF 似乎在注意到 appcache 发生更改时更加特殊,并且我认为我的缓存标头将其搞砸了。

编辑:我应该从WebMvcAutoConfiguration的源代码中添加它,它显示了如何为资源设置缓存,我只是不确定如何有选择地禁用我的 1 个案例,而不会潜在地破坏 spring boot 在该文件中设置的其余内容。

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!this.resourceProperties.isAddMappings()) {
            logger.debug("Default resource handling disabled");
            return;
        }

        Integer cachePeriod = this.resourceProperties.getCachePeriod();
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/")
                    .setCachePeriod(cachePeriod);
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**")
                    .addResourceLocations(RESOURCE_LOCATIONS)
                    .setCachePeriod(cachePeriod);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ger*_*ann 2

感谢您的问题和A!

我们遇到了类似的问题:所有浏览器(Chrome、Safari、IE),但其中一个(FF)没有缓存清单文件本身,而是在更改后重新加载它。

然而,在Spring Boot中设置缓存周期设置并没有完全解决问题。此外,我不想为 Spring Boot 应用程序提供的所有文件设置缓存控制参数,而只想禁用清单的缓存。

我的解决方案由两部分组成:

  1. 在清单文件中提供“rev”注释。尽管W3C 规范未涵盖,但某些浏览器似乎需要这样的注释来检测清单或引用(缓存)文件中的更改:

    # rev 7
    
    Run Code Online (Sandbox Code Playgroud)

    现在,我们通过 Maven 生成的唯一内部版本号更改缓存清单文件中的“rev”参数: https: //github.com/dukecon/dukecon_html5/commit/b60298f0b856a7e54c97620f278982142e3e1f45)。

  2. 通过提供一个过滤器来禁用缓存,该过滤器将标头“Cache-Control: no-cache”添加到完全相同的cache.manifest文件模式中:https ://github.com/dukecon/dukecon_server/commit/dc02f26996cb172df804da007546f439df75126d