如何在Chrome Storage API中有效地将项目添加到阵列?

时间:2022-06-24 17:14:38

From what I understand, if you want to have an array stored in the Chrome Storage API to which you want to continually add items, you need something like this:

据我所知,如果您想在Chrome Storage API中存储要继续添加项目的数组,则需要以下内容:

function addToHistory(url) {
    chrome.storage.sync.get('history', function(obj) {
        var history = obj.hasOwnProperty('history') ? obj.history : [];
        history.push(url);
        chrome.storage.sync.set({'history': history}, function() {
            if (chrome.runtime.lastError)
                console.log(chrome.runtime.lastError);
            else
                console.log("History saved successfully");
        });
    });
}

This code bothers me; loading and then saving the same array every time you push a single item onto the end is horribly inefficient (especially if your history array starts getting several thousand entries).

这段代码困扰我;每次将单个项目推送到末尾时加载然后保存相同的数组是非常低效的(特别是如果您的历史数组开始获得数千个条目)。

Is there a more efficient way of doing this? I'm assuming I'm not the first to want to push to an array, so is there already a recommended way of achieving this?

有没有更有效的方法呢?我假设我不是第一个想要推送到阵列的人,所以有没有推荐的实现方法?

1 个解决方案

#1


0  

I don't think that chrome.storage.sync API is ideal for what you need. Basically this API is great for remembering user preferences or simple and short data. Sync API have limitations for usage like: - 102.4 KB for all data kept in the storage - 8 KB of data per item - 1,800 writes per hour

我不认为chrome.storage.sync API非常适合您的需求。基本上,这个API非常适合记住用户首选项或简单和简短的数据。 Sync API具有以下用途的限制: - 存储中保留的所有数据的102.4 KB - 每个项目8 KB的数据 - 每小时1,800次写入

So if you planning to use this API to store some historical data the app may exceed limits very fast.

因此,如果您计划使用此API存储一些历史数据,应用程序可能会非常快地超出限制。

I'm assuming you are developing an extension, not the app. In app you have access to the chrome.syncFileSystem API which may be used to save syncable data in a file.

我假设你正在开发一个扩展,而不是应用程序。在应用程序中,您可以访问chrome.syncFileSystem API,该API可用于在文件中保存可同步数据。

Answering your question there's no one way to optimize your function. You can try to store the data periodically - for example every 30 seconds or so. You just need to remember to save data after a user closes the app.

回答你的问题,没有一种方法可以优化你的功能。您可以尝试定期存储数据 - 例如每30秒左右。您只需要记住在用户关闭应用程序后保存数据。

You can also store this value as a variable in memory and save it when the user leave the screen or close the app but it is dangerous because the app may close before asynchronous task complete.

您还可以将此值存储为内存中的变量,并在用户离开屏幕或关闭应用程序时保存它,但这很危险,因为应用程序可能会在异步任务完成之前关闭。

Anyway I think that this API is not the best solution for your app.

无论如何,我认为这个API不是您的应用程序的最佳解决方案。

#1


0  

I don't think that chrome.storage.sync API is ideal for what you need. Basically this API is great for remembering user preferences or simple and short data. Sync API have limitations for usage like: - 102.4 KB for all data kept in the storage - 8 KB of data per item - 1,800 writes per hour

我不认为chrome.storage.sync API非常适合您的需求。基本上,这个API非常适合记住用户首选项或简单和简短的数据。 Sync API具有以下用途的限制: - 存储中保留的所有数据的102.4 KB - 每个项目8 KB的数据 - 每小时1,800次写入

So if you planning to use this API to store some historical data the app may exceed limits very fast.

因此,如果您计划使用此API存储一些历史数据,应用程序可能会非常快地超出限制。

I'm assuming you are developing an extension, not the app. In app you have access to the chrome.syncFileSystem API which may be used to save syncable data in a file.

我假设你正在开发一个扩展,而不是应用程序。在应用程序中,您可以访问chrome.syncFileSystem API,该API可用于在文件中保存可同步数据。

Answering your question there's no one way to optimize your function. You can try to store the data periodically - for example every 30 seconds or so. You just need to remember to save data after a user closes the app.

回答你的问题,没有一种方法可以优化你的功能。您可以尝试定期存储数据 - 例如每30秒左右。您只需要记住在用户关闭应用程序后保存数据。

You can also store this value as a variable in memory and save it when the user leave the screen or close the app but it is dangerous because the app may close before asynchronous task complete.

您还可以将此值存储为内存中的变量,并在用户离开屏幕或关闭应用程序时保存它,但这很危险,因为应用程序可能会在异步任务完成之前关闭。

Anyway I think that this API is not the best solution for your app.

无论如何,我认为这个API不是您的应用程序的最佳解决方案。