Tweaks to how doatasource manageers cache connections
Tweaks to how doatasource manageers cache connections
diff --git a/src/library/LRUCache.php b/src/library/LRUCache.php
index bc0077556..10a42c281 100644
--- a/src/library/LRUCache.php
+++ b/src/library/LRUCache.php
@@ -49,12 +49,17 @@ class LRUCache
* @var int
*/
private $size;
+ /**
+ * Default size of LRU Cache
+ * @var int
+ */
+ const DEFAULT_SIZE = 200;
/**
* Creates an empty cache and sets the size
*
* @param int $size size of the cache
*/
- public function __construct($size = 100)
+ public function __construct($size = self::DEFAULT_SIZE)
{
$this->cache = [];
$this->size = $size;
@@ -107,4 +112,4 @@ class LRUCache
{
return $this->cache;
}
-}
\ No newline at end of file
+}
diff --git a/src/library/media_jobs/RecommendationJob.php b/src/library/media_jobs/RecommendationJob.php
index 1ebd33070..4c060f141 100644
--- a/src/library/media_jobs/RecommendationJob.php
+++ b/src/library/media_jobs/RecommendationJob.php
@@ -947,7 +947,7 @@ class RecommendationJob extends MediaJob
$this->lru_cache->put($row['ID'],
base64_decode($row['VECTOR'], true));
} else {
- var_dump($row['VECTOR']);
+ var_dump($row);
}
}
$context_distance_sum = (self::CONTEXT_WINDOW_LENGTH *
diff --git a/src/models/datasources/MysqlManager.php b/src/models/datasources/MysqlManager.php
index e34eedfcb..ab03e5493 100755
--- a/src/models/datasources/MysqlManager.php
+++ b/src/models/datasources/MysqlManager.php
@@ -93,7 +93,6 @@ class MysqlManager extends PdoManager
$this->pdo = new \PDO("mysql:host={$db_host}".
$db_port_string . $db_name_string,
$db_user, $db_password);
- self::$active_connections[$this->connect_string] = $this->pdo;
} catch (\PDOException $e) {
$this->pdo = false;
L\crawlLog("MysqlManager.php Connection failed: \n" .
@@ -104,6 +103,7 @@ class MysqlManager extends PdoManager
} else {
$this->pdo = self::$active_connections[$this->connect_string];
}
+ $this->updateConnectionCache();
$this->to_upper_dbms = "MYSQL";
return $this->pdo;
}
diff --git a/src/models/datasources/PdoManager.php b/src/models/datasources/PdoManager.php
index daf3d7aa3..4152ca4b5 100644
--- a/src/models/datasources/PdoManager.php
+++ b/src/models/datasources/PdoManager.php
@@ -99,6 +99,11 @@ class PdoManager extends DatasourceManager
* @var array
*/
public static $active_connections = [];
+ /**
+ * Maximum number of connections to cache
+ * @var int
+ */
+ const MAX_CONNECTIONS = 20;
/**
* {@inheritDoc}
*
@@ -125,7 +130,6 @@ class PdoManager extends DatasourceManager
try {
$this->pdo = new \PDO($db_host, $db_user, $db_password,
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
- self::$active_connections[$this->connect_string] = $this->pdo;
} catch (\PDOException $e) {
$this->pdo = false;
L\crawlLog("PDOManager.php Connection failed: \n" .
@@ -135,12 +139,27 @@ class PdoManager extends DatasourceManager
} else {
$this->pdo = self::$active_connections[$this->connect_string];
}
+ $this->updateConnectionCache();
$this->to_upper_dbms = false;
if (stristr($db_host, 'PGSQL')) {
$this->to_upper_dbms = 'PGSQL';
}
return $this->pdo;
}
+ /**
+ * Caches current PDO connection. If cache full evicts last
+ */
+ public function updateConnectionCache()
+ {
+ unset(self::$active_connections[$this->connect_string]);
+ self::$active_connections = [$this->connect_string => $this->pdo] +
+ self::$active_connections;
+ if (count(self::$active_connections) >= self::MAX_CONNECTIONS) {
+ $oldest_connection = array_key_last(self::$active_connections);
+ L\crawlLog("Evicting old db connection: $oldest_connection");
+ unset(self::$active_connections[$oldest_connection]);
+ }
+ }
/** {@inheritDoc} */
public function disconnect()
{
diff --git a/src/models/datasources/Sqlite3Manager.php b/src/models/datasources/Sqlite3Manager.php
index c94237b11..cae3eafad 100644
--- a/src/models/datasources/Sqlite3Manager.php
+++ b/src/models/datasources/Sqlite3Manager.php
@@ -86,12 +86,11 @@ class Sqlite3Manager extends PdoManager
$this->connect_time = time();
$this->connect_string = "$db_host:$db_user:$db_password:$db_name";
if (empty(self::$active_connections[$this->connect_string])) {
- self::$active_connections[$this->connect_string] =
- new \PDO($connect);
- }
- if (empty($this->pdo)) {
+ $this->pdo = new \PDO($connect);
+ } else {
$this->pdo = self::$active_connections[$this->connect_string];
}
+ $this->updateConnectionCache();
$this->to_upper_dbms = "SQLITE3";
return $this->pdo;
}