Create cronjob_db system for caching query results in cron.php
This commit is contained in:
parent
88e0409367
commit
f64bd4bc36
28
base.php
28
base.php
|
@ -52,6 +52,8 @@ function instance_config($software, $instance=null) {
|
|||
if ($instance === null)
|
||||
return apiresult(['error' => '<instance> 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';
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue