<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>资源指纹 on ZiYang FrontEnd Interview</title><link>https://fe-interview.pangcy.cn/tags/%E8%B5%84%E6%BA%90%E6%8C%87%E7%BA%B9/</link><description>Recent content in 资源指纹 on ZiYang FrontEnd Interview</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 06 Mar 2025 13:07:39 +0800</lastBuildDate><atom:link href="https://fe-interview.pangcy.cn/tags/%E8%B5%84%E6%BA%90%E6%8C%87%E7%BA%B9/index.xml" rel="self" type="application/rss+xml"/><item><title>ETag与Last-Modified校验对比</title><link>https://fe-interview.pangcy.cn/docs/network/network-14/</link><pubDate>Tue, 04 Mar 2025 09:31:00 +0000</pubDate><guid>https://fe-interview.pangcy.cn/docs/network/network-14/</guid><description>&lt;h2 id="考察点分析">考察点分析 &lt;a href="#%e8%80%83%e5%af%9f%e7%82%b9%e5%88%86%e6%9e%90" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;p>该题目主要考察候选人对HTTP缓存验证机制的核心理解与工程权衡能力，重点评估以下维度：&lt;/p>
&lt;ol>
&lt;li>&lt;strong>缓存机制原理&lt;/strong>：对条件请求（Conditional Requests）中两种校验方式的工作机制理解&lt;/li>
&lt;li>&lt;strong>精度差异辨析&lt;/strong>：对比时间戳校验与内容指纹校验的检测颗粒度差异&lt;/li>
&lt;li>&lt;strong>强弱校验区别&lt;/strong>：理解强校验（Strong Validation）与弱校验（Weak Validation）的应用场景&lt;/li>
&lt;li>&lt;strong>性能权衡意识&lt;/strong>：分析服务端计算开销与缓存命中率的平衡点&lt;/li>
&lt;li>&lt;strong>HTTP协议规范&lt;/strong>：对&lt;code>ETag&lt;/code>响应头格式与验证流程的掌握程度&lt;/li>
&lt;/ol>
&lt;h2 id="技术解析">技术解析 &lt;a href="#%e6%8a%80%e6%9c%af%e8%a7%a3%e6%9e%90" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;h3 id="关键知识点">关键知识点 &lt;a href="#%e5%85%b3%e9%94%ae%e7%9f%a5%e8%af%86%e7%82%b9" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>&lt;ol>
&lt;li>&lt;strong>Last-Modified校验&lt;/strong>：基于文件修改时间的秒级校验&lt;/li>
&lt;li>&lt;strong>ETag强校验&lt;/strong>：基于内容哈希值的字节级校验（如&lt;code>&amp;quot;123456&amp;quot;&lt;/code>）&lt;/li>
&lt;li>&lt;strong>ETag弱校验&lt;/strong>：基于资源语义变化的校验（如&lt;code>W/&amp;quot;123&amp;quot;&lt;/code>）&lt;/li>
&lt;li>&lt;strong>验证流程&lt;/strong>：客户端通过&lt;code>If-Modified-Since&lt;/code>和&lt;code>If-None-Match&lt;/code>请求头传递验证令牌&lt;/li>
&lt;/ol>
&lt;h3 id="原理剖析">原理剖析 &lt;a href="#%e5%8e%9f%e7%90%86%e5%89%96%e6%9e%90" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>&lt;p>&lt;strong>Last-Modified校验&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>服务端返回资源的最后修改时间（如&lt;code>Last-Modified: Tue, 01 Jan 2024 nbsp;00:00:00 GMT&lt;/code>）&lt;/li>
&lt;li>客户端下次请求携带该时间，服务端对比文件系统时间戳&lt;/li>
&lt;li>问题：1秒内多次修改无法检测；文件内容未变但时间戳被修改（如备份操作）会导致缓存失效&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>ETag校验&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>强ETag（无&lt;code>W/&lt;/code>前缀）：要求字节完全一致。通过哈希算法（如MD5）或版本号生成&lt;/li>
&lt;li>弱ETag（带&lt;code>W/&lt;/code>前缀）：允许非实质性变化（如HTML注释修改）。通过关键元数据生成&lt;/li>
&lt;li>客户端请求时携带&lt;code>If-None-Match&lt;/code>头部，服务端进行哈希值比对&lt;/li>
&lt;/ul>
&lt;h3 id="常见误区">常见误区 &lt;a href="#%e5%b8%b8%e8%a7%81%e8%af%af%e5%8c%ba" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>&lt;ol>
&lt;li>认为Last-Modified的时间精度可以达到毫秒级（实际HTTP日期格式只支持秒级）&lt;/li>
&lt;li>误将弱校验ETag用于需要严格缓存验证的场景&lt;/li>
&lt;li>忽视ETag生成算法可能引发的集群服务器一致性难题&lt;/li>
&lt;li>认为ETag必须使用哈希算法（实际上可采用版本号等机制）&lt;/li>
&lt;/ol>
&lt;h2 id="问题解答">问题解答 &lt;a href="#%e9%97%ae%e9%a2%98%e8%a7%a3%e7%ad%94" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;p>Last-Modified与ETag的核心差异体现在检测维度和计算方式：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>维度&lt;/th>
 &lt;th>Last-Modified&lt;/th>
 &lt;th>ETag&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>检测依据&lt;/td>
 &lt;td>文件系统时间戳（秒级）&lt;/td>
 &lt;td>内容哈希/版本号（字节级）&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>修改检测&lt;/td>
 &lt;td>时间变化即失效&lt;/td>
 &lt;td>内容变化才失效&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>计算成本&lt;/td>
 &lt;td>读取文件属性（低开销）&lt;/td>
 &lt;td>计算哈希值（高开销）&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>典型场景&lt;/td>
 &lt;td>静态资源版本稳定&lt;/td>
 &lt;td>需要精确缓存控制&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>ETag更精准的原因&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>直接校验内容而非时间，避免时间篡改导致的误判&lt;/li>
