当前位置:首页 » 文件管理 » 静态资源缓存

静态资源缓存

发布时间: 2022-02-11 05:30:57

缓存静态资源,不知怎么解决

之前看过apach及nginx对于静态资源(含js,图片,css等)部分的缓存,用于加速并减轻后台实际web服务器的压力。
静态资源缓存是WEB服务器优化的一种手段,基本原理如下:
1.客户端浏览器请求服务器一个服务(该服务含有图片,js等静态资源),通常会对于每一个网页中的独立图片或js文件发送一个http请求
2.WEB服务器对于每个资源HTTP请求进行解析,并生成一个资源修改时间的唯一值(可以是etag或last_modified参数),放入服务器端map,key为资源url,value为资源修改时间。最后将此资源修改时间的唯一值包含在http头上返回,因为是首次请求,所以会将所有内容放在http body中一并返回给客户浏览器端
3.客户浏览器接收服服务器响应,并将服务器返回的资源修改时间作为key放入浏览器客户端,value为http body中的实际资源内容
4.客户浏览器再次请求静态资源时,会将资源修改时间一并发送给服务器
5.服务端会从最新的map中取出该资源url对应的修改时间,如果值晚于客户端请求的资源修改时间,这时会返回最新的已经修改过的资源给客户端。否则返回304 not modifed

这里记录资源修改时间的方式有etag及last_modified。最先有的是last_modified,它的工作方式就是上述介绍的,但缺点是只能精确到秒级别。也就是说当你在一秒中修改资源两次,而客户端拿到的是第一次修改,那之后就算客户端第二次再次请求也不会拿到最新的资源。
而etag的出现正是为了解决last_modified的秒级问题,于http 1.1被提出。

今天测试了下,在没有nginx等前端反向代理服务器时,tomcat竟然默认对静态资源做了缓存。
tomcat默认运用etag及last_modifed。etag与if_no_match(客户端浏览器上传时在http head中应该放的属性名)一起使用,last_modified与If-Modified-Since一起使用。

客户端首次请求时,得到请求响应数据如下:

GET http://localhost:8080/webTest/jsp/index.jsp [HTTP/1.1 200 OK 1ms]
GET http://localhost:8080/webTest/js/hello.js [HTTP/1.1 200 OK 1ms]
GET http://localhost:8080/webTest/img/a.jpg [HTTP/1.1 200 OK 2ms]
我们看一下Hello.js这个请求响应具体信息:
server Apache-Coyote/1.1 (表明服务器是tomcat)
Last-Modified: Sun, 11 May 2014 10:54:33 GMT
Etag: W/"175-1399805673000"
Date: Sun, 11 May 2014 10:59:23 GMT
Content-Type: application/javascript;charset=UTF-8
Content-Length: 175
Accept-Ranges: bytes

从上面可以看到tomcat即返回了last_modified也返回了etag。

客户端再次请求时,请求数据如下:
If-None-Match: W/"175-1399805673000"
If-Modified-Since: Sun, 11 May 2014 10:54:33 GMT
响应如下:

GET http://localhost:8080/webTest/jsp/index.jsp [HTTP/1.1 200 OK 1ms]
GET http://localhost:8080/webTest/js/hello.js [HTTP/1.1 304 Not Modified 1ms]
GET http://localhost:8080/webTest/img/a.jpg [HTTP/1.1 304 Not Modified 1ms]
从中我们可以看到tomcat对于静态数据作了缓存。

接着我们分析tomcat对于这部分静态缓存的判断处理,这部分逻辑是写在DefaultServlet类中,
我们可以在doGet方法中进入ServiceContext方法中找到以下源码
// Check if the conditions specified in the optional If headers are
// satisfied.
if (cacheEntry.context == null) {

// Checking If headers
boolean included =
(request.getAttribute(Globals.INCLUDE_CONTEXT_PATH_ATTR) != null);
if (!included
&& !checkIfHeaders(request, response, cacheEntry.attributes)) { //这句判断是否需要返回整个资源请求
return;
}

}
上面源码的 if (!included
&& !checkIfHeaders(request, response, cacheEntry.attributes))
用于判断是否需要返回整个资源,如果indcluded与checkIfHeaders方法返回的都是false,这时就直接返回,说明资源未修改,或者是缓存不支持的请求方式。
我们接着查看checkIfHeaders方法:
/**
* Check if the conditions specified in the optional If headers are
* satisfied.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param resourceAttributes The resource information
* @return boolean true if the resource meets all the specified conditions,
* and false if any of the conditions is not satisfied, in which case
* request processing is stopped
*/
protected boolean checkIfHeaders(HttpServletRequest request,
HttpServletResponse response,
ResourceAttributes resourceAttributes)
throws IOException {

return checkIfMatch(request, response, resourceAttributes)
&& checkIfModifiedSince(request, response, resourceAttributes)
&& checkIfNoneMatch(request, response, resourceAttributes)
&& checkIfUnmodifiedSince(request, response, resourceAttributes);

}

