node.js - 使用 catbox 和 hapi js 进行内存缓存

标签 node.js caching hapi.js

我正在使用 catbox 在 hapijs 中进行内存缓存,在这些场景中应该向数据库发出请求以获取所有行

  • 请求的键不在缓存的 db_result 行中,然后调用 DB 并更新缓存并从缓存对象返回值
  • 有一个对缓存 db_result 行中的键的请求,返回该键的值

例如:如果缓存的 db_result[{ id: 12, name: 'app4' },{ id: 21, name: 'app5' }] 并且key12,不应该调用 DB,否则如果 key 是 13 那么应该进行 DB 调用并且 db_result 应该得到更新。

有没有关于如何配置此功能的示例。我是否正确遵循了指南?

请注意,我们在 hapi 之上使用胶水进行服务器配置。

最佳答案

有一个例子on Github使用 Cache-mongodb 但 API 与任何缓存提供程序一致。

// wildcard route that responds all requests
// either with data from cache or default string
server.route({
  method: 'GET',
  path: '/{path*}',
  handler: async (request, h) => {

      const key = {
          segment: 'examples',
          id: 'myExample'
      };

      // get item from cache segment
      const cached = await Cache.get(key);

      if (cached) {
          return `From cache: ${cached.item}`;
      }

      // fill cache with item
      await Cache.set(key, { item: 'my example' }, 5000);

      return 'my example';
  }
});

关于node.js - 使用 catbox 和 hapi js 进行内存缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43499249/

相关文章:

javascript - Hapi.js 和 Bookshelf.js 验证问题

IIS ASP缓存

php - Laravel - Blade View 中的重复查询 - 减少查询数量

javascript - 如何直接访问 Hapi (Node.js) 服务器上的文件?

node.js - 在带有 npm 的 Lambda 函数中使用 AWS Cognito

android picasso 无效不工作

javascript - 从另一个路由调用一个 hapi 路由

javascript - WebSocket 未连接到 socket.io

javascript - 检查 Discord 服务器中是否存在用户 ID

javascript - 在 Node.js 中提供静态文件 - 500 内部服务器错误