From 5199f65420bff773045c4bd1238ed50c3a150b28 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 2 Feb 2023 15:14:13 +0800 Subject: [PATCH] Allow settings model cache TTL to be configurable --- modules/system/behaviors/SettingsModel.php | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/system/behaviors/SettingsModel.php b/modules/system/behaviors/SettingsModel.php index 3dd8f02c5..cf7c8b06f 100644 --- a/modules/system/behaviors/SettingsModel.php +++ b/modules/system/behaviors/SettingsModel.php @@ -16,6 +16,10 @@ use System\Classes\ModelBehavior; * public $settingsCode = 'author_plugin_code'; * public $settingsFields = 'fields.yaml'; * + * Optionally: + * + * public $settingsCacheTtl = 1440; + * */ class SettingsModel extends ModelBehavior { @@ -25,6 +29,11 @@ class SettingsModel extends ModelBehavior protected $fieldConfig; protected $fieldValues = []; + /** + * @var integer Settings cache TTL, in seconds. + */ + protected int $cacheTtl = 1440; + /** * @var array Internal cache of model objects. */ @@ -62,6 +71,10 @@ class SettingsModel extends ModelBehavior * Parse the config */ $this->recordCode = $this->model->settingsCode; + + if ($this->model->propertyExists('settingsCacheTtl')) { + $this->cacheTtl = (int) $this->model->settingsCacheTtl; + } } /** @@ -108,10 +121,13 @@ class SettingsModel extends ModelBehavior */ public function getSettingsRecord() { - $record = $this->model - ->where('item', $this->recordCode) - ->remember(1440, $this->getCacheKey()) - ->first(); + $query = $this->model->where('item', $this->recordCode); + + if ($this->cacheTtl > 0) { + $query = $query->remember($this->cacheTtl, $this->getCacheKey()); + } + + $record = $query->first(); return $record ?: null; }