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.
This commit is contained in:
Luke Towers 2018-10-30 16:21:57 -06:00
parent 6fb08f3e1a
commit 5e877ea2ba
3 changed files with 30 additions and 18 deletions

View File

@ -41,8 +41,11 @@ class BrandSetting extends Model
public $attachOne = [ public $attachOne = [
'logo' => \System\Models\File::class '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 PRIMARY_COLOR = '#34495e'; // Wet Asphalt
const SECONDARY_COLOR = '#e67e22'; // Pumpkin const SECONDARY_COLOR = '#e67e22'; // Pumpkin
@ -79,7 +82,7 @@ class BrandSetting extends Model
public function afterSave() public function afterSave()
{ {
Cache::forget(self::CACHE_KEY); Cache::forget(self::instance()->cacheKey);
} }
public static function getLogo() public static function getLogo()
@ -95,13 +98,14 @@ class BrandSetting extends Model
public static function renderCss() public static function renderCss()
{ {
if (Cache::has(self::CACHE_KEY)) { $cacheKey = self::instance()->cacheKey;
return Cache::get(self::CACHE_KEY); if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
} }
try { try {
$customCss = self::compileCss(); $customCss = self::compileCss();
Cache::forever(self::CACHE_KEY, $customCss); Cache::forever($cacheKey, $customCss);
} }
catch (Exception $ex) { catch (Exception $ex) {
$customCss = '/* ' . $ex->getMessage() . ' */'; $customCss = '/* ' . $ex->getMessage() . ' */';

View File

@ -33,8 +33,11 @@ class EditorSetting extends Model
* @var mixed Settings form field defitions * @var mixed Settings form field defitions
*/ */
public $settingsFields = 'fields.yaml'; 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'; protected $defaultHtmlAllowEmptyTags = 'textarea, a, iframe, object, video, style, script';
@ -100,7 +103,7 @@ class EditorSetting extends Model
public function afterSave() public function afterSave()
{ {
Cache::forget(self::CACHE_KEY); Cache::forget(self::instance()->cacheKey);
} }
protected function makeStylesForTable($arr) protected function makeStylesForTable($arr)
@ -157,13 +160,14 @@ class EditorSetting extends Model
public static function renderCss() public static function renderCss()
{ {
if (Cache::has(self::CACHE_KEY)) { $cacheKey = self::instance()->cacheKey;
return Cache::get(self::CACHE_KEY); if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
} }
try { try {
$customCss = self::compileCss(); $customCss = self::compileCss();
Cache::forever(self::CACHE_KEY, $customCss); Cache::forever($cacheKey, $customCss);
} }
catch (Exception $ex) { catch (Exception $ex) {
$customCss = '/* ' . $ex->getMessage() . ' */'; $customCss = '/* ' . $ex->getMessage() . ' */';

View File

@ -35,8 +35,11 @@ class MailBrandSetting extends Model
* @var mixed Settings form field defitions * @var mixed Settings form field defitions
*/ */
public $settingsFields = 'fields.yaml'; 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 WHITE_COLOR = '#fff';
const BODY_BG = '#f5f8fa'; const BODY_BG = '#f5f8fa';
@ -80,18 +83,19 @@ class MailBrandSetting extends Model
public function resetCache() public function resetCache()
{ {
Cache::forget(self::CACHE_KEY); Cache::forget(self::instance()->cacheKey);
} }
public static function renderCss() public static function renderCss()
{ {
if (Cache::has(self::CACHE_KEY)) { $cacheKey = self::instance()->cacheKey;
return Cache::get(self::CACHE_KEY); if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
} }
try { try {
$customCss = self::compileCss(); $customCss = self::compileCss();
Cache::forever(self::CACHE_KEY, $customCss); Cache::forever($cacheKey, $customCss);
} }
catch (Exception $ex) { catch (Exception $ex) {
$customCss = '/* ' . $ex->getMessage() . ' */'; $customCss = '/* ' . $ex->getMessage() . ' */';