From 19cd3bb88e73cff369a519e43b52b66763c23cde Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Thu, 8 Mar 2018 17:27:15 +0100 Subject: [PATCH] * [ADD] Cache for theme icons. * [MOD] Improved caching by setting an expire time. --- .../web/themes/material-blue/inc/Icons.php | 66 +++++++++---------- lib/SP/Core/Acl/Actions.php | 59 +++++++++++------ lib/SP/Core/UI/Theme.php | 36 +++++++++- lib/SP/Html/Assets/IconBase.php | 38 +++++------ .../Services/Account/AccountSearchService.php | 15 ++++- lib/SP/Storage/FileCache.php | 14 +++- lib/SP/Storage/FileStorageInterface.php | 13 +++- 7 files changed, 162 insertions(+), 79 deletions(-) diff --git a/app/modules/web/themes/material-blue/inc/Icons.php b/app/modules/web/themes/material-blue/inc/Icons.php index f1276855..4a8530d2 100644 --- a/app/modules/web/themes/material-blue/inc/Icons.php +++ b/app/modules/web/themes/material-blue/inc/Icons.php @@ -43,47 +43,47 @@ class Icons extends ThemeIconsBase public function setIcons() { // Iconos de Acciones - $this->iconAdd = new FontIcon('add', 'mdl-color-text--indigo-A200', __('Añadir')); - $this->iconView = new FontIcon('visibility', 'mdl-color-text--indigo-A200', __('Ver Detalles')); - $this->iconViewPass = new FontIcon('lock_open', 'mdl-color-text--indigo-A200', __('Ver Clave')); - $this->iconEdit = new FontIcon('mode_edit', 'mdl-color-text--amber-A200', __('Editar')); - $this->iconDelete = new FontIcon('remove_circle', 'mdl-color-text--red-A200', __('Eliminar')); - $this->iconEditPass = new FontIcon('lock_outline', 'mdl-color-text--amber-A200', __('Cambiar Clave')); - $this->iconAppAdmin = new FontIcon('star', 'mdl-color-text--amber-A100', __('Admin Aplicación')); - $this->iconAccAdmin = new FontIcon('star_half', 'mdl-color-text--amber-A100', __('Admin Cuentas')); - $this->iconLdapUser = new FontIcon('business', 'mdl-color-text--deep-purple-A100', __('Usuario de LDAP')); - $this->iconDisabled = new FontIcon('error', 'mdl-color-text--red-A100', __('Deshabilitado')); - $this->iconEnabled = new FontIcon('check_circle', 'mdl-color-text--teal-500', __('Habilitado')); - $this->iconRefresh = new FontIcon('refresh', 'mdl-color-text--teal-500', __('Actualizar')); - $this->iconCopy = new FontIcon('content_copy', 'mdl-color-text--indigo-A200', __('Copiar')); + $this->iconAdd = new FontIcon('add', 'mdl-color-text--indigo-A200', __u('Añadir')); + $this->iconView = new FontIcon('visibility', 'mdl-color-text--indigo-A200', __u('Ver Detalles')); + $this->iconViewPass = new FontIcon('lock_open', 'mdl-color-text--indigo-A200', __u('Ver Clave')); + $this->iconEdit = new FontIcon('mode_edit', 'mdl-color-text--amber-A200', __u('Editar')); + $this->iconDelete = new FontIcon('remove_circle', 'mdl-color-text--red-A200', __u('Eliminar')); + $this->iconEditPass = new FontIcon('lock_outline', 'mdl-color-text--amber-A200', __u('Cambiar Clave')); + $this->iconAppAdmin = new FontIcon('star', 'mdl-color-text--amber-A100', __u('Admin Aplicación')); + $this->iconAccAdmin = new FontIcon('star_half', 'mdl-color-text--amber-A100', __u('Admin Cuentas')); + $this->iconLdapUser = new FontIcon('business', 'mdl-color-text--deep-purple-A100', __u('Usuario de LDAP')); + $this->iconDisabled = new FontIcon('error', 'mdl-color-text--red-A100', __u('Deshabilitado')); + $this->iconEnabled = new FontIcon('check_circle', 'mdl-color-text--teal-500', __u('Habilitado')); + $this->iconRefresh = new FontIcon('refresh', 'mdl-color-text--teal-500', __u('Actualizar')); + $this->iconCopy = new FontIcon('content_copy', 'mdl-color-text--indigo-A200', __u('Copiar')); $this->iconClipboard = new FontIcon('content_paste', 'mdl-color-text--indigo-A200'); - $this->iconEmail = new FontIcon('email', 'mdl-color-text--indigo-A200', __('Email')); + $this->iconEmail = new FontIcon('email', 'mdl-color-text--indigo-A200', __u('Email')); $this->iconOptional = new FontIcon('settings'); $this->iconPublicLink = new FontIcon('link', 'mdl-color-text--teal-500'); - $this->iconBack = new FontIcon('arrow_back', 'mdl-color-text--indigo-A200', __('Volver')); - $this->iconRestore = new FontIcon('restore', 'mdl-color-text--teal-500', __('Restaurar')); - $this->iconSave = new FontIcon('save', 'mdl-color-text--teal-500', __('Guardar')); - $this->iconHelp = new FontIcon('help_outline', 'mdl-color-text--indigo-A100', __('Ayuda')); - $this->iconClear = new FontIcon('clear_all', 'mdl-color--indigo-A200', __('Limpiar')); - $this->iconPlay = new FontIcon('play_circle_filled', 'mdl-color-text--teal-500', __('Realizar')); - $this->iconDownload = new FontIcon('file_download', 'mdl-color-text--indigo-A200', __('Descargar')); - $this->iconWarning = new FontIcon('warning', 'mdl-color-text--amber-A100', __('Aviso')); - $this->iconCheck = new FontIcon('cached', 'mdl-color-text--indigo-A200', __('Comprobar')); - $this->iconSearch = new FontIcon('search', 'mdl-color-text--indigo-A200', __('Buscar')); + $this->iconBack = new FontIcon('arrow_back', 'mdl-color-text--indigo-A200', __u('Volver')); + $this->iconRestore = new FontIcon('restore', 'mdl-color-text--teal-500', __u('Restaurar')); + $this->iconSave = new FontIcon('save', 'mdl-color-text--teal-500', __u('Guardar')); + $this->iconHelp = new FontIcon('help_outline', 'mdl-color-text--indigo-A100', __u('Ayuda')); + $this->iconClear = new FontIcon('clear_all', 'mdl-color--indigo-A200', __u('Limpiar')); + $this->iconPlay = new FontIcon('play_circle_filled', 'mdl-color-text--teal-500', __u('Realizar')); + $this->iconDownload = new FontIcon('file_download', 'mdl-color-text--indigo-A200', __u('Descargar')); + $this->iconWarning = new FontIcon('warning', 'mdl-color-text--amber-A100', __u('Aviso')); + $this->iconCheck = new FontIcon('cached', 'mdl-color-text--indigo-A200', __u('Comprobar')); + $this->iconSearch = new FontIcon('search', 'mdl-color-text--indigo-A200', __u('Buscar')); $this->iconAccount = new FontIcon('account_box', 'mdl-color-text--indigo-A200'); $this->iconGroup = new FontIcon('group_work', 'mdl-color-text--indigo-A200'); - $this->iconSettings = new FontIcon('settings', 'mdl-color-text--indigo-A200', __('Configuración')); + $this->iconSettings = new FontIcon('settings', 'mdl-color-text--indigo-A200', __u('Configuración')); $this->iconHeadline = new FontIcon('view_headline', 'mdl-color-text--indigo-A200'); - $this->iconInfo = new FontIcon('info_outline', 'mdl-color-text--indigo-A200', __('Información')); - $this->iconCritical = new FontIcon('error_outline', 'mdl-color-text--red-A200', __('Crítico')); - $this->iconNotices = new FontIcon('notifications', 'mdl-color-text--indigo-A200', __('Notificaciones')); - $this->iconRemove = new FontIcon('remove', 'mdl-color-text--indigo-A200', __('Eliminar')); + $this->iconInfo = new FontIcon('info_outline', 'mdl-color-text--indigo-A200', __u('Información')); + $this->iconCritical = new FontIcon('error_outline', 'mdl-color-text--red-A200', __u('Crítico')); + $this->iconNotices = new FontIcon('notifications', 'mdl-color-text--indigo-A200', __u('Notificaciones')); + $this->iconRemove = new FontIcon('remove', 'mdl-color-text--indigo-A200', __u('Eliminar')); // Iconos de Navegación - $this->iconNavPrev = new FontIcon('chevron_left', null, __('Página anterior')); - $this->iconNavNext = new FontIcon('chevron_right', null, __('Página siguiente')); - $this->iconNavFirst = new FontIcon('arrow_back', null, __('Primera página')); - $this->iconNavLast = new FontIcon('arrow_forward', null, __('Última página')); + $this->iconNavPrev = new FontIcon('chevron_left', null, __u('Página anterior')); + $this->iconNavNext = new FontIcon('chevron_right', null, __u('Página siguiente')); + $this->iconNavFirst = new FontIcon('arrow_back', null, __u('Primera página')); + $this->iconNavLast = new FontIcon('arrow_forward', null, __u('Última página')); // Iconos de Ordenación $this->iconUp = new FontIcon('arrow_drop_up'); diff --git a/lib/SP/Core/Acl/Actions.php b/lib/SP/Core/Acl/Actions.php index 79988364..e9e41d2c 100644 --- a/lib/SP/Core/Acl/Actions.php +++ b/lib/SP/Core/Acl/Actions.php @@ -25,6 +25,7 @@ namespace SP\Core\Acl; use SP\DataModel\ActionData; +use SP\Storage\FileException; use SP\Storage\FileStorageInterface; use SP\Storage\XmlFileStorageInterface; @@ -38,7 +39,7 @@ class Actions /** * Cache file name */ - const CACHE_NAME = 'actions'; + const ACTIONS_CACHE_FILE = CACHE_PATH . DIRECTORY_SEPARATOR . 'actions.cache'; /** * Cache expire time */ @@ -55,44 +56,60 @@ class Actions * @var XmlFileStorageInterface */ protected $xmlFileStorage; + /** + * @var FileStorageInterface + */ + private $fileStorage; /** * Action constructor. * - * @param FileStorageInterface $fileStorage + * @param FileStorageInterface $fileStorage * @param XmlFileStorageInterface $xmlFileStorage * @throws \SP\Core\Exceptions\FileNotFoundException */ public function __construct(FileStorageInterface $fileStorage, XmlFileStorageInterface $xmlFileStorage) { $this->xmlFileStorage = $xmlFileStorage; + $this->fileStorage = $fileStorage; - $this->loadCache($fileStorage); + $this->loadCache(); } /** * Loads actions from cache file * - * @param FileStorageInterface $fileStorage + * @return void * @throws \SP\Core\Exceptions\FileNotFoundException */ - protected function loadCache(FileStorageInterface $fileStorage) + protected function loadCache() { - $fileName = CACHE_PATH . DIRECTORY_SEPARATOR . self::CACHE_NAME; - - if (!file_exists($fileName) || filemtime($fileName) + self::CACHE_EXPIRE < time()) { - $this->map(); - $this->saveCache($fileStorage); + if ($this->fileStorage->isExpired(self::ACTIONS_CACHE_FILE, self::CACHE_EXPIRE)) { + $this->mapAndSave(); } else { - $this->actions = $fileStorage->load($fileName); + try { + $this->actions = $this->fileStorage->load(self::ACTIONS_CACHE_FILE); - if ($this->actions === false) { - $this->map(); - $this->saveCache($fileStorage); + debugLog('Loaded actions cache'); + } catch (FileException $e) { + processException($e); + + $this->mapAndSave(); } } } + /** + * @throws \SP\Core\Exceptions\FileNotFoundException + */ + protected function mapAndSave() + { + debugLog('ACTION CACHE MISS'); + + $this->map(); + $this->saveCache(); + } + /** * Sets an array of actions using id as key * @@ -100,8 +117,6 @@ class Actions */ protected function map() { - debugLog('ACTION CACHE MISS'); - $this->actions = []; $actionBase = new ActionData(); @@ -134,12 +149,16 @@ class Actions /** * Saves actions into cache file - * - * @param FileStorageInterface $fileStorage */ - protected function saveCache(FileStorageInterface $fileStorage) + protected function saveCache() { - $fileStorage->save(CACHE_PATH . DIRECTORY_SEPARATOR . self::CACHE_NAME, $this->actions); + try { + $this->fileStorage->save(self::ACTIONS_CACHE_FILE, $this->actions); + + debugLog('Saved actions cache'); + } catch (FileException $e) { + processException($e); + } } /** diff --git a/lib/SP/Core/UI/Theme.php b/lib/SP/Core/UI/Theme.php index 167908bb..170f1aa9 100644 --- a/lib/SP/Core/UI/Theme.php +++ b/lib/SP/Core/UI/Theme.php @@ -28,6 +28,8 @@ use SP\Bootstrap; use SP\Config\Config; use SP\Config\ConfigData; use SP\Core\Session\Session; +use SP\Storage\FileCache; +use SP\Storage\FileException; use Theme\Icons; defined('APP_ROOT') || die(); @@ -39,6 +41,11 @@ defined('APP_ROOT') || die(); */ class Theme implements ThemeInterface { + const ICONS_CACHE_FILE = CACHE_PATH . DIRECTORY_SEPARATOR . 'icons.cache'; + /** + * Cache expire time + */ + const CACHE_EXPIRE = 86400; /** * @var string */ @@ -75,18 +82,23 @@ class Theme implements ThemeInterface * @var string */ protected $module; + /** + * @var FileCache + */ + private $fileCache; /** * Theme constructor. * * @param $module - * @param Config $config + * @param Config $config * @param Session $session */ - public function __construct($module, Config $config, Session $session) + public function __construct($module, Config $config, Session $session, FileCache $fileCache) { $this->configData = $config->getConfigData(); $this->session = $session; + $this->fileCache = $fileCache; if (is_dir(VIEW_PATH)) { $this->initTheme(); @@ -142,12 +154,32 @@ class Theme implements ThemeInterface */ protected function initIcons() { + if (!$this->fileCache->isExpired(self::ICONS_CACHE_FILE, self::CACHE_EXPIRE)) { + try { + $this->icons = $this->fileCache->load(self::ICONS_CACHE_FILE); + + debugLog('Loaded icons cache'); + + return $this->icons; + } catch (FileException $e) { + processException($e); + } + } + $iconsClass = $this->themePathFull . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'Icons.php'; if (file_exists($iconsClass)) { require $iconsClass; $this->icons = new Icons(); + + try { + $this->fileCache->save(self::ICONS_CACHE_FILE, $this->icons); + + debugLog('Saved icons cache'); + } catch (FileException $e) { + processException($e); + } } return $this->icons; diff --git a/lib/SP/Html/Assets/IconBase.php b/lib/SP/Html/Assets/IconBase.php index 3c5583e7..0173d075 100644 --- a/lib/SP/Html/Assets/IconBase.php +++ b/lib/SP/Html/Assets/IconBase.php @@ -38,27 +38,27 @@ abstract class IconBase implements IconInterface * * @var string */ - protected $_icon = ''; + protected $icon = ''; /** * Título del icono * * @var string */ - protected $_title = ''; + protected $title = ''; /** * Clases CSS del icono * * @var array */ - protected $_class = array(); + protected $class = []; /** * @return string */ public function getTitle() { - return $this->_title; + return __($this->title); } /** @@ -67,18 +67,7 @@ abstract class IconBase implements IconInterface */ public function setTitle($title) { - $this->_title = $title; - - return $this; - } - - /** - * @param $class - * @return $this - */ - public function setClass($class) - { - $this->_class[] = $class; + $this->title = $title; return $this; } @@ -88,7 +77,18 @@ abstract class IconBase implements IconInterface */ public function getClass() { - return implode(' ', $this->_class); + return implode(' ', $this->class); + } + + /** + * @param $class + * @return $this + */ + public function setClass($class) + { + $this->class[] = $class; + + return $this; } /** @@ -96,7 +96,7 @@ abstract class IconBase implements IconInterface */ public function getIcon() { - return $this->_icon; + return $this->icon; } /** @@ -105,7 +105,7 @@ abstract class IconBase implements IconInterface */ public function setIcon($icon) { - $this->_icon = $icon; + $this->icon = $icon; return $this; } } \ No newline at end of file diff --git a/lib/SP/Services/Account/AccountSearchService.php b/lib/SP/Services/Account/AccountSearchService.php index f0e864e0..11732e99 100644 --- a/lib/SP/Services/Account/AccountSearchService.php +++ b/lib/SP/Services/Account/AccountSearchService.php @@ -53,8 +53,13 @@ class AccountSearchService extends Service */ const FILTERS_REGEX = '^(?:(?Puser|group|file|owner|maingroup|expired|private):(?:"(?P[\w\.]+)")?)$'; - const COLORS_FILE = CACHE_PATH . DIRECTORY_SEPARATOR . 'colors.cache'; + const COLORS_CACHE_FILE = CACHE_PATH . DIRECTORY_SEPARATOR . 'colors.cache'; + /** + * Cache expire time + */ + const CACHE_EXPIRE = 86400; + /** * Colores para resaltar las cuentas */ @@ -300,7 +305,9 @@ class AccountSearchService extends Service $this->accountColor[$id] = '#' . self::COLORS[array_rand(self::COLORS)]; try { - $this->fileCache->save(self::COLORS_FILE, $this->accountColor); + $this->fileCache->save(self::COLORS_CACHE_FILE, $this->accountColor); + + debugLog('Saved accounts color cache'); return $this->accountColor[$id]; } catch (FileException $e) { @@ -332,7 +339,9 @@ class AccountSearchService extends Service private function loadColors() { try { - $this->accountColor = $this->fileCache->load(self::COLORS_FILE); + $this->accountColor = $this->fileCache->load(self::COLORS_CACHE_FILE); + + debugLog('Loaded accounts color cache'); } catch (FileException $e) { processException($e); } diff --git a/lib/SP/Storage/FileCache.php b/lib/SP/Storage/FileCache.php index 75beb6e2..11885383 100644 --- a/lib/SP/Storage/FileCache.php +++ b/lib/SP/Storage/FileCache.php @@ -51,7 +51,7 @@ class FileCache implements FileStorageInterface /** * @param string $path - * @param mixed $data + * @param mixed $data * @return FileStorageInterface * @throws FileException */ @@ -92,4 +92,16 @@ class FileCache implements FileStorageInterface return $this; } + + /** + * Returns whether the file is expired + * + * @param string $path + * @param int $time + * @return mixed + */ + public function isExpired($path, $time = 86400) + { + return !file_exists($path) || time() > filemtime($path) + $time; + } } \ No newline at end of file diff --git a/lib/SP/Storage/FileStorageInterface.php b/lib/SP/Storage/FileStorageInterface.php index 7d17521f..8f7f41a8 100644 --- a/lib/SP/Storage/FileStorageInterface.php +++ b/lib/SP/Storage/FileStorageInterface.php @@ -35,13 +35,15 @@ interface FileStorageInterface * @param string $path * * @return mixed + * @throws FileException */ public function load($path); /** * @param string $path - * @param mixed $data + * @param mixed $data * @return FileStorageInterface + * @throws FileException */ public function save($path, $data); @@ -51,4 +53,13 @@ interface FileStorageInterface * @return mixed */ public function delete($path); + + /** + * Returns whether the file is expired + * + * @param string $path + * @param int $time + * @return mixed + */ + public function isExpired($path, $time = 86400); } \ No newline at end of file