gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:08 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com