You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/support/redis/Redis.ts

76 lines
2.0 KiB

import {Inject, Singleton} from '../../di'
import {Config} from '../../service/Config'
import * as IORedis from 'ioredis'
import {RedisOptions} from 'ioredis'
import {Logging} from '../../service/Logging'
import {Unit} from '../../lifecycle/Unit'
import {AsyncPipe} from '../../util'
export {RedisOptions} from 'ioredis'
/**
* Unit that loads configuration for and manages instantiation
* of an IORedis connection.
*/
@Singleton()
export class Redis extends Unit {
/** The config service. */
@Inject()
protected readonly config!: Config
/** The loggers. */
@Inject()
protected readonly logging!: Logging
/**
* The instantiated connection, if one exists.
* @private
*/
private connection?: IORedis.Redis
async up(): Promise<void> {
this.logging.info('Attempting initial connection to Redis...')
this.logging.debug('Config:')
this.logging.debug(Config)
this.logging.debug(this.config)
await this.getConnection()
}
async down(): Promise<void> {
this.logging.info('Disconnecting Redis...')
if ( this.connection?.status === 'ready' ) {
await this.connection.disconnect()
}
}
/**
* Get the IORedis connection instance.
*/
public async getConnection(): Promise<IORedis.Redis> {
if ( !this.connection ) {
const options = this.config.get('redis.connection') as RedisOptions
this.logging.verbose(options)
this.connection = new IORedis(options)
}
return this.connection
}
/**
* Get the IORedis connection in an AsyncPipe.
*/
public pipe(): AsyncPipe<IORedis.Redis> {
return new AsyncPipe<IORedis.Redis>(() => this.getConnection())
}
/**
* Get an IORedis.Pipeline instance in an AsyncPipe.
*/
public multi(): AsyncPipe<IORedis.Pipeline> {
return this.pipe()
.tap(redis => {
return redis.multi()
})
}
}