Update Cache class: correct last modified date, improve file existence checks, and enhance cache creation logic for better performance and clarity.
This commit is contained in:
+35
-28
@@ -5,7 +5,7 @@
|
||||
Module : /Data/Cache.php
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : March 16th, 2008.
|
||||
Last Modified : October 12th, 2026.
|
||||
Last Modified : November 4th, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
@@ -61,7 +61,7 @@ final class Cache {
|
||||
$this->web_user = $app->session()->get('WEB_USER') ?? [];
|
||||
$this->is_login = (count($this->web_user) > 0);
|
||||
|
||||
$rootdir = (defined('PROJECT_ROOT')) ? PROJECT_ROOT : $app->get('app.project_root');
|
||||
$rootdir = (defined('PROJECT_ROOT')) ? constant('PROJECT_ROOT') : $app->get('app.project_root');
|
||||
$this->cachedir = $rootdir . '/temp/cache/';
|
||||
$this->localedir = $rootdir . '/app/Scripts/locale/';
|
||||
$this->jsapp = $rootdir . '/app/Scripts/';
|
||||
@@ -157,24 +157,22 @@ final class Cache {
|
||||
|
||||
private function create_cache(string $type = "css", array $afile = [], string $cachefile = ""): void {
|
||||
$contents = ''; $smini = null;
|
||||
$bpublic = ($this->app->request()->ip === '127.0.0.1') ? false : true;
|
||||
if ($bpublic === true) {
|
||||
if (!$this->IS_DEV) {
|
||||
if ($type === "css") {
|
||||
$smini = new Minify\CSS;
|
||||
} else {
|
||||
$smini = new Minify\JS;
|
||||
}
|
||||
}
|
||||
foreach ($afile as $nfile) {
|
||||
if (file_exists($nfile)) {
|
||||
if ($bpublic === true) {
|
||||
$smini->add($nfile);
|
||||
foreach ($afile as $file) {
|
||||
if (!file_exists($file)) continue;
|
||||
if (!$this->IS_DEV) {
|
||||
$smini->add($file);
|
||||
} else {
|
||||
$contents .= "\n". file_get_contents($nfile);
|
||||
$contents .= "\n". file_get_contents($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($bpublic) $contents = $smini->minify();
|
||||
if (!$this->IS_DEV) $contents = $smini->minify();
|
||||
file_put_contents($this->cachedir . $cachefile, $contents);
|
||||
}
|
||||
|
||||
@@ -195,38 +193,43 @@ final class Cache {
|
||||
public function Cache(string $mode = "index"): array {
|
||||
if ($mode === 'index') {
|
||||
$this->ini['index']['app'] = (string) $this->db->fetchField("SELECT JS FROM app_operator_profile WHERE UserID=?", [$this->web_user['UserID']]) ?? '';
|
||||
$_menu = $this->db->fetchField("SELECT CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_operator WHERE UserID=?", [$this->web_app['PARENT_CODE'], $this->web_user['UserID']]) ?? '';
|
||||
$_menu = (string) $this->db->fetchField("SELECT CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_operator WHERE UserID=?", [$this->web_app['PARENT_CODE'], $this->web_user['UserID']]) ?? '';
|
||||
$_gmenu = '';
|
||||
if ($this->web_user['Grup_MenuID'] > 0) $_gmenu = $this->db->fetchField('SELECT CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_menu_grup WHERE Grup_MenuID=?', [$this->web_app['PARENT_CODE'], $this->web_user['Grup_MenuID']]) ?? '';
|
||||
if ($this->web_user['Grup_MenuID'] > 0) $_gmenu = (string) $this->db->fetchField('SELECT CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_menu_grup WHERE Grup_MenuID=?', [$this->web_app['PARENT_CODE'], $this->web_user['Grup_MenuID']]) ?? '';
|
||||
$this->my_menu = array_filter(array_unique(explode(",", $_gmenu .",". $_menu)));
|
||||
}
|
||||
|
||||
$is_modif = false;
|
||||
foreach (['css','js','app'] as $type) {
|
||||
$is_index_app = ($mode === 'index' && $type === 'app');
|
||||
if (empty($this->ini[$mode][$type]) || !file_exists($this->cachedir . $this->ini[$mode][$type]) || ($this->IS_DEV)) {
|
||||
$is_iapp = ($mode === 'index' && $type === 'app');
|
||||
if (!empty($this->ini[$mode][$type]) &&
|
||||
file_exists($this->cachedir . $this->ini[$mode][$type]) &&
|
||||
!$is_iapp && !$this->IS_DEV) continue;
|
||||
|
||||
[ $afile, $lastmod ] = $this->gen_file($mode, $type);
|
||||
$cachefile = md5($mode . '-' . $type . '-' . $lastmod) . ($type === 'css' ? '.css' : '.js');
|
||||
if ($is_index_app) $cachefile = md5('index-app-' . (string) $this->web_user['UserID'] . $lastmod) . '.js';
|
||||
$cachefile = md5($mode.'-'.$type.'-'. ($is_iapp?(string) $this->web_user['UserID'].'-':'') .$lastmod) . ($type === 'css' ? '.css' : '.js');
|
||||
|
||||
if (($this->ini[$mode][$type] ?? false) === $cachefile &&
|
||||
file_exists($this->cachedir . $this->ini[$mode][$type]) &&
|
||||
!$this->IS_DEV) continue;
|
||||
|
||||
$this->create_cache($type, $afile, $cachefile);
|
||||
$this->ini[$mode][$type] = $cachefile;
|
||||
if ($is_index_app) $this->db->runQuery("INSERT INTO app_operator_profile (UserID,JS) VALUES (?, ?) ON DUPLICATE KEY UPDATE JS=VALUES(JS)", [$this->web_user['UserID'], $cachefile]);
|
||||
if ($is_iapp) $this->db->runQuery("INSERT INTO app_operator_profile (UserID,JS) VALUES (?, ?) ON DUPLICATE KEY UPDATE JS=VALUES(JS)", [$this->web_user['UserID'], $cachefile]);
|
||||
$is_modif = true;
|
||||
}
|
||||
}
|
||||
|
||||
$acache = [ 'CSS' => [], 'JS' => [], 'JSX' => [] ];
|
||||
foreach ($this->ini[$mode] as $_type => $_file) {
|
||||
if (file_exists($this->cachedir . $_file)) {
|
||||
if (!file_exists($this->cachedir . $_file)) continue;
|
||||
if ($_type === 'css') {
|
||||
$acache['CSS'][] = 'cache/' . $_file;
|
||||
} else $acache['JS'][] = 'cache/' . $_file;
|
||||
}
|
||||
}
|
||||
if ($is_modif === true) $this->save_ini();
|
||||
|
||||
$nonce = $this->app->get('csp_nonce');
|
||||
$_secret = $this->web_app['APP_CODE'] .";". $this->web_app['APP_SID'] .";". $this->app->request()->getVar('HTTP_HOST') .";". "http://".$this->app->request()->getVar('HTTP_HOST') .";". isMobile() .";". ($this->web_app['AUTH_LEVEL']?$this->web_app['AUTH_LEVEL']:0);
|
||||
$_secret = $this->web_app['APP_CODE'] .";". $this->web_app['APP_SID'] .";". $this->app->request()->getVar('HTTP_HOST') .";". "http://".$this->app->request()->getVar('HTTP_HOST') .";". isMobile() .";". ($this->web_app['AUTH_LEVEL'] ?? 0);
|
||||
$_suser = ($this->is_login) ? $this->web_user['Username'] .";". $this->web_user['UserID'] .";". $this->web_user['Grup_MenuID'] .";". $this->web_user['Level'] : "nobody;999;999;9";
|
||||
|
||||
$acache['JSX'][] = '<script nonce="'.$nonce.'" type="text/javascript">';
|
||||
@@ -240,11 +243,15 @@ final class Cache {
|
||||
}
|
||||
|
||||
public function Make_Cache(array $amode = ['login', 'index', 'monitor']): array {
|
||||
$_alog = [];
|
||||
$is_modif = false;
|
||||
$is_runway = (defined('RUNWAY_CMD')) ? constant('RUNWAY_CMD') : false;
|
||||
foreach ($amode as $mode) {
|
||||
foreach (['css','js','app'] as $type) {
|
||||
if ($mode === 'index' && $type === 'app') continue;
|
||||
if (empty($this->ini[$mode][$type]) || !file_exists($this->cachedir . $this->ini[$mode][$type])) {
|
||||
if (!empty($this->ini[$mode][$type]) && file_exists($this->cachedir . $this->ini[$mode][$type]) && !$is_runway) continue;
|
||||
if ($is_runway) $_alog[] = $mode.'-'.$type.' -> '.$this->ini[$mode][$type];
|
||||
|
||||
[ $afile, $lastmod ] = $this->gen_file($mode, $type);
|
||||
$cachefile = md5($mode . '-' . $type . '-' . $lastmod) . ($type === 'css' ? '.css' : '.js');
|
||||
$this->create_cache($type, $afile, $cachefile);
|
||||
@@ -252,14 +259,14 @@ final class Cache {
|
||||
$is_modif = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($is_modif === true) $this->save_ini();
|
||||
|
||||
if (in_array('index', $amode)) {
|
||||
$agrup = $this->db->fetchAll('SELECT Grup_MenuID,CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_menu_grup', [$this->web_app['PARENT_CODE']]);
|
||||
$auser = $this->db->fetchAll("SELECT A.*,IFNULL(B.JS,'') JS FROM (SELECT UserID,Grup_MenuID,CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_operator) A LEFT JOIN app_operator_profile B ON (A.UserID=B.UserID)", [$this->web_app['PARENT_CODE']]);
|
||||
$auser = $this->db->fetchAll("SELECT A.*,IFNULL(B.JS,'') JS FROM (SELECT UserID,Username,Grup_MenuID,CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_operator) A LEFT JOIN app_operator_profile B ON (A.UserID=B.UserID)", [$this->web_app['PARENT_CODE']]);
|
||||
foreach ($auser as $user) {
|
||||
if (empty($user['JS']) || !file_exists($this->cachedir . $user['JS'])) {
|
||||
if (!empty($user['JS']) && file_exists($this->cachedir . $user['JS']) && !$is_runway) continue;
|
||||
if ($is_runway) $_alog[] = 'index-app: '.$user['Username'].' -> '.$user['JS'];
|
||||
$this->my_menu = array_filter(array_unique(explode(",", $user['Menu'] .",". ($agrup[$user['Grup_MenuID']]['Menu'] ?? ''))));
|
||||
|
||||
[ $afile, $lastmod ] = $this->gen_file('index', 'app');
|
||||
@@ -268,8 +275,8 @@ final class Cache {
|
||||
$this->db->runQuery("INSERT INTO app_operator_profile (UserID,JS) VALUES (?, ?) ON DUPLICATE KEY UPDATE JS=VALUES(JS)", [$user['UserID'], $cachefile]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [ 'status' => 1, 'user_count' => count($auser) ];
|
||||
$_alog[] = 'Processed of '.(string) count($auser).' users.';
|
||||
return [ 'status' => 1, 'log' => $_alog ];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user