Create cronjob_db system for caching query results in cron.php

This commit is contained in:
Bofh 2022-12-03 15:59:01 +01:00
parent 88e0409367
commit f64bd4bc36
2 changed files with 40 additions and 4 deletions

View File

@ -52,6 +52,8 @@ function instance_config($software, $instance=null) {
if ($instance === null) if ($instance === null)
return apiresult(['error' => '<instance> GET argument must be provided'], 400); return apiresult(['error' => '<instance> GET argument must be provided'], 400);
$GLOBALS['ap_instance'] = $instance;
$GLOBALS['ap_software'] = $software;
if (isset($GLOBALS['_cache'][$software.$instance])) if (isset($GLOBALS['_cache'][$software.$instance]))
return $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)); 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 // classes
require 'classes/PgDatabase.php'; require 'classes/PgDatabase.php';

View File

@ -37,21 +37,29 @@ class PgDatabase {
} }
public function fetch($sql, $cache=null) { 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) { if ($cache !== null) {
$cached = content_cache__get($sql); $cached = content_cache__get($cache_key);
if ($cached !== null) if ($cached !== null)
return $cached; return $cached;
} }
$result = $this->query($sql); $result = $this->query($sql);
$data = pg_fetch_assoc($result); $data = pg_fetch_assoc($result);
if ($cache !== null) if ($cache !== null)
content_cache__put($sql, $cache, $data); content_cache__put($cache_key, $cache, $data);
return $data; return $data;
} }
public function fetch_all($sql, $cache=null) { 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) { if ($cache !== null) {
$cached = content_cache__get($sql); $cached = content_cache__get($cache_key);
if ($cached !== null) if ($cached !== null)
return $cached; return $cached;
} }
@ -62,7 +70,7 @@ class PgDatabase {
} }
pg_free_result($result); pg_free_result($result);
if ($cache !== null) if ($cache !== null)
content_cache__put($sql, $cache, $data); content_cache__put($cache_key, $cache, $data);
return $data; return $data;
} }