"这页面怎么又卡了?" 小张第3次刷新电商网站的商品列表页,手指不耐烦地敲击着桌面,每次点击都要重新加载全部数据,用户体验差到让人想摔键盘,作为前端开发,你很清楚——这时候就该缓存登场了。
缓存就像是你办公桌最顺手那个抽屉,把常用工具放在触手可及的地方,而不是每次用都要跑去仓库翻找,在前端开发中,合理使用缓存能让你的应用快如闪电,今天我们就来聊聊如何用JavaScript实现数据缓存。
// 存储数据 function setLocalCache(key, value) { try { const data = JSON.stringify(value); localStorage.setItem(key, data); console.log(`成功缓存数据到localStorage: ${key}`); } catch (error) { console.error('localStorage存储失败:', error); } } // 读取数据 function getLocalCache(key) { try { const data = localStorage.getItem(key); return data ? JSON.parse(data) : null; } catch (error) { console.error('localStorage读取失败:', error); return null; } } // 示例使用 const productList = [{id: 1, name: "新款手机"}, {id: 2, name: "无线耳机"}]; setLocalCache('product_list', productList); // 需要时取出 const cachedProducts = getLocalCache('product_list'); if (cachedProducts) { console.log('从缓存获取商品列表:', cachedProducts); }
特点:
// 存储会话数据 function setSessionCache(key, value) { try { const data = JSON.stringify(value); sessionStorage.setItem(key, data); } catch (error) { console.error('sessionStorage存储失败:', error); } } // 读取会话数据 function getSessionCache(key) { try { const data = sessionStorage.getItem(key); return data ? JSON.parse(data) : null; } catch (error) { console.error('sessionStorage读取失败:', error); return null; } } // 示例:存储用户当前会话的临时偏好 setSessionCache('user_theme_preference', 'dark'); // 页面刷新后仍可获取 const theme = getSessionCache('user_theme_preference');
特点:
有时候我们需要更快的访问速度,可以使用纯内存缓存:
class MemoryCache { constructor() { this.cache = new Map(); this.defaultTTL = 60 * 60 * 1000; // 默认1小时过期 } set(key, value, ttl = this.defaultTTL) { const expireAt = Date.now() + ttl; this.cache.set(key, { value, expireAt }); console.log(`内存缓存已更新: ${key}`); } get(key) { const item = this.cache.get(key); if (!item) return null; if (Date.now() > item.expireAt) { this.cache.delete(key); return null; } return item.value; } delete(key) { return this.cache.delete(key); } clear() { this.cache.clear(); } } // 使用示例 const cache = new MemoryCache(); // 缓存API响应 cache.set('user_profile', {name: '张三', age: 28}, 30 * 60 * 1000); // 30分钟过期 // 获取缓存 const profile = cache.get('user_profile'); if (profile) { console.log('从内存获取用户资料:', profile); } else { console.log('缓存已过期或不存在'); }
适用场景:
结合内存缓存和localStorage,实现一个智能缓存系统:
class SmartCache { constructor() { this.memoryCache = new Map(); this.syncQueue = new Set(); // 防止重复刷新 } // 获取数据,如果过期会自动刷新 async get(key, fetchFunc, ttl = 3600000) { // 1. 先检查内存缓存 const memoryItem = this.memoryCache.get(key); if (memoryItem && Date.now() < memoryItem.expireAt) { return memoryItem.value; } // 2. 检查localStorage const localItem = getLocalCache(key); if (localItem && localItem.expireAt > Date.now()) { // 更新到内存缓存 this.memoryCache.set(key, { value: localItem.value, expireAt: localItem.expireAt }); return localItem.value; } // 3. 如果正在刷新,避免重复请求 if (this.syncQueue.has(key)) { return localItem?.value || null; // 返回过期数据或null } // 4. 数据已过期,需要刷新 this.syncQueue.add(key); try { console.log(`正在刷新缓存: ${key}`); const freshData = await fetchFunc(); // 更新缓存 const expireAt = Date.now() + ttl; this.memoryCache.set(key, { value: freshData, expireAt }); setLocalCache(key, { value: freshData, expireAt }); return freshData; } catch (error) { console.error(`刷新缓存失败: ${key}`, error); return localItem?.value || null; // 降级返回旧数据 } finally { this.syncQueue.delete(key); } } } // 使用示例 const apiCache = new SmartCache(); // 模拟API请求函数 async function fetchUserData() { console.log('真实API请求...'); // 这里应该是实际的fetch或axios调用 return { name: '李四', lastLogin: new Date().toISOString() }; } // 获取用户数据,会自动管理缓存 async function getUserData() { return apiCache.get('user_data', fetchUserData, 5 * 60 * 1000); // 5分钟缓存 } // 在组件中使用 async function displayUser() { const userData = await getUserData(); console.log('用户数据:', userData); } // 第一次调用会触发API请求 displayUser(); // 5分钟内再次调用会直接返回缓存 setTimeout(displayUser, 1000);
问题1:缓存雪崩
ttl * (0.9 + Math.random()*0.2)
问题2:内存泄漏
问题3:缓存污染
根据2025年8月的前沿实践,这些缓存技术值得关注:
缓存就像前端开发的"超能力",用得好能让你的应用快人一步,用不好反而会成为负担,记住几个黄金法则:
去给你的项目加上合适的缓存吧,用户会感谢你的!
本文由 濯子 于2025-08-02发表在【云服务器提供商】,文中图片由(濯子)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://vps.7tqx.com/wenda/519552.html
发表评论