From 5e877ea2ba0ab4e29935c6a6be5ca343ae1493b5 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 30 Oct 2018 16:21:57 -0600 Subject: [PATCH] Use a public property for the cache key for settings models that cache rendered CSS This enables developers to modify what the cache key is for a given instance of a settings model so that they can do things like load user dependent settings values and have those values affect the rendered CSS. Previously the key being static across all instances of a given setting model would prevent that from being feasible without clearing the cache for every request. --- modules/backend/models/BrandSetting.php | 16 ++++++++++------ modules/backend/models/EditorSetting.php | 16 ++++++++++------ modules/system/models/MailBrandSetting.php | 16 ++++++++++------ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/modules/backend/models/BrandSetting.php b/modules/backend/models/BrandSetting.php index 389f12d12..c03160af9 100644 --- a/modules/backend/models/BrandSetting.php +++ b/modules/backend/models/BrandSetting.php @@ -41,8 +41,11 @@ class BrandSetting extends Model public $attachOne = [ 'logo' => \System\Models\File::class ]; - - const CACHE_KEY = 'backend::brand.custom_css'; + + /** + * @var string The key to store rendered CSS in the cache under + */ + public $cacheKey = 'backend::brand.custom_css'; const PRIMARY_COLOR = '#34495e'; // Wet Asphalt const SECONDARY_COLOR = '#e67e22'; // Pumpkin @@ -79,7 +82,7 @@ class BrandSetting extends Model public function afterSave() { - Cache::forget(self::CACHE_KEY); + Cache::forget(self::instance()->cacheKey); } public static function getLogo() @@ -95,13 +98,14 @@ class BrandSetting extends Model public static function renderCss() { - if (Cache::has(self::CACHE_KEY)) { - return Cache::get(self::CACHE_KEY); + $cacheKey = self::instance()->cacheKey; + if (Cache::has($cacheKey)) { + return Cache::get($cacheKey); } try { $customCss = self::compileCss(); - Cache::forever(self::CACHE_KEY, $customCss); + Cache::forever($cacheKey, $customCss); } catch (Exception $ex) { $customCss = '/* ' . $ex->getMessage() . ' */'; diff --git a/modules/backend/models/EditorSetting.php b/modules/backend/models/EditorSetting.php index 9dc9d86ec..4fda96bcf 100644 --- a/modules/backend/models/EditorSetting.php +++ b/modules/backend/models/EditorSetting.php @@ -33,8 +33,11 @@ class EditorSetting extends Model * @var mixed Settings form field defitions */ public $settingsFields = 'fields.yaml'; - - const CACHE_KEY = 'backend::editor.custom_css'; + + /** + * @var string The key to store rendered CSS in the cache under + */ + public $cacheKey = 'backend::editor.custom_css'; protected $defaultHtmlAllowEmptyTags = 'textarea, a, iframe, object, video, style, script'; @@ -100,7 +103,7 @@ class EditorSetting extends Model public function afterSave() { - Cache::forget(self::CACHE_KEY); + Cache::forget(self::instance()->cacheKey); } protected function makeStylesForTable($arr) @@ -157,13 +160,14 @@ class EditorSetting extends Model public static function renderCss() { - if (Cache::has(self::CACHE_KEY)) { - return Cache::get(self::CACHE_KEY); + $cacheKey = self::instance()->cacheKey; + if (Cache::has($cacheKey)) { + return Cache::get($cacheKey); } try { $customCss = self::compileCss(); - Cache::forever(self::CACHE_KEY, $customCss); + Cache::forever($cacheKey, $customCss); } catch (Exception $ex) { $customCss = '/* ' . $ex->getMessage() . ' */'; diff --git a/modules/system/models/MailBrandSetting.php b/modules/system/models/MailBrandSetting.php index 9f2a075c5..528ce2641 100644 --- a/modules/system/models/MailBrandSetting.php +++ b/modules/system/models/MailBrandSetting.php @@ -35,8 +35,11 @@ class MailBrandSetting extends Model * @var mixed Settings form field defitions */ public $settingsFields = 'fields.yaml'; - - const CACHE_KEY = 'system::mailbrand.custom_css'; + + /** + * @var string The key to store rendered CSS in the cache under + */ + public $cacheKey = 'system::mailbrand.custom_css'; const WHITE_COLOR = '#fff'; const BODY_BG = '#f5f8fa'; @@ -80,18 +83,19 @@ class MailBrandSetting extends Model public function resetCache() { - Cache::forget(self::CACHE_KEY); + Cache::forget(self::instance()->cacheKey); } public static function renderCss() { - if (Cache::has(self::CACHE_KEY)) { - return Cache::get(self::CACHE_KEY); + $cacheKey = self::instance()->cacheKey; + if (Cache::has($cacheKey)) { + return Cache::get($cacheKey); } try { $customCss = self::compileCss(); - Cache::forever(self::CACHE_KEY, $customCss); + Cache::forever($cacheKey, $customCss); } catch (Exception $ex) { $customCss = '/* ' . $ex->getMessage() . ' */';