首先,我想说我是快乐的新手,所以不要苛刻地评判我。
我正在按照本教程尝试使用catbox
和catbox-redis
npm
包基于 Redis 设置服务器端缓存,但出现以下错误:
{
reason: Error: Connection is closed.
at Redis.connectionCloseHandler (/home/yuriy/dev/sources/all/hapi-getting-started/node_modules/ioredis/built/redis/index.js:305:24)
at Object.onceWrapper (events.js:300:26)
at Redis.emit (events.js:210:5)
at processTicksAndRejections (internal/process/task_queues.js:75:11)
}
如您所见,它说错误存在于ioredis
(根据package-lock.json的v4.14.1(包中,该包是catbox-redis
的依赖项。
我在本地运行 Redis 服务器。
username@my-computer:~$ redis-cli -v
redis-cli 4.0.9
username@my-computer:~$ redis-cli
127.0.0.1:6379> ping
PONG
这是我的package.json
:
{
"name": "hapi-getting-started",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"build": "rimraf dist && tsc",
"start": "rimraf dist && tsc && node dist/index.js",
"dev": "tsc -w | nodemon dist/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@hapi/catbox": "^10.2.3",
"@hapi/catbox-redis": "^5.0.5",
"@hapi/hapi": "^18.4.0",
"rimraf": "^3.0.0",
"typescript": "^3.7.2"
},
"devDependencies": {
"@types/hapi__catbox": "^10.2.2",
"@types/hapi__catbox-redis": "^5.0.0",
"@types/hapi__hapi": "^18.2.6",
"@types/node": "^12.12.14",
"nodemon": "^2.0.1"
}
}
这是我的src/index.ts
:
const Hapi = require('@hapi/hapi');
const CatboxRedis = require('@hapi/catbox-redis');
console.log(`Running environment ${process.env.NODE_ENV || 'dev'}`);
// Catch uncaught exceptions
process.on('uncaughtException', (error: Error) => {
console.error(`uncaughtException ${error.message}`);
console.error({ reason });
});
// Catch unhandled rejected promises
process.on('unhandledRejection', (reason: any) => {
console.error(`unhandledRejection ${reason}`);
console.error({ error });
});
const init = async () => {
const server = Hapi.server({
host: 'localhost',
port: 8000,
cache: {
name: 'redis-cache',
provider: {
constructor: CatboxRedis,
options: {
partition: 'my_cached_data',
tls: {},
},
},
},
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
init();
知道我做错了什么吗?我在这个问题上花了很多时间,所以任何帮助将不胜感激。
好的,似乎在快乐的官方文档中存在缓存问题(服务器端缓存部分(。解决方案非常简单,但并不明显:我刚刚删除了tls: {},
。
const server = Hapi.server({
host: 'localhost',
port: 8000,
cache: {
name: 'redis-cache',
provider: {
constructor: CatboxRedis,
options: {
partition: 'my_cached_data',
// tls: {}, <-- Here is a problem, remove this line
},
},
},
});
这是ioredis
配置参数。从catbox-redis
文档中:
tls
- 表示ioredis的 TLS 配置选项的对象。
您可以在ioredis
文档中找到更多详细信息。