private,] max-age=${n}, s-maxage=${m}
expires
存在的问题是他依赖于客户端的系统时间, 客户端系统时间错误可能会引起判断错误. HTTP1.1增加了Cache-Control
解决此问题, 这个指令值比较丰富, 常见的如下:
public/private: 标识资源能不能被代理服务器缓存, public
标识资源既能被代理服务器缓存也能被浏览器缓存, private
标识资源只能被浏览器缓存, 不能被代理服务器缓存.
max-age: 用于指定在客户端缓存的有效时间, 单位s, 超过n秒需要重新请求, 不超过则可以使用缓存
s-maxage: 这个是针对代理服务器的, 表示资源在代理服务器缓存时间没有超过这个时间不必向源服务器请求, 否则需要.
no-cache: 有这个指令表示不走浏览器缓存了, 协商缓存还可以走
no-store: 强制无缓存, 协商缓存也不走了, 测试发下即使响应中有Last-Modified
, 浏览器请求时页不会带If-Modified-Since
一个实例

协商缓存
所谓协商缓存就是客户端想用缓存资源时先向服务器询问, 如果服务器如果认为这个资源没有过期, 可以继续用则给出304响应, 客户端继续使用原来的资源; 否则给出200, 并在响应body加上资源, 客户端使新的资源.
1.Last-Modified与If-Modified-Since
这个机制是, 服务器在响应头中加上Last-Modified
, 一般是一个资源的最后修改时间, 浏览器首次请求时获得这个时间, 下一次请求时将这个时间放在请求头的If-Modified-Since
, 服务器收到这个If-Modified-Since
时间n
后查询资源的最后修改时间m
与之对比, 若m>n
, 给出200响应, 更新Last-Modified
为新的值, body中为这个资源, 浏览器收到后使用新的资源; 否则给出304响应, body无数据, 浏览器使用上一次缓存的资源.
2.Etag与If-None-Match
Last-Modified
模式存两个问题, 一是它是秒级别的比对, 所以当资源的变化小于一秒时浏览器可能使用错误的资源; 二是资源的最新修改时间变了可能内容并没有变, 但是还是会给出完整响应, 造成浪费. 基于此在HTTP1.1引入了Etag模式.
这个与上面的Last-Modified
机制基本相同, 不过不再是比对最后修改时间而是比对资源的标识, 这个Etag一般是基于资源内容生成的标识. 由于Etag是基于内容生成的, 所以当且仅当内容变化才会给出完整响应, 无浪费和错误的问题.
演示第8, 10
如何选择缓存策略
https://tools.ietf.org/pdf/rfc7234.pdf
附录
1.演示代码
const http = require('http');
const fs = require('fs');
let etag = 0;
let tpl = fs.readFileSync('./index.html');
let img = fs.readFileSync('./test.png');
http.createServer((req, res) => {
etag++; // 我是个假的eTag
console.log('--->', req.url);
switch (req.url) {
// 模板
case '/index':
res.writeHead(200, {
'Content-Type': 'text/html',
'Cache-Control': 'no-store'
});
res.end(tpl);
break;
// 1. 不给任何与缓存相关的头, 任何情况下, 既不会被浏览器缓存, 也不会被代理服务缓存
case '/img/nothing_1':
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(img);
break;
// 2. 设置了no-cache表明每次要使用缓存资源前需要向服务器确认
case '/img/cache-control=no-cache_2':
res.writeHead(200, {
'Content-Type': 'image/png',
'cache-control': 'no-cache'
});
res.end(img);
break;
// 3. 设置max-age表示在浏览器最多缓存的时间
case '/img/cache-control=max-age_3':
res.writeHead(200, {
'Content-Type': 'image/png',
'cache-control': 'max-age=10'
});
res.end(img);
break;
// 4. 设置了max-age s-maxage public: public 是说这个资源可以被服务器缓存, 也可以被浏览器缓存,
// max-age意思是浏览器的最长缓存时间为n秒, s-maxage表明代理服务器的最长缓存时间为那么多秒
case '/img/cache-control=max-age_s-maxage_public_4':
res.writeHead(200, {
'Content-Type': 'image/png',
'cache-control': 'public, max-age=10, s-maxage=40'
});
res.end(img);
break;
// 设置了max-age s-maxage private: private 是说这个资源只能被浏览器缓存, 不能被代理服务器缓存
// max-age说明了在浏览器最长缓存时间, 这里的s-maxage实际是无效的, 因为不能被代理服务缓存
case '/img/cache-control=max-age_s-maxage_private_5':
res.writeHead(200, {
'Content-Type': 'image/png',
'cache-control': 'private, max-age=10, s-maxage=40'
});
res.end(img);
break;
// 7. 可以被代理服务器缓存, 确不能被浏览器缓存
case '/img/cache-control=private_max-age_7':
res.writeHead(200, {
'Content-Type': 'image/png',
'cache-control': 'public, s-maxage=40'
});
res.end(img);
break;
// 8. 协商缓存
case '/img/talk_8':
let stats = fs.statSync('./test.png');
let mtimeMs = stats.mtimeMs;
let If_Modified_Since = req.headers['if-modified-since'];
let oldTime = 0;
if(If_Modified_Since) {
const If_Modified_Since_Date = new Date(If_Modified_Since);
oldTime = If_Modified_Since_Date.getTime();
}
mtimeMs = Math.floor(mtimeMs / 1000) * 1000; // 这种方式的精度是秒, 所以毫秒的部分忽略掉
console.log('mtimeMs', mtimeMs);
console.log('oldTime', oldTime);
if(oldTime < mtimeMs) {
res.writeHead(200, {
'Cache-Control': 'no-cache',
// 测试发现, 必须要有max-age=0 或者no-cache,或者expires为当前, 才会协商, 否则没有协商的过程
'Last-Modified': new Date(mtimeMs).toGMTString()
});
res.end(fs.readFileSync('./test.png'));
}else {
res.writeHead(304);
res.end();
}
// 9. 设置了expires, 表示资源到期时间
case '/img/expires_9':
const d = new Date(Date.now() + 5000);
res.writeHead(200, {
'Content-Type': 'image/png',
'expires': d.toGMTString()
});
res.end(img);
break;
// 10. 设置了expires, 表示资源到期时间
case '/img/etag_10':
const If_None_Match = req.headers['if-none-match'];
console.log('If_None_Match,',If_None_Match);
if(If_None_Match != etag) {
res.writeHead(200, {
'Content-Type': 'image/png',
'Etag': String(etag)
});
res.end(img);
}else {
res.statusCode = 304;
res.end();
}
break;
// 11. no-store 能协商缓存吗? 不能, 请求不会带if-modified-since
case '/img/no-store_11':
const stats2 = fs.statSync('./test.png');
let mtimeMs2 = stats2.mtimeMs;
let If_Modified_Since2 = req.headers['if-modified-since'];
let oldTime2 = 0;
if(If_Modified_Since2) {
const If_Modified_Since_Date = new Date(If_Modified_Since2);
oldTime2 = If_Modified_Since_Date.getTime();
}
mtimeMs2 = Math.floor(mtimeMs2 / 1000) * 1000; // 这种方式的精度是秒, 所以毫秒的部分忽略掉
console.log('mtimeMs', mtimeMs2);
console.log('oldTime', oldTime2);
if(oldTime2 < mtimeMs2) {
res.writeHead(200, {
'Cache-Control': 'no-store',
// 测试发现, 必须要有max-age=0 或者no-cache,或者expires为当前, 才会协商, 否则没有协商的过程
'Last-Modified': new Date(mtimeMs2).toGMTString()
});
res.end(fs.readFileSync('./test.png'));
}else {
res.writeHead(304);
res.end();
}
default:
res.statusCode = 404;
res.statusMessage = 'Not found',
res.end();
}
}).listen(1234);