<?php

namespace app\common;

use think\facade\Config;
use think\cache\driver\Redis as TpRedis;

/**
 * Description of Redis
 *
 * @author sgq
 */
class Redis extends TpRedis {

    protected $db;
    protected static $instance = [];

    public function __construct($db) {
        $options = Config::get("cache.stores.redis");
        $this->db = $db;
        $options["select"] = $db;
        $this->options = array_merge($this->options, $options);
        parent::__construct();
    }

    public static function instance($db = 0) {
        if (!isset(self::$instance[$db])) {
            self::$instance[$db] = new self($db);
        }
        return self::$instance[$db];
    }

    public function __destruct() {
        self::$instance[$this->db]->close();
        unset(self::$instance[$this->db]);
    }

}
