Skip to content

Commit

Permalink
Adding the cache system
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Jan 10, 2016
1 parent 6c4bb75 commit 65b0cee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
6 changes: 6 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
'table' => 'settings',
'model' => \Arcanesoft\Settings\Models\Setting::class,
],

'cache' => [
'enabled' => true,

'key' => 'cached_settings'
],
];
79 changes: 75 additions & 4 deletions src/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Arcanedev\Support\Collection;
use Arcanesoft\Settings\Helpers\Arr;
use Arcanesoft\Settings\Models\Setting;
use Illuminate\Contracts\Cache\Repository as Cache;

/**
* Class SettingsManager
Expand Down Expand Up @@ -44,18 +45,27 @@ class SettingsManager implements Contracts\Settings
*/
private $model;

/**
* The cache repository
*
* @var \Illuminate\Contracts\Cache\Repository
*/
private $cache;

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
*/
/**
* SettingsManager constructor.
*
* @param Setting $model
* @param \Arcanesoft\Settings\Models\Setting $model
* @param \Illuminate\Contracts\Cache\Repository $cache
*/
public function __construct(Setting $model)
public function __construct(Setting $model, Cache $cache)
{
$this->model = $model;
$this->cache = $cache;
$this->data = new Collection;
}

Expand All @@ -70,7 +80,40 @@ public function __construct(Setting $model)
*/
protected function getDefaultDomain()
{
return config('arcanesoft.settings.default-domain', 'default');
return $this->config('default-domain', 'default');
}

/**
* Get the cache key.
*
* @return string
*/
protected function getCacheKey()
{
return $this->config('cache.key', 'cached_settings');
}

/**
* Check if cache is enabled.
*
* @return bool
*/
protected function isCached()
{
return $this->config('cache.enabled', false);
}

/**
* Get the config value by key.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
private function config($key, $default = null)
{
return config("arcanesoft.settings.$key", $default);
}

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -226,6 +269,8 @@ public function save()
$model->delete();
}
}

$this->flushCachedSettings();
}

/**
Expand Down Expand Up @@ -324,7 +369,7 @@ private function checkLoaded()
*/
private function loadData()
{
foreach ($this->model->all() as $setting) {
foreach ($this->getCachedSettings() as $setting) {
/** @var Setting $setting */
$data = $this->data->get($setting->domain, []);

Expand All @@ -333,4 +378,30 @@ private function loadData()
$this->data->put($setting->domain, $data);
}
}

/**
* Get cached settings.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
private function getCachedSettings()
{
if ($this->isCached()) {
return $this->cache->rememberForever($this->getCacheKey(), function() {
return $this->model->all();
});
}

return $this->model->all();
}

/**
* Flush the cached settings.
*/
private function flushCachedSettings()
{
if ($this->isCached()) {
$this->cache->forget($this->getCacheKey());
}
}
}
5 changes: 4 additions & 1 deletion tests/SettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ public function it_can_reset()
*/
private function makeSettings()
{
return new SettingsManager(new Setting);
return new SettingsManager(
new Setting,
$this->app[\Illuminate\Contracts\Cache\Repository::class]
);
}

/**
Expand Down

0 comments on commit 65b0cee

Please sign in to comment.