MyPgsql.php 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/5/24
  6. * Time: 17:40
  7. */
  8. namespace app\logic;
  9. use app\pplus\Instance;
  10. use Illuminate\Database\Capsule\Manager as Capsule;
  11. use app\lib\GlobConfigs;
  12. class MyPgsql
  13. {
  14. use Instance;
  15. private $pgsql = null;
  16. private $ctime = 0;
  17. public function getDb()
  18. {
  19. if ((time() - $this->ctime) <= 60 * 5) {
  20. if ($this->pgsql) {
  21. return $this->pgsql;
  22. }
  23. }
  24. if ($this->pgsql) {
  25. return $this->pgsql;
  26. }
  27. return $this->Init();
  28. }
  29. public function Init()
  30. {
  31. echo "MyPgsql--Init" . "\n";
  32. $conf = GlobConfigs::getKey('pgsql');
  33. $pgsql = new Capsule();
  34. $pgsql->addConnection($conf);
  35. $pgsql->setAsGlobal();
  36. $pgsql->bootEloquent();
  37. $this->pgsql = $pgsql;
  38. $this->ctime = time();
  39. return $pgsql;
  40. }
  41. }