如何有选择地禁用 Spring Boot 的缓存(manifest.appcache) [英] how to selectively disable cache for spring boot (manifest.appcache)

查看:20
本文介绍了如何有选择地禁用 Spring Boot 的缓存(manifest.appcache)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个问题它表明春天安全管理弹簧启动缓存.来自 spring boot 文档 它显示了如何使用以下方法为资源设置缓存:

From this question it shows that spring security manages cache for spring boot. From the spring boot documentation it shows how to set cache for resources using:

spring.resources.cache-period= # cache timeouts in headers sent to browser

cache-period 非常适合 Spring Boot 的所有预定义静态位置(即 /css**, /js/**>, /images/**) 但我也在生成一个 manifest.appcache 用于离线下载我的静态资产,并且由于所有上述 spring 安全/启动发送使用 manifest.appcache 返回缓存标头

The cache-period is great for all the predefined static locations for spring boot (i.e. /css**, /js/**, /images/**) but I'm also generating a manifest.appcache for offline downloading of my static assets and due to all the above spring security/boot sends back cache headers with the 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"
}

我想知道如何为 manifest.appcache 添加排除项.无论我的标题如何,IE 和 Chrome 似乎都对 appcache做正确的事",但 FF 在注意到 appcache 发生变化时似乎更奇怪,我认为我的缓存标题搞砸了.

I'd like to know how to add an exclusion for manifest.appcache. IE and Chrome seem to 'do the right thing' with appcache regardless of my headers, but FF seems to be a little more peculiar in noting when the appcache has changed and I'm thinking my cache headers are screwing it up.

我应该从源添加 WebMvcAutoConfiguration 它显示了如何为资源设置缓存,我只是不确定如何有选择地禁用我的 1 个案例破坏 spring boot 在这个文件中设置的其余部分.

I should add from the source for WebMvcAutoConfiguration it shows how the cache is setup for the resources, I'm just unsure how to selectively disable for my 1 case w/o potentially disrupting the rest of what spring boot sets up in this file.

    @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);
        }
    }

推荐答案

感谢 Q 和 A!

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

We had similar problems: All browsers (Chrome, Safari, IE) but one (FF) didn't cache the manifest files themselves but reloaded it after a change.

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

However, setting the cache period setting in Spring Boot didn't solve the problem completely. Additionally I didn't want to set cache-control parameters for all files served by the Spring Boot application, but only to disable caching for the manifest.

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

My solution consists of of two parts:

  1. 在清单文件中提供rev"注释.尽管 W3C 规范 未涵盖,但某些浏览器似乎想要这样的评论这是为了检测清单或引用(缓存)文件中的更改:

  1. Provide a "rev" comment in the Manifest file. Though not covered by the W3C spec some browsers seem to want a comment like this to detect changes in the manifest or the referenced (cached) files:

# rev 7

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

We now change the "rev" parameter in the cache manifest file by a Maven generated unique build number: https://github.com/dukecon/dukecon_html5/commit/b60298f0b856a7e54c97620f278982142e3e1f45).

这篇关于如何有选择地禁用 Spring Boot 的缓存(manifest.appcache)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