Skip to content

◷ 发表于: 2025-03-18

◷ 更新于: 2025-03-27

🅆 字数: 0

缓存命中率指标

javascript
// 上报数据到 Grafana
function report(name, label) {
// ...
}

// 检查资源加载是否命中缓存
function checkResourceCacheHit() {
    // 获取页面加载性能信息
    const perfEntries = performance.getEntriesByType('resource');

    for (const entry of perfEntries) {
        // 判断资源的加载时间是否小于50毫秒
        // 50ms 来自于经验总结,可以根据实际情况调整
        let hitCache = entry.duration < 50;
        report('cacheHiteRate', hitCache);
    }
}

setTimeout(() => {
    checkResourceCacheHit();
}, 3000);

基于 CC BY-NC-SA 4.0 许可发布