springboot设置静态资源缓存一年
由于本人所在项目组,所用的项目是前后端分离的,前端是React 的SPA,每次打包都会新版本的静态文件。
然而,在有些时候,这些静态资源是不变的,故可以将资源缓存至用户本地,提升性能。
写法如下,需要继承WebMvcConfigurerAdapter类,并重写addResourceHandlers方法。就可以告诉浏览器强制缓存
package cn.ixan.search.configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.concurrent.TimeUnit; @Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* <p>设置静态资源缓存时间:1年</p>
* <p>注意:不可缓存入口文件</p>
* @param registry 资源注册
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/*.js","/*.css")
.addResourceLocations("classpath:/static/")
//.setCacheControl(CacheControl.maxAge(,TimeUnit.DAYS).cachePublic())
.setCachePeriod();
}
}
据某位大佬说,大家可以强制设置静态资源缓存一年,但一定不要缓存入口文件,大家可以试试改造自己的项目。
如果设置成功,会有下面的效果,from disk cache,一般情况是from memory cache

大家还可以注意下cache-control这个值,当然这个值是以秒为单位的。

有兴趣的同学可以研究研究cache-control,补充补充web基础知识
springboot设置静态资源缓存一年的更多相关文章
- springboot设置静态资源不拦截的方法
springboot设置静态资源不拦截的方法 springboot不拦截静态资源需配置如下的类: import org.springframework.context.annotation.Confi ...
- SpringBoot 配置静态资源映射
SpringBoot 配置静态资源映射 (嵌入式servlet容器)先决知识 request.getSession().getServletContext().getRealPath("/& ...
- springboot下静态资源的处理(转)
在SpringBoot中有默认的静态资源文件相关配置,需要通过如下源码跟踪: WebMvcAutoConfiguration-->configureResourceChain(method)-- ...
- springboot配置静态资源访问路径
其实在springboot中静态资源的映射文件是在resources目录下的static文件夹,springboot推荐我们将静态资源放在static文件夹下,因为默认配置就是classpath:/s ...
- SpringBoot - 搭建静态资源存储服务器
目录 前言 环境 实现效果 具体实现 文件上传 配置类 上传接口 上传实现 辅助类 实体 上传测试 文件访问 配置类 项目源码 前言 记录下SpringBoot下静态资源存储服务器的搭建. 环境 wi ...
- springboot访问静态资源404
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...
- 【SpringBoot】06.SpringBoot访问静态资源
SpringBoot访问静态资源 1.SpringBoot从classpath/static的目录 目录名称必须是static 启动项目,访问http://localhost:8080/0101.jp ...
- nodejs koa2 设置 静态资源目录
参考这篇文章:https://blog.csdn.net/qq_38262910/article/details/89147571?utm_medium=distribute.pc_relevant_ ...
随机推荐
- Ceph中PG和PGP的区别
http://www.zphj1987.com/2016/10/19/Ceph%E4%B8%ADPG%E5%92%8CPGP%E7%9A%84%E5%8C%BA%E5%88%AB/ 一.前言 首先来一 ...
- spring整合hibernate之买书小测试
spring来整合hibernate就是用spring来管理hibernate的sessionFactory和让hibernate来使用spring的声明式事务. 一:加入相应的jar包. 二:写hi ...
- 系统的重要文件/etc/inittab被删除了--急救办法!
如果在生产环境中,系统的重要文件/etc/inittab被删除了(系统还没重启,崩溃前),不要急,下面告诉你该如何处理.1.模拟误删除文件[root@localhost ~]# rm -rf /etc ...
- win7 SP1 原版 32位 百度网盘下载
下载地址:https://pan.baidu.com/s/1o6I410XduG1kcmn9vQ3miw 提取码:15vm 扫码下载:
- 为什么NULL能多次free
void __cdecl _free_base (void * pBlock) { int retval = 0; if (pBlock == NULL) ...
- Gson extend 思路
package org.rx.core.internal; import com.google.gson.*; import net.sf.cglib.proxy.Enhancer; import n ...
- MySQL coalesce函数用法说明(转)
在mysql中,其实有不少方法和函数是很有用的,一个叫coalesce的,作用是将返回传入的参数中第一个非null的值,比如 SELECT COALESCE(NULL, NULL, 1); -- Re ...
- Source Insight下载及注册码
下载地址:http://www.sourceinsight.com/down35.html 注册码: SI3US-205035-36448 SI3US-466908-65897 SI3US-36893 ...
- maven项目报错:Class path contains multiple SLF4J bindings
maven项目编译不报错,运行时报错如下: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [b ...
- OA集成备注
1. 查看轨迹方法<script type="text/javascript"> function WinOpenIt(url) { //alert(1); var t ...