admin管理员组文章数量:1279008
My Chrome extension stores some data using chrome.storage
, & fetches the results from the storage as needed.
However, I realize now that I need to periodically need to refresh the data in chrome.storage
- which means clearing it from time to time.
Is it possible to:
Have Chrome treat
chrome.storage
as Session storage - that is - it clears the data once the browser closes?If above is not possible, is
chrome.storage.remove
the only way to clear storage?What happens when the
chrome.storage
space hits its limit?
My Chrome extension stores some data using chrome.storage
, & fetches the results from the storage as needed.
However, I realize now that I need to periodically need to refresh the data in chrome.storage
- which means clearing it from time to time.
Is it possible to:
Have Chrome treat
chrome.storage
as Session storage - that is - it clears the data once the browser closes?If above is not possible, is
chrome.storage.remove
the only way to clear storage?What happens when the
chrome.storage
space hits its limit?
1 Answer
Reset to default 13Store
Date.now()
value with each object or alongside using a prefixed/suffixed key.
A separate key might be better if the objects are big.- Periodically check all items and remove the expired ones i.e. where the stored date is less than current
Date.now() - CACHE_DURATION_IN_MS
- Check the expiry date when reading, and re-fetch from the web (or elsewhere) if needed
- Periodically check all items and remove the expired ones i.e. where the stored date is less than current
remove()
andclear()
are the only methods to delete from chrome.storage5,242,880 bytes: the maximum amount (in bytes) of data that can be stored in local storage, as measured by the JSON stringification of every value plus every key's length. This value will be ignored if the extension has the unlimitedStorage permission. Updates that would cause this limit to be exceeded fail immediately and set runtime.lastError.
Note:
chrome.runtime.lastError
is undefined when no error occurred.As you can see above, to ignore the default 5MB limit use "unlimitedStorage" permission.
Primitive example (a better one would use Promise):
function save(data, callback, retrying) {
Object.keys(data).forEach(k => data[k].savedOn = Date.now());
chrome.storage.local.set(data, () => {
if (!chrome.runtime.lastError)
return callback();
if (retrying)
throw chrome.runtime.lastError.message;
cleanup(() => save(data, callback, true));
});
}
const CACHE_DURATION = 7 * 24 * 3600 * 1000; // 7 days
function cleanup(callback) {
var cutoff = Date.now() - CACHE_DURATION;
chrome.storage.local.get(null, all => {
var expiredKeys = Object.keys(all).filter(k => all[k].savedOn < cutoff);
chrome.storage.local.remove(expiredKeys, callback);
});
}
本文标签: javascriptHow to automatically clear chromestorageStack Overflow
版权声明:本文标题:javascript - How to automatically clear chrome.storage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741295768a2370808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论