从缓存中排除特定文件(静态资源) |Spring Boot Web [英] Exclude particular file (static resource) from being cached | Spring Boot Web

查看:30
本文介绍了从缓存中排除特定文件(静态资源) |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.

推荐答案

  1. addResourceHandler 方法中,您指定 URL 或 URL 模式.
  2. addResourceLocations 中,您指定一个实际包含(或可能有许多用逗号分隔的目录)静态资源的有效目录.
  1. In the addResourceHandler method, you specify URL, or URL pattern.
  2. 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.

  1. 如果你只想对文件 prop.js 禁用缓存,试试这个:

  1. 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屋!

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