我正在尝试使用nodejs作为运行时从AWS SAM应用程序访问Redis缓存。 Redis服务器正在运行,我可以使用终端进行连接。 当sam应用程序尝试访问Redis时,它显示以下错误消息
error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
我试图在redis配置文件/usr/local/etc/redis.conf
中将bind选项更改为0.0.0.0,protectedMode更改为no,但是仍然面临相同的问题。
import redis, {RedisClient} from 'redis';
export default class LocalCache {
private client: RedisClient;
constructor(host?: string, port?: number ) {
this.client = redis.createClient(6379, '127.0.0.1');
this.client.on('connect', function() {
console.info('Redis client connected');
});
}
public close() {
return this.client.quit();
}
public read(key: string): Promise<string | null> {
return new Promise ((resolve, reject) => {
this.client.get(key, (err, res) => {
if(err){
reject(err);
}
resolve(res);
});
});
}
public write(key: string, value: string, time?: number | string,
expiryMode?: 'ex' | 'px'): Promise<string|null> {
return new Promise ((resolve, reject) => {
if (time) {
expiryMode = expiryMode || 'ex';
this.client.set(key, value, expiryMode, 30)
resolve('success');
}
this.client.set(key, value);
resolve('success');
});
}
}
当我在本地运行SAM应用程序时,我希望sam应用程序应该能够访问本地Redis,并允许我在缓存中获取和设置值。