From f64bd4bc367341a474d9912733c3353574e213b8 Mon Sep 17 00:00:00 2001 From: Bofh Date: Sat, 3 Dec 2022 15:59:01 +0100 Subject: [PATCH] Create cronjob_db system for caching query results in cron.php --- base.php | 28 ++++++++++++++++++++++++++++ classes/PgDatabase.php | 16 ++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/base.php b/base.php index 942b398..640df1b 100644 --- a/base.php +++ b/base.php @@ -52,6 +52,8 @@ function instance_config($software, $instance=null) { if ($instance === null) return apiresult(['error' => ' GET argument must be provided'], 400); + $GLOBALS['ap_instance'] = $instance; + $GLOBALS['ap_software'] = $software; if (isset($GLOBALS['_cache'][$software.$instance])) return $GLOBALS['_cache'][$software.$instance]; @@ -317,5 +319,31 @@ function content_cache__put($key, $config, $data) { file_put_contents($cache_file.','.$config, serialize($data)); } +function cronjob_db_create__check_cachecfg($software, $instance, $sql, $cache=null) { + if ($cache === null) + return false; + $ps = explode(',',trim($cache)); + if ($ps[0] !== 'always') + return false; + return cronjob_db_create($software, $instance, $sql, intval($ps[1])); +} + +function cronjob_db_create($software, $instance, $sql, $time=3600) { + $job_key = md5($software.$instance.$sql); + $dir_crons = $GLOBALS['appconf']['data_dir'].'/cron'; + if (!file_exists($dir_crons)) + mkdir($dir_crons); + $dir_crons_db = $dir_crons.'/db'; + if (!file_exists($dir_crons_db)) + mkdir($dir_crons_db); + $cron_file = $software.','.$instance.','.$job_key; + foreach (scandir($dir_crons_db) as $fl) { + if (strpos($fl, $cron_file) !== false) + unlink($dir_crons_db.'/'.$fl); + } + file_put_contents($dir_crons_db.'/'.$cron_file.','.$time, $sql); + return $cron_file; +} + // classes require 'classes/PgDatabase.php'; diff --git a/classes/PgDatabase.php b/classes/PgDatabase.php index 926a12a..0c0df0d 100644 --- a/classes/PgDatabase.php +++ b/classes/PgDatabase.php @@ -37,21 +37,29 @@ class PgDatabase { } public function fetch($sql, $cache=null) { + $sql = trim($sql); + cronjob_db_create__check_cachecfg($GLOBALS['ap_software'], + $GLOBALS['ap_instance'], $sql, $cache); + $cache_key = $GLOBALS['ap_software'].$GLOBALS['ap_instance'].$sql; if ($cache !== null) { - $cached = content_cache__get($sql); + $cached = content_cache__get($cache_key); if ($cached !== null) return $cached; } $result = $this->query($sql); $data = pg_fetch_assoc($result); if ($cache !== null) - content_cache__put($sql, $cache, $data); + content_cache__put($cache_key, $cache, $data); return $data; } public function fetch_all($sql, $cache=null) { + $sql = trim($sql); + cronjob_db_create__check_cachecfg($GLOBALS['ap_software'], + $GLOBALS['ap_instance'], $sql, $cache); + $cache_key = $GLOBALS['ap_software'].$GLOBALS['ap_instance'].$sql; if ($cache !== null) { - $cached = content_cache__get($sql); + $cached = content_cache__get($cache_key); if ($cached !== null) return $cached; } @@ -62,7 +70,7 @@ class PgDatabase { } pg_free_result($result); if ($cache !== null) - content_cache__put($sql, $cache, $data); + content_cache__put($cache_key, $cache, $data); return $data; }