diff --git a/README.md b/README.md index 8d9ab2f..5e34ed0 100644 --- a/README.md +++ b/README.md @@ -125,11 +125,12 @@ Setting::setConstraint(function($query, $insert) { ### Custom stores -This package uses the Laravel `Manager` class under the hood, so it's easy to add your own custom session store driver if you want to store in some other way. All you need to do is extend the abstract `SettingStore` class, implement the abstract methods and call `Setting::extend`. +This package uses the Laravel `Manager` class under the hood, so it's easy to add your own custom session store driver if you want to store in some other way. +All you need to do is extend the abstract `CachedSettingStore` class, implement the abstract methods and call `Setting::extend`. ```php ``` +If you want your setting store to be exempt from Laravel's caching, extend from `SettingStore` instead. ## Contact diff --git a/src/CachedSettingStore.php b/src/CachedSettingStore.php new file mode 100644 index 0000000..4478ec1 --- /dev/null +++ b/src/CachedSettingStore.php @@ -0,0 +1,85 @@ + + * @license http://opensource.org/licenses/MIT + * @package l4-settings + */ + +namespace anlutro\LaravelSettings; + +abstract class CachedSettingStore extends SettingStore +{ + /** + * Cache key for save + */ + const CACHE_KEY = 'setting:cache'; + + /** + * @var \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface + */ + protected $cache = null; + + /** + * Cache TTL in seconds. + * + * @var int + */ + protected $cacheTtl = 15; + + /** + * Whether to reset the cache when changing a setting. + * + * @var boolean + */ + protected $cacheForgetOnWrite = true; + + /** + * Set the cache. + * @param \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface $cache + * @param int $ttl + * @param bool $forgetOnWrite + */ + public function setCache($cache, $ttl = null, $forgetOnWrite = null) + { + $this->cache = $cache; + if ($ttl !== null) { + $this->cacheTtl = $ttl; + } + if ($forgetOnWrite !== null) { + $this->cacheForgetOnWrite = $forgetOnWrite; + } + } + + /** + * Save any changes done to the settings data. + * + * @return void + */ + public function save() + { + if ($this->unsaved && $this->cache && $this->cacheForgetOnWrite) { + $this->cache->forget(static::CACHE_KEY); + } + + parent::save(); + } + + /** + * Read data from a store + * + * @return array + */ + private function readData() + { + if ($this->cache) { + return $this->cache->remember(static::CACHE_KEY, $this->cacheTtl, function () { + return $this->read(); + }); + } + + return $this->read(); + } + +} diff --git a/src/DatabaseSettingStore.php b/src/DatabaseSettingStore.php index 5abea1d..b5b9453 100644 --- a/src/DatabaseSettingStore.php +++ b/src/DatabaseSettingStore.php @@ -12,7 +12,7 @@ use Illuminate\Database\Connection; use Illuminate\Support\Arr; -class DatabaseSettingStore extends SettingStore +class DatabaseSettingStore extends CachedSettingStore { /** * The database connection instance. diff --git a/src/JsonSettingStore.php b/src/JsonSettingStore.php index a913089..85b1b01 100644 --- a/src/JsonSettingStore.php +++ b/src/JsonSettingStore.php @@ -11,7 +11,7 @@ use Illuminate\Filesystem\Filesystem; -class JsonSettingStore extends SettingStore +class JsonSettingStore extends CachedSettingStore { /** * @param \Illuminate\Filesystem\Filesystem $files diff --git a/src/SettingStore.php b/src/SettingStore.php index 3375158..424c86f 100644 --- a/src/SettingStore.php +++ b/src/SettingStore.php @@ -11,11 +11,6 @@ abstract class SettingStore { - /** - * Cache key for save - */ - const CACHE_KEY = 'setting:cache'; - /** * The settings data. * @@ -58,25 +53,6 @@ abstract class SettingStore */ protected $defaults = []; - /** - * @var \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface - */ - protected $cache = null; - - /** - * Cache TTL in seconds. - * - * @var int - */ - protected $cacheTtl = 15; - - /** - * Whether to reset the cache when changing a setting. - * - * @var boolean - */ - protected $cacheForgetOnWrite = true; - /** * Set default values. * @@ -87,23 +63,6 @@ public function setDefaults(array $defaults) $this->defaults = $defaults; } - /** - * Set the cache. - * @param \Illuminate\Contracts\Cache\Store|\Illuminate\Cache\StoreInterface $cache - * @param int $ttl - * @param bool $forgetOnWrite - */ - public function setCache($cache, $ttl = null, $forgetOnWrite = null) - { - $this->cache = $cache; - if ($ttl !== null) { - $this->cacheTtl = $ttl; - } - if ($forgetOnWrite !== null) { - $this->cacheForgetOnWrite = $forgetOnWrite; - } - } - /** * Get a specific key from the settings data. * @@ -213,10 +172,6 @@ public function save() return; } - if ($this->cache && $this->cacheForgetOnWrite) { - $this->cache->forget(static::CACHE_KEY); - } - $this->write($this->data); $this->unsaved = false; } @@ -237,18 +192,13 @@ public function load($force = false) } /** - * Read data from a store or cache + * Read data from a store. * * @return array */ private function readData() { - if ($this->cache) { - return $this->cache->remember(static::CACHE_KEY, $this->cacheTtl, function () { - return $this->read(); - }); - } - + // note: this method exists purely for CachedSettingStore. return $this->read(); } diff --git a/src/SettingsManager.php b/src/SettingsManager.php index adb9a14..13a572a 100644 --- a/src/SettingsManager.php +++ b/src/SettingsManager.php @@ -23,9 +23,7 @@ public function createJsonDriver() { $path = $this->getConfig('anlutro/l4-settings::path'); - $store = new JsonSettingStore($this->getSupportedContainer()['files'], $path); - - return $this->wrapDriver($store); + return new JsonSettingStore($this->getSupportedContainer()['files'], $path); } public function createDatabaseDriver() @@ -36,14 +34,12 @@ public function createDatabaseDriver() $keyColumn = $this->getConfig('anlutro/l4-settings::keyColumn'); $valueColumn = $this->getConfig('anlutro/l4-settings::valueColumn'); - $store = new DatabaseSettingStore($connection, $table, $keyColumn, $valueColumn); - - return $this->wrapDriver($store); + return new DatabaseSettingStore($connection, $table, $keyColumn, $valueColumn); } public function createMemoryDriver() { - return $this->wrapDriver(new MemorySettingStore()); + return new MemorySettingStore(); } public function createArrayDriver() @@ -60,19 +56,21 @@ protected function getConfig($key) return $this->getSupportedContainer()['config']->get($key); } - protected function wrapDriver($store) + protected function createDriver($driver) { - $store->setDefaults($this->getConfig('anlutro/l4-settings::defaults')); + $instance = parent::createDriver($driver); + + $instance->setDefaults($this->getConfig('anlutro/l4-settings::defaults')); - if ($this->getConfig('anlutro/l4-settings::enableCache')) { - $store->setCache( + if ($instance instanceof CachedSettingStore && $this->getConfig('anlutro/l4-settings::enableCache')) { + $instance->setCache( $this->getSupportedContainer()['cache'], $this->getConfig('anlutro/l4-settings::cacheTtl'), $this->getConfig('anlutro/l4-settings::forgetCacheByWrite') ); } - return $store; + return $instance; } protected function getSupportedContainer()