可以看到tomcat只有当这四个属性全部返回true(也就是说全部认为资源已经改变)才会返回true,这样最终会将整个资源(最新修改过的)返回客户端。
在这里,我们从上面实际过程当中看到,浏览器第二次请求资源时在http请求header中放了
If-None-Match: W/"175-1399805673000"
If-Modified-Since: Sun, 11 May 2014 10:54:33 GMT
这两个属性。
因此我们查看
&& checkIfModifiedSince(request, response, resourceAttributes)
&& checkIfNoneMatch(request, response, resourceAttributes)
这两个方法
checkIfModifiedSince源码如下:
/**
* Check if the if-modified-since condition is satisfied.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param resourceInfo File object
* @return boolean true if the resource meets the specified condition,
* and false if the condition is not satisfied, in which case request
* processing is stopped
*/
protected boolean checkIfModifiedSince(HttpServletRequest request,
HttpServletResponse response,
ResourceAttributes resourceAttributes) {
try {
long headerValue = request.getDateHeader("If-Modified-Since");
long lastModified = resourceAttributes.getLastModified();
if (headerValue != -1) {

// If an If-None-Match header has been specified, if modified since
// is ignored.
if ((request.getHeader("If-None-Match") == null)
&& (lastModified < headerValue + 1000)) {
// The entity has not been modified since the date
// specified by the client. This is not an error case.
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setHeader("ETag", resourceAttributes.getETag());

return false;
}
}
} catch (IllegalArgumentException illegalArgument) {
return true;
}
return true;

}
源码中可以看到:
if ((request.getHeader("If-None-Match") == null)
&& (lastModified < headerValue + 1000)) {
这句话表明只有在客户端浏览器发送的请求头中不包含If-None-Match,IfModifiedSince才会生效。
我们接着看checkIfNoneMatch,源码如下:
/**
* Check if the if-none-match condition is satisfied.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param resourceInfo File object
* @return boolean true if the resource meets the specified condition,
* and false if the condition is not satisfied, in which case request
* processing is stopped
*/
protected boolean checkIfNoneMatch(HttpServletRequest request,
HttpServletResponse response,
ResourceAttributes resourceAttributes)
throws IOException {

String eTag = resourceAttributes.getETag();
String headerValue = request.getHeader("If-None-Match");
if (headerValue != null) {

boolean conditionSatisfied = false;

if (!headerValue.equals("*")) {

StringTokenizer commaTokenizer =
new StringTokenizer(headerValue, ",");

while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
String currentToken = commaTokenizer.nextToken();
if (currentToken.trim().equals(eTag))
conditionSatisfied = true;
}

} else {
conditionSatisfied = true;
}

if (conditionSatisfied) {

// For GET and HEAD, we should respond with
// 304 Not Modified.
// For every other method, 412 Precondition Failed is sent
// back.
if ( ("GET".equals(request.getMethod()))
|| ("HEAD".equals(request.getMethod())) ) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setHeader("ETag", eTag);

return false;
}
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
}
return true;

}
这里:
String eTag = resourceAttributes.getETag();
String headerValue = request.getHeader("If-None-Match");
这两句比较简单,就是分别从服务器缓存和http请求头中中取出etag。
接着判断这两个etag如果相等,则conditionSatisfied为true,会执行到以下语句:
if (conditionSatisfied) {

// For GET and HEAD, we should respond with
// 304 Not Modified.
// For every other method, 412 Precondition Failed is sent
// back.
if ( ("GET".equals(request.getMethod()))
|| ("HEAD".equals(request.getMethod())) ) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setHeader("ETag", eTag);

return false;
}
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
这段语句中可以发现,如果资源未改变的情况下,并且请求方式为GET或者HEAD时,会返回304状态码。否则返回一个412状态码,同样不会返回资源内容。
如果上述最终
if ((request.getHeader("If-None-Match") == null)
&& (lastModified < headerValue + 1000))
条件不成立,即资源更新了或者是第一次请求,这里会读取当前请求资源文件,并最终放入http响应中。

⑵ ios中webview怎么做静态资源的缓存

1、在重写onkeydown方法 即可,例如: @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { goback } return super.dispatchKeyEvent(event); } 2、这个方法就可以监听按钮返回键或者简直返回键操作。 3、return false就禁止返回。

⑶ 如何为WordPress和Typecho设置静态资源缓存

在HTTP标头中为静态资源设置过期日期或最长存在时间,可指示浏览器从本地磁盘中加载以前下载的资源,而不是通过网络加载。这样, 网站加载速度会更快.
下面的代码都需要放到.htaccess中才能生效. 推荐设置过期时间为一个月, 即: max-age=2592000.
####通过FilesMatch设置
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|css|js)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
####通过mod_expires.c设置
<IfMole mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 months"
ExpiresByType text/html "access plus 1 months"
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpeg "access plus 2 months"
ExpiresByType application/x-shockwave-flash "access plus 2 months"
ExpiresByType application/x-javascript "access plus 2 months"
</IfMole>
如需针对具体的资源类型需要这样:
ExpiresByType image/gif "access plus 5 hours 3 minutes"
更多规则参考Apache Mole mod_expires.
参考来源:
Cache Control with htaccess FILES MATCH
Apache里配置 mod_expires 和 mod_headers 以支持CACHE
Tags: wordpress静态资源缓存, typecho静态资源缓存, wordpress浏览器缓存, typecho浏览器缓存, 静态资源缓存, 浏览器缓存,wordpress cache expire, typecho cache expire

