<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/24
 * Time: 17:40
 */

namespace app\logic;

use  app\pplus\Instance;
use Illuminate\Database\Capsule\Manager as Capsule;
use app\lib\GlobConfigs;

class MyPgsql
{
    use Instance;
    private $pgsql = null;
    private $ctime = 0;

    public function getDb()
    {
        if ((time() - $this->ctime) <= 60 * 5) {
            if ($this->pgsql) {
                return $this->pgsql;
            }
        }

        if ($this->pgsql) {
            return $this->pgsql;
        }
        return $this->Init();
    }

    public function Init()
    {
        echo "MyPgsql--Init" . "\n";
        $conf = GlobConfigs::getKey('pgsql');
        $pgsql = new Capsule();
        $pgsql->addConnection($conf);
        $pgsql->setAsGlobal();
        $pgsql->bootEloquent();
        $this->pgsql = $pgsql;
        $this->ctime = time();
        return $pgsql;
    }


}