/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk

« back to all changes in this revision

Viewing changes to classes/database.php

  • Committer: Suren A. Chilingaryan
  • Date: 2020-02-25 02:51:25 UTC
  • Revision ID: csa@suren.me-20200225025125-opezzcpks904243z
Cache globaly response of 'SHOW DATABASES' to avoid overloading the source databases seriving many ADEI servers

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 }
39
39
}
40
40
 
 
41
class DBCache {
 
42
  var $cache;
 
43
  public function __construct() {
 
44
    $this->cache = array();
 
45
  }
 
46
  
 
47
  public function Add($cat, $key, &$value) {
 
48
    if (!is_array($this->cache[$cat]))
 
49
        $this->cache[$cat] = array();
 
50
    $this->cache[$cat][$key] = $value;
 
51
  }
 
52
  
 
53
  public function Get($cat, $key, $default = false) {
 
54
    if (isset($this->cache[$cat][$key]))
 
55
        return $this->cache[$cat][$key];
 
56
    
 
57
    return $default;
 
58
  }
 
59
}
 
60
 
 
61
global $global_database_cache;
 
62
$global_database_cache = new DBCache();
 
63
 
41
64
class DATABASE {
42
65
 var $dbh, $connected;
43
66
 var $driver;
45
68
 
46
69
 var $server;
47
70
 var $dbname;
 
71
 var $connection; // string uniquely identifying connection
48
72
 
49
73
 var $text_quote = '\'';
50
74
 var $col_quote_l = "\"";
69
93
    if ($server['sqldrv']) $this->driver = $server['sqldrv'];
70
94
    else $this->driver = $server['driver'];
71
95
 
 
96
    switch($server['driver']) {
 
97
      case "odbc":
 
98
        if ($server['source'])
 
99
            $this->connection = "${server['driver']}:driver=${server['source']};uid=${server['user']}";
 
100
        else
 
101
            $this->connection = "${server['driver']}:driver=${server['subdrv']};host=${server['host']}" . ($server['port']?(";port=" . $server['port']):"") . ";uid=${server['user']}";
 
102
        break;
 
103
      default:
 
104
        $this->connection = "${server['driver']}:host=${server['host']}" . ($server['port']?(";port=" . $server['port']):"") . ";uid=" . $server['user'];
 
105
    }
 
106
 
72
107
    // Connect on first query to avoid repetitions...
73
108
    //$this->ReConnect();
74
109
 }
92
127
 
93
128
     try {
94
129
        $this->odbc = false;
95
 
        
 
130
 
 
131
            // This is default and may be overriden in specific cases (e.g. ODBC)
96
132
        switch($server['driver']) {
97
133
          case "odbc":
98
134
            if ($server['source']) {
147
183
                    $this->dbh = new PDO ("odbc:DRIVER=" . $server['subdrv'] . ";SERVER=" . $server['host'] . ";PORT=" . ($server['port']?$server['port']:"1433") . $dbtext . ";PROTOCOL=TCPIP;UID=" . $server['user'] . ";PWD=" . $server['password'], NULL, NULL, $pdo_opts);
148
184
                }
149
185
            }
150
 
 
151
186
            $this->odbc = true;
152
187
          break;
153
188
          default:      
471
506
 }
472
507
 
473
508
 function ShowDatabases() {
 
509
    global $global_database_cache;
 
510
 
 
511
        // This intended to speedup large multi-source setups where multiple ADEI servers referring to the same slow database
 
512
    $list = $global_database_cache->Get("databases", $this->connection);
 
513
    if ($list !== false) return $list;
 
514
    
474
515
    switch ($this->driver) {
475
516
        case "mysql":
476
 
            return $this->Query("SHOW DATABASES", DATABASE::GLOBAL_QUERY|DATABASE::FETCH_NUM);
 
517
            $list = $this->Query("SHOW DATABASES", DATABASE::GLOBAL_QUERY|DATABASE::FETCH_NUM);
 
518
            break;
477
519
        case "mssql":
478
 
            return $this->Query("SELECT name FROM master..sysdatabases", DATABASE::GLOBAL_QUERY|DATABASE::FETCH_NUM);
 
520
            $list = $this->Query("SELECT name FROM master..sysdatabases", DATABASE::GLOBAL_QUERY|DATABASE::FETCH_NUM);
 
521
            break;
479
522
        default:
480
523
            throw new ADEIException(translate("The ShowDatabases for \"%s\" is not implemented", $this->driver));
481
524
    }
 
525
    
 
526
    $global_database_cache->Add("databases", $this->connection, $list);
 
527
    return $list;
482
528
 } 
483
529
 
484
530
 function ShowTables() {