Skip to content

Commit

Permalink
Add caching to reduce queries to the database and github
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Nov 18, 2024
1 parent dc09cfb commit 3c7eaa9
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 29 deletions.
69 changes: 69 additions & 0 deletions root/app/www/public/classes/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
----------------------------------
------ Created: 111724 ------
------ Austin Best ------
----------------------------------
*/

//-- BRING IN THE EXTRAS
loadClassExtras('Cache');

class Cache
{
public $cache;

public function __construct()
{
$this->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);
}
}
7 changes: 5 additions & 2 deletions root/app/www/public/classes/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions root/app/www/public/classes/Starr.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ class Starr
{
use Overrides;

public $cache;

public function __construct()
{
global $cache;

$this->cache = $cache ?: new Cache();
}

public function __tostring()
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -142,6 +153,8 @@ public function getEndpoints($app)
}
}

$this->cache->set($cacheKey, json_encode($endpoints), STARR_ENDPOINT_LIST_TIME);

return $endpoints;
}

Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 23 additions & 7 deletions root/app/www/public/classes/traits/Database/Apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ 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;
}

public function getAppFromId($appId, $appsTable)
{
$appsTable ??= $this->getAppsTable();
$appsTable = $appsTable ?: $this->getAppsTable();

foreach ($appsTable as $app) {
if ($app['id'] == $appId) {
Expand Down Expand Up @@ -58,6 +68,8 @@ public function addApp($fields = [])
return $this->error();
}

$this->cache->bust(APPS_TABLE_CACHE_KEY);

return;
}

Expand All @@ -81,6 +93,8 @@ public function updateApp($appId, $fields = [])
return $this->error();
}

$this->cache->bust(APPS_TABLE_CACHE_KEY);

return;
}

Expand All @@ -94,6 +108,8 @@ public function deleteApp($appId)
return $this->error();
}

$this->cache->bust(APPS_TABLE_CACHE_KEY);

return;
}

Expand Down
32 changes: 25 additions & 7 deletions root/app/www/public/classes/traits/Database/Starrs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ 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;
}

public function getStarrAppFromId($id, $starrsTable)
{
$starrsTable ??= $this->getStarrsTable();
$starrsTable = $starrsTable ?: $this->getStarrsTable();

foreach ($starrsTable as $starrApp) {
if ($starrApp['id'] == $id) {
Expand All @@ -47,6 +57,8 @@ public function deleteStarrApp($starrId)
return $this->error();
}

$this->cache->bust(STARRS_TABLE_CACHE_KEY);

return;
}

Expand All @@ -62,6 +74,8 @@ public function addStarrApp($starrApp, $fields = [])
return $this->error();
}

$this->cache->bust(STARRS_TABLE_CACHE_KEY);

return;
}

Expand All @@ -76,6 +90,8 @@ public function updateStarrApp($id, $fields = [])
return $this->error();
}

$this->cache->bust(STARRS_TABLE_CACHE_KEY);

return;
}

Expand All @@ -92,6 +108,8 @@ public function updateStarrAppSetting($id, $field, $value)
return $this->error();
}

$this->cache->bust(STARRS_TABLE_CACHE_KEY);

return;
}
}
17 changes: 9 additions & 8 deletions root/app/www/public/functions/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
8 changes: 8 additions & 0 deletions root/app/www/public/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions root/app/www/public/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
}
}

//-- INITIALIZE THE CACHE CLASS
$cache = new Cache();

//-- INITIALIZE THE SHELL CLASS
$shell = new Shell();

Expand Down

0 comments on commit 3c7eaa9

Please sign in to comment.