`title: IndexedDB 数据归档与清理窗口设计``categories: Web 开发/前端/数据管理``keywords: IndexedDB,归档,清理窗口,生命周期,配额``description: 设计按时间与生命周期的归档与清理窗口,将冷数据转移或压缩,保留关键索引,降低配额压力并维持查询性能。`窗口与策略活跃窗口保留完整数据;冷数据归档精简字段或压缩为 Blob;记录最后访问时间。实现示例async function archiveOld(db, beforeTs) { return new Promise((resolve, reject) => { const tx = db.transaction('items', 'readwrite'); const s = tx.objectStore('items'); const idx = s.index('byCreatedAt'); const r = IDBKeyRange.upperBound(beforeTs); idx.openCursor(r).onsuccess = e => { const c = e.target.result; if (!c) return; const v = c.value; s.put({ id: v.id, createdAt: v.createdAt, archive: true }); c.continue(); }; tx.oncomplete = () => resolve(); tx.onerror = () => reject(tx.error); }); }

发表评论 取消回复