From 41e82faa64d8fc8932737b3615bfa762f3d796aa Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 31 Mar 2017 20:38:57 +0200 Subject: [PATCH] fixed cache, use files as cache instead of database, minor fixes --- cache/cache.php | 15 ++++++++++++--- cache/cache_css.php | 19 ++++++++++++++++++- cache/cache_filemask.php | 2 +- cache/cache_js.php | 19 ++++++++++++++++++- config/config_ids.php | 4 ++++ page/State.php | 4 ++-- sai/page/default_page.php | 20 ++++++++++---------- system/HEADER.php | 1 + 8 files changed, 66 insertions(+), 18 deletions(-) diff --git a/cache/cache.php b/cache/cache.php index 9ca0a2a..d4b7068 100644 --- a/cache/cache.php +++ b/cache/cache.php @@ -27,13 +27,16 @@ class cache { $result = \SYSTEM\SQL\SYS_CACHE_CHECK::Q1(array($cache,$ident)); if(!$result){ return NULL;} + if(!file_exists($result['data'])){ + return NULL;} + if($header){ if(\SYSTEM\HEADER::available($result['type'])){ call_user_func('\SYSTEM\HEADER::'.$result['type']); }else{ \SYSTEM\HEADER::FILE($ident);} } - return $result['cache'] == \SYSTEM\CACHE\cache_filemask::CACHE_FILEMASK ? \file_get_contents($result['data']) : $result['data']; + return \file_get_contents($result['data']); } /** @@ -49,8 +52,14 @@ class cache { public static function put($cache, $ident, $type, $data, $fail_on_exist = false){ if(($fail_on_exist && self::get($cache,$ident) != NULL)){ return NULL;} - - $result = \SYSTEM\SQL\SYS_CACHE_PUT::Q1(array($cache,$ident, $type, $data)); + + $path = \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_CACHE).$ident; + + $file = \fopen($path, "w"); + \fwrite($file, $data); + \fclose($file); + + $result = \SYSTEM\SQL\SYS_CACHE_PUT::Q1(array($cache,$ident, $type, $path)); return $result ? $data : NULL; } diff --git a/cache/cache_css.php b/cache/cache_css.php index 4d518fd..a8d8e58 100644 --- a/cache/cache_css.php +++ b/cache/cache_css.php @@ -57,7 +57,24 @@ class cache_css { * @param array $files List of Files to be cached into one Cacheentry * @return url Returns the requested Cache-URL */ - public static function url($files){ + /*public static function url($files){ + $ident = self::ident($files); + if(!\SYSTEM\CACHE\cache_css::get($ident)){ + \LIB\lib_minify::php(); + $minifier = new \MatthiasMullie\Minify\CSS(); + foreach($files as $f){ + $minifier->add($f->SERVERPATH());} + \SYSTEM\CACHE\cache_css::put($ident, $minifier->minify());} + return './api.php?call=cache&id='.self::CACHE_CSS.'&ident='.$ident; + }*/ + + /** + * Minify CSS and calculate URL from it + * + * @param array $files List of Files to be cached into one Cacheentry + * @return url Returns the requested Cache-URL + */ + public static function minify($files){ $ident = self::ident($files); if(!\SYSTEM\CACHE\cache_css::get($ident)){ \LIB\lib_minify::php(); diff --git a/cache/cache_filemask.php b/cache/cache_filemask.php index 232464a..51dc299 100644 --- a/cache/cache_filemask.php +++ b/cache/cache_filemask.php @@ -28,7 +28,7 @@ class cache_filemask { public static function put($file){ $ext = pathinfo($file); $ext = strtoupper(array_key_exists('extension', $ext) ? $ext['extension'] : ''); - return \SYSTEM\CACHE\cache::put(self::CACHE_FILEMASK, self::ident($file), $ext ,$file);} + return \SYSTEM\CACHE\cache::put(self::CACHE_FILEMASK, self::ident($file), $ext ,file_get_contents($file));} /** * Get Data from Cache diff --git a/cache/cache_js.php b/cache/cache_js.php index eba7228..303c08a 100644 --- a/cache/cache_js.php +++ b/cache/cache_js.php @@ -57,7 +57,24 @@ class cache_js { * @param array $files List of Files to be cached into one Cacheentry * @return url Returns the requested Cache-URL */ - public static function url($files){ + /*public static function url($files){ + $ident = self::ident($files); + if(!\SYSTEM\CACHE\cache_js::get($ident)){ + \LIB\lib_minify::php(); + $minifier = new \MatthiasMullie\Minify\JS(); + foreach($files as $f){ + $minifier->add($f->SERVERPATH());} + \SYSTEM\CACHE\cache_js::put($ident, $minifier->minify());} + return './api.php?call=cache&id='.self::CACHE_JS.'&ident='.$ident; + }*/ + + /** + * Minify JS files and calculate URL for it + * + * @param array $files List of Files to be cached into one Cacheentry + * @return url Returns the requested Cache-URL + */ + public static function minify($files){ $ident = self::ident($files); if(!\SYSTEM\CACHE\cache_js::get($ident)){ \LIB\lib_minify::php(); diff --git a/config/config_ids.php b/config/config_ids.php index dfc000f..a42c939 100644 --- a/config/config_ids.php +++ b/config/config_ids.php @@ -56,6 +56,10 @@ class config_ids { /** int Logextraction path used by the Application */ const SYS_CRON_LOG2SQLITE_PATH = 30; + + /** int Cache path used by the Application */ + const SYS_CONFIG_PATH_CACHE = 31; + /** int Projectname of the Application */ const SYS_SAI_CONFIG_PROJECT = 54; } \ No newline at end of file diff --git a/page/State.php b/page/State.php index 099f949..b3b4114 100644 --- a/page/State.php +++ b/page/State.php @@ -53,7 +53,7 @@ class State { $row['css'] = $row['js'] = array(); if(\class_exists($row['php_class']) && \method_exists($row['php_class'], 'css') && \is_callable($row['php_class'].'::css')){ $row['css'] = array_merge($row['css'], \call_user_func($row['php_class'].'::css'));} - $row['css'] = count($row['css']) > 0 ? array(\SYSTEM\CACHE\cache_css::url($row['css'])) : array(); + $row['css'] = count($row['css']) > 0 ? array(\SYSTEM\CACHE\cache_css::minify($row['css'])) : array(); if(\class_exists($row['php_class']) && \method_exists($row['php_class'], 'scss') && \is_callable($row['php_class'].'::scss')){ $scss = \call_user_func($row['php_class'].'::scss'); foreach($scss as $s){ @@ -61,7 +61,7 @@ class State { } if(\class_exists($row['php_class']) && \method_exists($row['php_class'], 'js') && \is_callable($row['php_class'].'::js')){ $row['js'] = array_merge($row['js'], \call_user_func($row['php_class'].'::js'));} - $row['js'] = count($row['js']) > 0 ? array(\SYSTEM\CACHE\cache_js::url($row['js'])) : array(); + $row['js'] = count($row['js']) > 0 ? array(\SYSTEM\CACHE\cache_js::minify($row['js'])) : array(); if(\class_exists($row['php_class']) && \method_exists($row['php_class'], 'title') && \is_callable($row['php_class'].'::title')){ $row['title'] = \call_user_func($row['php_class'].'::title');} if(\class_exists($row['php_class']) && \method_exists($row['php_class'], 'meta') && \is_callable($row['php_class'].'::meta')){ diff --git a/sai/page/default_page.php b/sai/page/default_page.php index 3eabd86..7bf274b 100644 --- a/sai/page/default_page.php +++ b/sai/page/default_page.php @@ -66,9 +66,9 @@ class default_page implements \SYSTEM\PAGE\DefaultPage { */ public static function css(){ return \SYSTEM\HTML\html::link(\LIB\lib_bootstrap::css()->WEBPATH(false)). - \SYSTEM\HTML\html::link(\LIB\lib_tablesorter::css()->WEBPATH(false)). - \SYSTEM\HTML\html::link(\SYSTEM\CACHE\cache_css::url( - array( \LIB\lib_system::css(), + \SYSTEM\HTML\html::link(\SYSTEM\CACHE\cache_css::minify( + array( \LIB\lib_tablesorter::css(), + \LIB\lib_system::css(), new \SYSTEM\PSAI('page/css/sai_classes.css'), new \SYSTEM\PSAI('page/css/sai.css')))); } @@ -79,13 +79,13 @@ class default_page implements \SYSTEM\PAGE\DefaultPage { * @return string Returns html of the Sai js includes */ public static function js(){ - return \SYSTEM\HTML\html::script(\LIB\lib_jquery::js()->WEBPATH()). - \SYSTEM\HTML\html::script(\LIB\lib_bootstrap::js()->WEBPATH()). - \SYSTEM\HTML\html::script(\LIB\lib_tablesorter::js()->WEBPATH()). - \SYSTEM\HTML\html::script(\LIB\lib_bootstrap_growl::js()->WEBPATH()). - \SYSTEM\HTML\html::script(\LIB\lib_tinymce::js()->WEBPATH(false)). - \SYSTEM\HTML\html::script( \SYSTEM\CACHE\cache_js::url( - array( \LIB\lib_system::js(), + return \SYSTEM\HTML\html::script( \SYSTEM\CACHE\cache_js::minify( + array( \LIB\lib_jquery::js(), + \LIB\lib_bootstrap::js(), + \LIB\lib_tablesorter::js(), + \LIB\lib_bootstrap_growl::js(), + \LIB\lib_tinymce::js(), + \LIB\lib_system::js(), new \SYSTEM\PSAI('page/js/sai.js')))). \SYSTEM\HTML\html::script('https://www.google.com/jsapi'). ''; diff --git a/system/HEADER.php b/system/HEADER.php index 50e6cbb..d269af9 100644 --- a/system/HEADER.php +++ b/system/HEADER.php @@ -92,6 +92,7 @@ class HEADER { */ public static function JS(){ if(self::checkHeader()){ + header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 7))); // 1 week header('content-type:application/javascript;');}} /** * Send CSS Headers, if Header was not sent yet