53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { RedisClientDriver } from './driver'
|
|
import { event } from '../../event'
|
|
|
|
interface EasyRedisConfig {
|
|
host:string,
|
|
port:number,
|
|
db:number,
|
|
password:string
|
|
}
|
|
|
|
const config_cache = new Map<string,EasyRedisConfig>()
|
|
|
|
export function getConfig (prefix:string):EasyRedisConfig {
|
|
|
|
if(!config_cache.has(prefix)) {
|
|
|
|
const host = process.env[prefix + 'REDIS_HOST'] || ''
|
|
const port = process.env[prefix + 'REDIS_PORT'] || '6379'
|
|
const db = process.env[prefix + 'REDIS_DB'] || '0'
|
|
const password = process.env[prefix + 'REDIS_PWD'] || ''
|
|
|
|
const cache = {
|
|
host,
|
|
port:parseInt(port,10),
|
|
db:parseInt(db,10),
|
|
password
|
|
}
|
|
|
|
config_cache.set(prefix,cache)
|
|
return cache
|
|
}
|
|
|
|
return config_cache.get(prefix) as unknown as EasyRedisConfig
|
|
}
|
|
|
|
function init () {
|
|
const clients_connect = [{ name:'normal',prefix:'' }]
|
|
clients_connect.forEach(item=>{
|
|
let success_count = 0
|
|
const config = getConfig(item.prefix)
|
|
const client = new RedisClientDriver(item.name,config.host,config.port,config.password,config.db)
|
|
|
|
client.addListener('connect',()=>{
|
|
success_count++
|
|
if(success_count === clients_connect.length) {
|
|
event.emit('redis-all-connected')
|
|
}
|
|
})
|
|
})
|
|
|
|
}
|
|
|
|
init() |