使用 superagent-cache 为不同的参数缓存 HTTP 响应
Caching HTTP responses using superagent-cache for different params
我正在使用 superagent-cache 在 Node Express 服务器上缓存响应。
所以请求是这样的:
myServer.com/api/posts?id=21
而且缓存工作得很好。一旦 Node 从 API 服务器获取响应,它会将相同的数据传递给所有访问用户,而不是一次又一次地调用 API。
现在的问题是我想将用户 ID 添加到请求中。所以新的请求变成:
myServer.com/api/posts?id=21&userId=123
现在缓存变成了用户特定的。 Superagent 将它作为一个单独的请求,每次为每个用户获取它。这是多余的。有什么办法可以告诉 superagent 避免 userId
参数?
我是 superagent-cache
的作者。很高兴你喜欢它!
您可以使用 .pruneQuery
chainable 执行带有给定参数的查询,但在不使用该参数作为生成键的一部分的情况下缓存它。
您的新代码应如下所示:
superagent
.get('myServer.com/api/posts')
.query({id: id, userId: userId})
.pruneQuery(['userId'])
.end(function (error, response){
// handle response
}
);
我正在使用 superagent-cache 在 Node Express 服务器上缓存响应。 所以请求是这样的:
myServer.com/api/posts?id=21
而且缓存工作得很好。一旦 Node 从 API 服务器获取响应,它会将相同的数据传递给所有访问用户,而不是一次又一次地调用 API。
现在的问题是我想将用户 ID 添加到请求中。所以新的请求变成:
myServer.com/api/posts?id=21&userId=123
现在缓存变成了用户特定的。 Superagent 将它作为一个单独的请求,每次为每个用户获取它。这是多余的。有什么办法可以告诉 superagent 避免 userId
参数?
我是 superagent-cache
的作者。很高兴你喜欢它!
您可以使用 .pruneQuery
chainable 执行带有给定参数的查询,但在不使用该参数作为生成键的一部分的情况下缓存它。
您的新代码应如下所示:
superagent
.get('myServer.com/api/posts')
.query({id: id, userId: userId})
.pruneQuery(['userId'])
.end(function (error, response){
// handle response
}
);