Add support for jobs & queueables, migrations
- Create migration directives & migrators - Modify Cache classes to support array manipulation - Create Redis unit and RedisCache implementation - Create Queueable base class and Queue class that uses Cache backend
This commit is contained in:
75
src/support/redis/Redis.ts
Normal file
75
src/support/redis/Redis.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
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()
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user