Sets a maximum age (minimum one day) for script storage values, after which they are deleted
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/562172/1881822/Temporary-Script-Storage.js
通过此类的实例设置和访问的存储将仅在您在构造函数中设置的生命周期内存在,但至少必须是一天。默认值为 30 天。之后,当调用 sweepExpiredEntries() 时,它将过期并被删除,您可以使用 setInterval() 安排此调用。
此库对通过 GM 的 setValue() 直接设置的脚本存储没有任何影响。这样的存储将正常运行。
使用示例:
const persistentStorage = new TTLStorage();
await persistentStorage.ready();
setInterval(() => persistentStorage.sweepExpiredEntries(), 1000 * 60 * 60 * 24);
try {
await persistentStorage.set("key1", "hello"); // throws
const msg = persistentStorage.get("key1"); // fast memory read
await persistentStorage.delete("key1"); // throws
} catch {}
此外,任何使用此库的脚本都必须包含以下元数据:
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// @grant GM.listValues
// @grant GM.info
这是必要的,因为将每个库所需的内容放在其自己的元数据中是不够的。顶级脚本必须为其所有依赖项提供必要的 @require 和 @grant。