gpt4 book ai didi

redis - 在redis-brain中用新的redis服务器替换本地redis服务器

转载 作者:可可西里 更新时间:2023-11-01 11:14:39 28 4
gpt4 key购买 nike

我目前正在尝试使用 Hubot 构建一个 slack 机器人,我需要在 Redis 上保留一些东西。我正在使用来自 https://github.com/hubotio/hubot-redis-brain 的以下脚本

'use strict'

// Description:
// Persist hubot's brain to redis
//
// Configuration:
// REDISTOGO_URL or REDISCLOUD_URL or BOXEN_REDIS_URL or REDIS_URL.
// URL format: redis://<host>:<port>[/<brain_prefix>]
// URL format (UNIX socket): redis://<socketpath>[?<brain_prefix>]
// If not provided, '<brain_prefix>' will default to 'hubot'.
// REDIS_NO_CHECK - set this to avoid ready check (for exampel when using Twemproxy)
//
// Commands:
// None

const Url = require('url')
const Redis = require('redis')

module.exports = function (robot) {
let client, prefix
const redisUrlEnv = getRedisEnv()
const redisUrl = process.env[redisUrlEnv] || 'redis://localhost:6379'

if (redisUrlEnv) {
robot.logger.info(`hubot-redis-brain: Discovered redis from ${redisUrlEnv} environment variable`)
} else {
robot.logger.info('hubot-redis-brain: Using default redis on localhost:6379')
}

if (process.env.REDIS_NO_CHECK) {
robot.logger.info('Turning off redis ready checks')
}

const info = Url.parse(redisUrl)

if (info.hostname === '') {
client = Redis.createClient(info.pathname)
prefix = (info.query ? info.query.toString() : undefined) || 'hubot'
} else {
client = (info.auth || process.env.REDIS_NO_CHECK)
? Redis.createClient(info.port, info.hostname, {no_ready_check: true})
: Redis.createClient(info.port, info.hostname)
prefix = (info.path ? info.path.replace('/', '') : undefined) || 'hubot'
}

robot.brain.setAutoSave(false)

const getData = () =>
client.get(`${prefix}:storage`, function (err, reply) {
if (err) {
throw err
} else if (reply) {
robot.logger.info(`hubot-redis-brain: Data for ${prefix} brain retrieved from Redis`)
robot.brain.mergeData(JSON.parse(reply.toString()))
robot.brain.emit('connected')
} else {
robot.logger.info(`hubot-redis-brain: Initializing new data for ${prefix} brain`)
robot.brain.mergeData({})
robot.brain.emit('connected')
}

robot.brain.setAutoSave(true)
})

if (info.auth) {
client.auth(info.auth.split(':')[1], function (err) {
if (err) {
return robot.logger.error('hubot-redis-brain: Failed to authenticate to Redis')
}

robot.logger.info('hubot-redis-brain: Successfully authenticated to Redis')
getData()
})
}

client.on('error', function (err) {
if (/ECONNREFUSED/.test(err.message)) {

} else {
robot.logger.error(err.stack)
}
})

client.on('connect', function () {
robot.logger.debug('hubot-redis-brain: Successfully connected to Redis')
if (!info.auth) { getData() }
})

robot.brain.on('save', (data) => {
if (!data) {
data = {}
}
client.set(`${prefix}:storage`, JSON.stringify(data))
})

robot.brain.on('close', () => client.quit())
}

function getRedisEnv () {
if (process.env.REDISTOGO_URL) {
return 'REDISTOGO_URL'
}

if (process.env.REDISCLOUD_URL) {
return 'REDISCLOUD_URL'
}

if (process.env.BOXEN_REDIS_URL) {
return 'BOXEN_REDIS_URL'
}

if (process.env.REDIS_URL) {
return 'REDIS_URL'
}
}

我的问题是我不知道应该更改代码的哪一部分,以便脚本使用我的 Redis 服务器而不是本地 Redis 服务器。另外,我的 Redis 服务器有一个密码作为 url 的一部分,我也想知道把它放在哪里。谢谢。

最佳答案

似乎您可以不更改任何代码,而是将环境变量 REDIS_URL 设置为您的 Redis 实例的 URL。

关于redis - 在redis-brain中用新的redis服务器替换本地redis服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54912709/

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