&lt;li>弱校验可过滤无实质影响的修改（如CDN添加注释）&lt;/li>
&lt;li>支持集群环境下的版本同步（如版本号ETag）&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>计算开销考量&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>大文件哈希计算消耗CPU资源（需流式计算优化）&lt;/li>
&lt;li>动态内容需实时生成ETag增加延迟&lt;/li>
&lt;li>分布式系统需保持ETag生成算法一致性&lt;/li>
&lt;/ul>
&lt;h2 id="解决方案">解决方案 &lt;a href="#%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;h3 id="优化etag生成nodejs示例">优化ETag生成（Node.js示例） &lt;a href="#%e4%bc%98%e5%8c%96etag%e7%94%9f%e6%88%90nodejs%e7%a4%ba%e4%be%8b" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>


 
 
 

 
 
 
 

 

 &lt;div class="prism-codeblock ">
 &lt;pre id="18e8a77" class="language-javascript ">
 &lt;code>const crypto = require(&amp;#39;crypto&amp;#39;);
const fs = require(&amp;#39;fs&amp;#39;);

// 流式生成强ETag（SHA256哈希）
function generateStrongETag(filePath) {
 return new Promise((resolve) =&amp;gt; {
 const hash = crypto.createHash(&amp;#39;sha256&amp;#39;);
 fs.createReadStream(filePath)
 .on(&amp;#39;data&amp;#39;, (chunk) =&amp;gt; hash.update(chunk))
 .on(&amp;#39;end&amp;#39;, () =&amp;gt; resolve(hash.digest(&amp;#39;hex&amp;#39;)));
 });
}

// 弱ETag生成（文件大小&amp;#43;修改时间）
function generateWeakETag(filePath) {
 const stats = fs.statSync(filePath);
 return `W/&amp;#34;${stats.size}-${stats.mtimeMs}&amp;#34;`;
}&lt;/code>
 &lt;/pre>
 &lt;/div>
&lt;p>&lt;strong>优化策略&lt;/strong>：&lt;/p></description></item></channel></rss>