获取与第一个API获取相同大小的响应

huangapple go评论70阅读模式
英文:

Get same size response as from first API fetch

问题

所以首先开始,假设我有一个提供约 ~300kB 数据的 API。有时,这个 API 甚至在 56 秒内都没有响应(这不是我们的 API,因此我们无法在其中实现任何逻辑)。因此,我们考虑创建另一个在 NodeJS 环境中运行的 API。主要思想是定期从原始 API 中重新获取数据,并将信息存储在 REDIS 数据库中以获得更好的性能。这样,我们就不需要等待停滞的原始 API,并可以从我们的 API 中获取数据。

然而,从原始 API 到我们的 API 的第一次获取(从原始 API 到我们的 API)的数据量为约 300kB,但从我们的 API 到我们的项目的获取现在变成了 1.5MB

是否有可能将这些数字还原到原始大小?

英文:

So to first start, let's say I have an API which gives a ~300kB worth of data. Sometimes, the API is not responding for even 5 or 6 seconds (This is not our API, hence we cannot implement any logic inside it). Hence, we thought of creating another API which runs in NodeJS environment. The main idea is to refetch the data once in a while from the original API and store information inside REDIS database for better performance. In that way, we do not need to wait for stalled original API and fetch the data from our API.

However, the first fetch (from the original API to our API) is worth 300kB of data, but the fetch from our API to our project now becomes 1.5MB.

Is there any possibility to bring those number to the original size?

答案1

得分: 2

原始的API服务器可能在其响应上使用gzip或deflate进行某种压缩,而您的fetch将自动对其进行解压缩。

要在请求时获得类似大小的响应,您只需要在获取响应后自行进行压缩。您可以选择在每个请求上实时执行此操作,或将压缩后的响应存储在Redis中(可能是两者中更好的选项之一)。

如果您正在使用Express,您可以使用Express的compression中间件,只需添加以下行即可实时压缩:

app.use(compression())

这将自动压缩超过1KB的任何响应。

如果您希望缓存压缩版本到Redis,这是首选方法,因为它可以减少Redis实例的内存使用,并使请求略微更快,您可以使用zlib

const zlib = require('zlib');

const apiResponse = '...'; // 从API获取,将内容作为字符串获取

zlib.gzip(apiResponse, (err, buffer) => {
    const valueToSave = buffer.toString('utf8');
    // 这里是Redis逻辑。
})

每当您请求API时,您只需要附加Content-Encoding: gzip头部,以告知客户端如何解码响应。

英文:

The original API server is probably doing some sort of compression using gzip or deflate on its response, and your fetch will automatically be decompressing it.

To get a similar size when requesting it you just need to compress it yourself after fetching it. You can either do this on the fly for each request, or store the compressed response in Redis (Probably the better of the 2 options).

If you are using Express you can use Express's compression middleware, which will compress on the fly, by just adding the below line.

app.use(compression())

which will automatically compress any responses over 1kb.

If you want to cache the compressed version on the Redis, the preferable method since it reduces memory usage on the Redis instance and also makes the request slightly faster you can use zlib

const zlib = require('zlib');

const apiResponse = '...'; // fetch from API, get content as string

zlib.gzip(apiResponse, (err, buffer) => {
    const valueToSave = buffer.toString('utf8');
    // Redis logic here.
})

And whenever you make the request to your API all you need to do is append the Content-Encoding: gzip header to tell the client how to decode the response.

答案2

得分: 0

尝试在你的应用程序中启用压缩。在 ExpressJs 中,添加以下库:

npm i compression --save

现在在主文件中使用以下代码来启用它:

// app.js
// 导入库
const compression = require('compression');
const express = require('express');
const app = express();

// 以下行将压缩所有 HTTP 响应
app.use(compression());

app.get('/', (req, res) => {
  res.send("<Your bulky data>");
});

app.listen(3000, function () {
  console.log('Started on port 3000!');
});
英文:

Try enabling compression in your application. In ExpressJs, add below library:

npm i compression --save

Now to enable it in you app, use below in the main file:

// app.js
// Import the library
const compression = require(&#39;compression&#39;);
const express = require(&#39;express&#39;);
const app = express();

// Below line will compress all the HTTP responses
app.use(compression());

app.get(&#39;/&#39;, (req, res) =&gt; {
  res.send(&quot;&lt;Your bulky data&gt;&quot;);
});

app.listen(3000, function () {
  console.log(&#39;Started on port 3000!&#39;);
});

huangapple
  • 本文由 发表于 2023年7月3日 20:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定