API.ai Node.js webhook 的缓存响应
Caching response for API.ai Node.js webhook
我在 Node.js 中为 API.ai 设计了一个 webhook,它与多个 API 交互以收集信息并响应用户。
因为我正在与多个 API 进行交互,所用的响应时间超过 5 秒,这导致 API.ai 请求超时。
为了克服这个问题,我正在尝试将缓存实现到 node.js webhook 中,它将 API 的响应保存到一定时间。这将删除超时,直到达到 max-age header 时间。
编辑:我可以用来缓存 API 子请求响应的最佳节点模块是什么。
注意:我对 http 请求使用 request node module,但它似乎没有提供缓存响应的方法。
所有给出的答案对于解决请求端的缓存问题都是合理的。但由于您指定了 API.AI 和操作,您可能还能够或需要在对话进行时存储信息。您可以使用 API.AI 上下文来执行此操作。
甚至可能是,如果您将其限制为每次用户响应仅进行一次远程调用,您也许能够在时间范围内完成它。
例如,如果您正在就电影时间和订票进行对话,对话可能如下所示:
User: "I want to see a movie."
[You use an API to lookup the nearest theater, store the theater's location in a context and reply] "Your nearest theater is the Mall Megaplex. Are you interested in one there?"
User: "Sure"
[You now already have the theater, so you query for what it is playing with another API call and store it in a context] "There are seven different movies playing, including Star Wars and Jaws. Do those sound interesting?"
User: "No"
[You already have the data in the context, so you don't need another call.] "How about Rocky or..."
通过这种方式,您进行了相同数量的调用(通常),但是在您进行时将用户的结果存储在会话中,而不是收集用户的所有信息或所有可能的结果,并且然后缩小范围。
最终决定使用以下模块:
https://www.npmjs.com/package/memory-cache
这更适合我的方案。等我有空的时候可能会尽快尝试使用Redis。
我在 Node.js 中为 API.ai 设计了一个 webhook,它与多个 API 交互以收集信息并响应用户。
因为我正在与多个 API 进行交互,所用的响应时间超过 5 秒,这导致 API.ai 请求超时。
为了克服这个问题,我正在尝试将缓存实现到 node.js webhook 中,它将 API 的响应保存到一定时间。这将删除超时,直到达到 max-age header 时间。
编辑:我可以用来缓存 API 子请求响应的最佳节点模块是什么。
注意:我对 http 请求使用 request node module,但它似乎没有提供缓存响应的方法。
所有给出的答案对于解决请求端的缓存问题都是合理的。但由于您指定了 API.AI 和操作,您可能还能够或需要在对话进行时存储信息。您可以使用 API.AI 上下文来执行此操作。
甚至可能是,如果您将其限制为每次用户响应仅进行一次远程调用,您也许能够在时间范围内完成它。
例如,如果您正在就电影时间和订票进行对话,对话可能如下所示:
User: "I want to see a movie."
[You use an API to lookup the nearest theater, store the theater's location in a context and reply] "Your nearest theater is the Mall Megaplex. Are you interested in one there?"
User: "Sure"
[You now already have the theater, so you query for what it is playing with another API call and store it in a context] "There are seven different movies playing, including Star Wars and Jaws. Do those sound interesting?"
User: "No"
[You already have the data in the context, so you don't need another call.] "How about Rocky or..."
通过这种方式,您进行了相同数量的调用(通常),但是在您进行时将用户的结果存储在会话中,而不是收集用户的所有信息或所有可能的结果,并且然后缩小范围。
最终决定使用以下模块:
https://www.npmjs.com/package/memory-cache
这更适合我的方案。等我有空的时候可能会尽快尝试使用Redis。