从缓存中排除特定文件(静态资源) |Spring Boot Web [英] Exclude particular file (static resource) from being cached | Spring Boot Web
问题描述
我有一个 spring boot 应用程序,下面是注册资源处理程序的代码:
I have a spring boot application and below is the code for registrations of resource handlers:
@Configuration
public class MyAppConfig extends WebMvcConfigurerAdapter {
...
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/js/**")
.addResourceLocations("/resources/js/").setCachePeriod(31536000);
//in same directory where all the js files placed,
//I want to cache all the files in that directory
//except for one file i.e. prop.js
/*registry.addResourceHandler("/resources/js/prop.js")
.addResourceLocations("/resources/js/").setCachePeriod(0);*/
}
...
}
有些事情我需要理解,即.
1、addResourceHandler
的参数是什么意思?
2、addResourceLocations
的参数是什么意思?
3. 有什么办法可以明确防止prop.js
被缓存?
There are certain things I need to understand, viz.
1. What does the parameter of addResourceHandler
mean?
2. What does the parameter of addResourceLocations
mean?
3. Is there any way to explicitly prevent prop.js
from being cached?
可能有与 prop.js
处于同一级别的文件,我希望缓存所有其他文件.
There are may files at the same level where prop.js
is, I want all other files to be cached.
推荐答案
- 在
addResourceHandler
方法中,您指定 URL 或 URL 模式. - 在
addResourceLocations
中,您指定一个实际包含(或可能有许多用逗号分隔的目录)静态资源的有效目录.
- In the
addResourceHandler
method, you specify URL, or URL pattern. - In the
addResourceLocations
, you specify a valid directory actually containing (or possibly many directories separated by commas) the static resources.
所以最后,您在网络服务器上的 URL ans 目录之间做了一个简单的映射.
So in the end, you made a simple mapping between URL ans directories on your web-server.
如果你只想对文件
prop.js
禁用缓存,试试这个:
if you want to disable caching only for the file
prop.js
, try this :
registry
.addResourceHandler("/resources/js/**/prop.js")
.addResourceLocations("/resources/js/")
.setCachePeriod(0);
registry
.addResourceHandler("/resources/js/**")
.addResourceLocations("/resources/js/")
.setCachePeriod(31536000);
这篇关于从缓存中排除特定文件(静态资源) |Spring Boot Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!