diff --git a/root/app/www/public/classes/Cache.php b/root/app/www/public/classes/Cache.php new file mode 100644 index 0000000..c5934a2 --- /dev/null +++ b/root/app/www/public/classes/Cache.php @@ -0,0 +1,69 @@ +init(); + } + + public function __tostring() + { + return 'Class loaded: Cache'; + } + + public function init() + { + $this->cache = new Memcached(); + $this->cache->addServer('127.0.0.1', '11211') or die('Cache connection failure'); + } + + public function set($key, $data, $seconds) + { + if (!$this->cache) { + $this->init(); + } + + if ($key && $data && $seconds) { + $this->cache->set($key, $data, $seconds); + } + } + + public function get($key) + { + if (!$this->cache) { + $this->init(); + } + + if (!$key) { + return; + } + + return $this->cache->get($key); + } + + public function bust($key) + { + if (!$this->cache) { + $this->init(); + } + + if (!$key) { + return; + } + + $this->cache->delete($key); + } +} diff --git a/root/app/www/public/classes/Database.php b/root/app/www/public/classes/Database.php index 1d700f4..4f04740 100644 --- a/root/app/www/public/classes/Database.php +++ b/root/app/www/public/classes/Database.php @@ -28,13 +28,16 @@ class Database public $notificationLinkTable; public $starr; public $shell; + public $cache; public function __construct($dbName) { + global $starr, $shell, $cache; $this->connect(DATABASE_PATH . $dbName); $this->dbName = $dbName; - $this->starr = new Starr(); - $this->shell = new Shell(); + $this->starr = $starr ?: new Starr(); + $this->shell = $shell ?: new Shell(); + $this->cache = $cache ?: new Cache(); } public function connect($dbFile) diff --git a/root/app/www/public/classes/Starr.php b/root/app/www/public/classes/Starr.php index 12a2f15..5051ffa 100644 --- a/root/app/www/public/classes/Starr.php +++ b/root/app/www/public/classes/Starr.php @@ -14,9 +14,13 @@ class Starr { use Overrides; + public $cache; + public function __construct() { + global $cache; + $this->cache = $cache ?: new Cache(); } public function __tostring() @@ -85,6 +89,13 @@ public function testConnection($app, $url, $apikey) public function getEndpoints($app) { + $cacheKey = sprintf(STARR_ENDPOINT_LIST_KEY, $app); + $cache = $this->cache->get($cacheKey); + + if ($cache) { + return json_decode($cache, true); + } + switch ($app) { case 'lidarr': $openapi = 'https://raw.githubusercontent.com/lidarr/Lidarr/develop/src/Lidarr.Api.V1/openapi.json'; @@ -142,6 +153,8 @@ public function getEndpoints($app) } } + $this->cache->set($cacheKey, json_encode($endpoints), STARR_ENDPOINT_LIST_TIME); + return $endpoints; } @@ -183,9 +196,9 @@ public function getAppFromProxiedKey($apikey, $truncated = false) { global $proxyDb, $appsTable, $starrsTable; - $proxyDb ??= new Database(PROXY_DATABASE_NAME); - $starrsTable ??= $proxyDb->getStarrsTable(); - $appsTable ??= $proxyDb->getAppsTable(); + $proxyDb = $proxyDb ?: new Database(PROXY_DATABASE_NAME); + $starrsTable = $starrsTable ?: $proxyDb->getStarrsTable(); + $appsTable = $appsTable ?: $proxyDb->getAppsTable(); $access = $starrAppDetails = $proxiedAppDetails = []; @@ -216,8 +229,8 @@ public function getAppFromStarrKey($apikey, $starrsTable) { global $proxyDb; - $proxyDb ??= new Database(PROXY_DATABASE_NAME); - $starrsTable ??= $proxyDb->getStarrsTable(); + $proxyDb = $proxyDb ?: new Database(PROXY_DATABASE_NAME); + $starrsTable = $starrsTable ?: $proxyDb->getStarrsTable(); foreach ($starrsTable as $starrApp) { if ($starrApp['apikey'] == $apikey) { diff --git a/root/app/www/public/classes/traits/Database/Apps.php b/root/app/www/public/classes/traits/Database/Apps.php index 3d06678..ce016da 100644 --- a/root/app/www/public/classes/traits/Database/Apps.php +++ b/root/app/www/public/classes/traits/Database/Apps.php @@ -13,12 +13,22 @@ public function getAppsTable() { $apps = []; - $q = "SELECT * - FROM " . APPS_TABLE . " - ORDER BY name ASC"; - $r = $this->query($q); - while ($row = $this->fetchAssoc($r)) { - $apps[] = $row; + $appsTableCache = $this->cache->get(APPS_TABLE_CACHE_KEY); + + if ($appsTableCache) { + $apps = json_decode($appsTableCache, true); + } + + if (empty($apps)) { + $q = "SELECT * + FROM " . APPS_TABLE . " + ORDER BY name ASC"; + $r = $this->query($q); + while ($row = $this->fetchAssoc($r)) { + $apps[] = $row; + } + + $this->cache->set(APPS_TABLE_CACHE_KEY, json_encode($apps), APPS_TABLE_CACHE_TIME); } return $apps; @@ -26,7 +36,7 @@ public function getAppsTable() public function getAppFromId($appId, $appsTable) { - $appsTable ??= $this->getAppsTable(); + $appsTable = $appsTable ?: $this->getAppsTable(); foreach ($appsTable as $app) { if ($app['id'] == $appId) { @@ -58,6 +68,8 @@ public function addApp($fields = []) return $this->error(); } + $this->cache->bust(APPS_TABLE_CACHE_KEY); + return; } @@ -81,6 +93,8 @@ public function updateApp($appId, $fields = []) return $this->error(); } + $this->cache->bust(APPS_TABLE_CACHE_KEY); + return; } @@ -94,6 +108,8 @@ public function deleteApp($appId) return $this->error(); } + $this->cache->bust(APPS_TABLE_CACHE_KEY); + return; } diff --git a/root/app/www/public/classes/traits/Database/Starrs.php b/root/app/www/public/classes/traits/Database/Starrs.php index d9bb911..bc37bcd 100644 --- a/root/app/www/public/classes/traits/Database/Starrs.php +++ b/root/app/www/public/classes/traits/Database/Starrs.php @@ -13,12 +13,22 @@ public function getStarrsTable() { $starrs = []; - $q = "SELECT * - FROM " . STARRS_TABLE . " - ORDER BY name ASC"; - $r = $this->query($q); - while ($row = $this->fetchAssoc($r)) { - $starrs[] = $row; + $starrsTableCache = $this->cache->get(STARRS_TABLE_CACHE_KEY); + + if ($starrsTableCache) { + $starrs = json_decode($starrsTableCache, true); + } + + if (empty($starrs)) { + $q = "SELECT * + FROM " . STARRS_TABLE . " + ORDER BY name ASC"; + $r = $this->query($q); + while ($row = $this->fetchAssoc($r)) { + $starrs[] = $row; + } + + $this->cache->set(STARRS_TABLE_CACHE_KEY, json_encode($starrs), STARRS_TABLE_CACHE_TIME); } return $starrs; @@ -26,7 +36,7 @@ public function getStarrsTable() public function getStarrAppFromId($id, $starrsTable) { - $starrsTable ??= $this->getStarrsTable(); + $starrsTable = $starrsTable ?: $this->getStarrsTable(); foreach ($starrsTable as $starrApp) { if ($starrApp['id'] == $id) { @@ -47,6 +57,8 @@ public function deleteStarrApp($starrId) return $this->error(); } + $this->cache->bust(STARRS_TABLE_CACHE_KEY); + return; } @@ -62,6 +74,8 @@ public function addStarrApp($starrApp, $fields = []) return $this->error(); } + $this->cache->bust(STARRS_TABLE_CACHE_KEY); + return; } @@ -76,6 +90,8 @@ public function updateStarrApp($id, $fields = []) return $this->error(); } + $this->cache->bust(STARRS_TABLE_CACHE_KEY); + return; } @@ -92,6 +108,8 @@ public function updateStarrAppSetting($id, $field, $value) return $this->error(); } + $this->cache->bust(STARRS_TABLE_CACHE_KEY); + return; } } diff --git a/root/app/www/public/functions/stats.php b/root/app/www/public/functions/stats.php index 27f16af..d83f48f 100644 --- a/root/app/www/public/functions/stats.php +++ b/root/app/www/public/functions/stats.php @@ -30,22 +30,23 @@ function getTotalEndpointStats($starrsTable, $appsTable) { global $starr; - $starr ??= new Starr(); - $stats = []; + $starr = $starr ?: new Starr(); + $stats = $endpointList = []; if ($starrsTable) { foreach ($starrsTable as $starrApp) { - $app = $starr->getStarrInterfaceNameFromId($starrApp['starr']); - $allowed = 0; - $total = 0; - $apps = 0; - $totalEndpoints = $starr->getEndpoints($app); + $app = $starr->getStarrInterfaceNameFromId($starrApp['starr']); + $allowed = $total = $apps = 0; + + if (!$endpointList[$app]) { + $endpointList[$app] = $starr->getEndpoints($app); + } foreach ($appsTable as $proxiedApp) { if ($proxiedApp['starr_id'] == $starrApp['id']) { $endpoints = json_decode($proxiedApp['endpoints'], true); $allowed += count($endpoints); - $total += count($totalEndpoints); + $total += count($endpointList[$app]); $apps++; } } diff --git a/root/app/www/public/includes/constants.php b/root/app/www/public/includes/constants.php index f223fa5..eda8cb7 100644 --- a/root/app/www/public/includes/constants.php +++ b/root/app/www/public/includes/constants.php @@ -44,3 +44,11 @@ define('MIGRATION_LOG', LOGS_PATH . 'system/migrations.log'); define('CRON_HOUSEKEEPER_LOG', LOGS_PATH . 'system/cron-housekeeper.log'); define('LOG_LINES_PER_PAGE', 1000); + +//-- CACHE +define('STARRS_TABLE_CACHE_KEY', 'starr_table'); +define('STARRS_TABLE_CACHE_TIME', 86400); //-- 1 DAY +define('APPS_TABLE_CACHE_KEY', 'apps_table'); +define('APPS_TABLE_CACHE_TIME', 86400); //-- 1 DAY +define('STARR_ENDPOINT_LIST_KEY', 'endpoints_%s'); //-- _starrApp +define('STARR_ENDPOINT_LIST_TIME', 604800); //-- 1 WEEK diff --git a/root/app/www/public/loader.php b/root/app/www/public/loader.php index fc256b6..4150c81 100644 --- a/root/app/www/public/loader.php +++ b/root/app/www/public/loader.php @@ -49,6 +49,9 @@ } } +//-- INITIALIZE THE CACHE CLASS +$cache = new Cache(); + //-- INITIALIZE THE SHELL CLASS $shell = new Shell();