node.js - 使用服务器方法进行内存缓存中的 Hapi 不接受对象作为参数

标签 node.js caching methods server hapi.js

嗨,我目前正在nodejs上使用hapi框架开发反向代理服务器,有两个简单的任务:

  1. 将 API 发送到另一台服务器
  2. 根据任务 1 的响应,将数据与一些静态数据合并

任务 1 没有问题,所有内容都可以通过预先设置正确缓存。但是任务 2 就没那么幸运了,错误如下

Debug: internal, implementation, error server-0 Error: Invalid method key when invoking: replacer server-0 at Object.settings.cache.generateFunc.func [as replacer] (/home/jack/WebstormProjects/dcm-reverse-proxy/node_modules/hapi/lib/methods.js:145:51) server-0 at /home/jack/WebstormProjects/dcm-reverse-proxy/server.js:67:32 server-0 at /home/jack/WebstormProjects/dcm-reverse-proxy/node_modules/hapi/node_modules/catbox/node_modules/hoek/lib/index.js:850:22 server-0 at _combinedTickCallback (internal/process/next_tick.js:67:7) server-0 at process._tickDomainCallback (internal/process/next_tick.js:122:9)

我终于发现服务器方法注册只能接受字符串参数来生成缓存id,

//register
server.method('somemethod', proxyService.replacer, {
    cache:{
        cache: 'inMemCache',
        expiresIn: 5 * 60 * 1000,
        generateTimeout: 2000
    }
});

//calling, argument1 and argument2 must be {string}
server.methods.somemethod(argument1, argument2, function(_error, _result, cached, _report){
...
});

只是想知道以前是否有人经历过这种情况,如果我想以不同的格式传递 argument1 和 argument2 我该怎么办?

提前致谢。

最佳答案

我自己找到了答案,hapi服务器方法已经记录:

Generate a custom key

In addition to the above options, you may also pass a custom function used to generate a key based on the parameters passed to your method. If your method only accepts some combination of string, number, and boolean values hapi will generate a sane key for you. However, if your method accepts an object parameter, you should specify a function that will generate a key similar to the following. Note: Any arguments that are passed to your method are available to the generateKey method, but not the callback.

关于node.js - 使用服务器方法进行内存缓存中的 Hapi 不接受对象作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37384718/

相关文章:

javascript - 单击事件监听器仅适用于从 mongoDB 和 node.js 生成的第一个列表项

node.js - 使用 AWS 时,我应该在哪里上传我的应用程序文件?

java - 搜索引擎中的缓存管理

javascript - 如何用js重定向并在翻到上一页时发出警报?

c# - Main() 不想访问类变量

javascript - 如何让内部 eslint 插件在没有 eslint-plugin-* 前缀的情况下工作

javascript - 如何从主 node.js 脚本运行多个 node.js 脚本?

c++ - 在 C++ 中,如何让(嵌套的)比较仿函数引用封闭类的数据?

asp.net - ASP.NET 缓存的位置

java - java 是否有内置的 elementAt(stack s, int index) 方法?