⑷ 如何将静态资源缓存在cdn节点上

首先要纠正这个问题,动态内容是没办法缓存的。
动态内容需要的是信息的实时交互,如果采用了缓存,会影响正常业务。
举个例子,国航官网,订票内容就是动态的,如果这时候将这部分进行缓存,那当用户订票的时候,官网显示剩余10张票,并且在缓存过期内,会一直显示10张票。而实际数据库里已经没有票了。这时候势必会影响到客户体验。

动态内容的CDN加速其实还是链路和协议的优化。
首先,国内ISP复杂,南北互通的问题,会导致访问速度慢,CDN厂商有覆盖全国的PBL网络(可以理解为CDN提供商自己的私有网络,独立的ISP),当采用动态加速时,将内容引入CDN供应商的网络内,再根据分布在全国的CDN节点作为接入和落地口,达到链路的最优。
其次,CDN供应商会针对TCP等协议进行优化和调整,使正常的TCP三次握手减少到1次,从而减少计算机与计算机、路由之间的信息传递环节,从而达到加速目的。

⑸ 如何解决网站静态页缓存问题

使用静态页缓存提升网站性能,变化很少的静态资源可以设置客户端缓存时间,减少请求。
如果一定要不要静态页缓存,可以设置页面不缓存静态页面,这个要根据实际的WEB应用服务进行设置了。

⑹ 为什么要把静态资源放在WEB-INF下

方法一:javaWeb的程序如果使用的开发工具是Eclipse,那么在项目下回自动生成一个WebContent文件夹,使用的是MyEclipse工具开发,那么会有个WebRoot文件夹。方法2:javaWeb工程的话一定会有jsp文件存在的啦javaWeb工程作为java工程导入

⑺ 给静态资源的header加控制缓存的参数有多大意义

让静态资源不被SpringMVC分配器过滤有两种法:①把所有的JS和CSS文件移至别的文件夹②为resources文件夹需要被过滤的文件类型分别写一个mapping第二种方法是在web.xml配置静态资源映射到default去吧。第一种方法意思是,比如之前的目录结构是把css,js放在webroot下的resources文件夹中,页面上通过这样的请求来访问。

⑻ 做前端静态资源缓存服务器有哪些成熟易搭建的方案

我现在是把阿里云的 CDN 直接解析到 OSS 。

每天的 PV , 1 万到 5 万。

然而才用了一个多月就跑了 300+G 流量。 0.36/GB 。泪。

阿里云的 CDN 实在是太贵了,用峰值带宽的话,根本就不能控制成本啊!万一有个用户 100M 水管,那一天岂不是要付 100 块钱?

所以还不如选一个好一点的 BGP 线路机器反代到 OSS 。

自己用 squid 搭建吗?
如果主站是 HTTPS 的, squid 能配置 SSL 吗?还是说要 nginx 配置 SSL 以后再去反代 squid ,然后 squid 反代 oss ?
有没有配置脚本
还是装个 AMH/WDCP 之类面板,然后可以傻瓜化配置?

对主机磁盘 IO 、内存有什么要求?

⑼ 静态资源存储在localStorage有什么缺点

1.localStorage一般只能存5m。 2.老的浏览器不支持HTML5。 3.有些浏览器如果使用‘无痕浏览模式‘会禁用localStorage的setItem方法。 建议:用localStorage作为一个不可靠的缓存使用,优化性能(如:缓存ajax请求),而不是作为数据库使用。

⑽ springmvc 怎么清除前台静态资源缓存

让静态资源不被SpringMVC分配器过滤有两种办法:
① 把所有的 JS 和 CSS 文件移至别的文件夹
② 为 resources 文件夹需要被过滤的文件类型分别写一个 mapping

第二种方法是在web.xml配置静态资源映射到default去吧。
第一种方法意思是,比如之前的目录结构是把css,js放在webroot下的resources文件夹中,页面上通过 这样的请求来访问。

热点内容
如何在conda中安装需要编译的软件 发布:2024-05-03 02:41:21 浏览:780
易语言垃圾清理源码 发布:2024-05-03 02:40:34 浏览:182
滴滴苹果安卓哪个派单量好 发布:2024-05-03 02:22:52 浏览:289
lua编程pdf 发布:2024-05-03 01:39:26 浏览:24
脐带血存储的三大忌 发布:2024-05-03 01:32:38 浏览:934
宝马可编程 发布:2024-05-03 01:32:20 浏览:226
匿名ftp的作用 发布:2024-05-03 01:30:13 浏览:420
php支付程序 发布:2024-05-03 01:07:09 浏览:287
安卓插口是什么型号图片 发布:2024-05-03 01:01:57 浏览:253
移动android开发 发布:2024-05-03 00:26:42 浏览:628