From 6091b59e181528e5f2fb2870ad247e8a1101eae2 Mon Sep 17 00:00:00 2001 From: Siau Pheng Date: Wed, 4 Nov 2026 15:36:17 +0700 Subject: [PATCH] Update Cache class: correct last modified date, improve file existence checks, and enhance cache creation logic for better performance and clarity. --- src/Data/Cache.php | 99 +++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/src/Data/Cache.php b/src/Data/Cache.php index 2cc5e48..222d41e 100644 --- a/src/Data/Cache.php +++ b/src/Data/Cache.php @@ -5,7 +5,7 @@ Module : /Data/Cache.php Created By : Siau Pheng (siaupheng@3fonia.com). 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); - } else { - $contents .= "\n". file_get_contents($nfile); - } + foreach ($afile as $file) { + if (!file_exists($file)) continue; + if (!$this->IS_DEV) { + $smini->add($file); + } else { + $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)) { - [ $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'; - $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]); - $is_modif = true; - } + $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.'-'. ($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_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 ($_type === 'css') { - $acache['CSS'][] = 'cache/' . $_file; - } else $acache['JS'][] = 'cache/' . $_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'][] = '