import * as Knex from 'knex'
import { Singleton, Container, Scope, Inject } from "typescript-ioc";
import { Value, logger } from "mslib"
import { RedisClient } from '../refs/redis-tool'

/**
 * 
 * @date 2021-12-06 17:03:45
 * @author 吴锋霖
 */
@Singleton
class RedisClientFactory {

    @Value('redis.host')
    host: string
    @Value('redis.port')
    port: number
    @Value('redis.database')
    database: number
    @Value('redis.password')
    password: string

    private redisClient: RedisClient

    async init() {
        if (this.redisClient) {
            return;
        }

        logger.info('准备初始化Redis---------->host:%j,database:%j', this.host, this.database)
        this.redisClient = new RedisClient(this.host, this.port, this.password, this.database)

        Container.bind(RedisClient).factory(() => {
            let factory = Container.get(RedisClientFactory);
            return factory.get();
        }).scope(Scope.Singleton);
        logger.info('初始化Redis完成---------->host:%j,database:%j', this.host, this.database)
    }

    get(): RedisClient {
        return this.redisClient;
    }

}

export { RedisClientFactory,RedisClient }