bod*_*ydo 8 javascript caching google-chrome google-chrome-extension
我正在为我工作的图书馆编写Chrome扩展程序.该扩展程序每次打开时都会从库的API中获取最新的书名.
随着越来越多的人使用它,它会对发送API数据的库的服务器造成很大的负担.
在Chrome扩展程序中缓存数据的方法是什么?
例如,我想首次打开Chrome扩展程序时获取数据,然后将其保存(不知道在哪里?),并且仅在经过1小时后才向API请求并再次保存数据.
有人可以推荐一种方法在Chrome扩展程序中执行此操作吗?
Rud*_*die 10
对于本地存储,请使用chrome.storage.local.它有一个非常简单的API和每个配置文件5 MB的存储空间.
许可是"storage",它将授予您访问chrome.storage.local和chrome.storage.sync.local每个配置文件5 MB,保存在客户端上.sync是100 KB,保存在Google帐户中.相同的API.
我发现sync它不可靠,但你的需求似乎是local.
用法:
function fetchLive(callback) {
doSomething(function(data) {
chrome.storage.local.set({cache: data, cacheTime: Date.now()}, function() {
callback(data);
});
});
}
function fetch(callback) {
chrome.storage.local.get(['cache', 'cacheTime'], function(items) {
if (items.cache && items.cacheTime && items.cacheTime) {
if (items.cacheTime > Date.now() - 3600*1000) {
return callback(items.cache); // Serialization is auto, so nested objects are no problem
}
}
fetchLive(callback);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3779 次 |
| 最近记录: |