mirror of
https://github.com/flarum/framework.git
synced 2024-11-28 11:34:36 +08:00
Build a caching repository decorator for settings
This commit is contained in:
parent
8e9cf4fd2e
commit
ba3fa73f16
45
src/Core/Settings/CachedSettingsRepository.php
Normal file
45
src/Core/Settings/CachedSettingsRepository.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Flarum\Core\Settings;
|
||||
|
||||
class CachedSettingsRepository implements SettingsRepository
|
||||
{
|
||||
protected $inner;
|
||||
|
||||
protected $isCached;
|
||||
|
||||
protected $cache = [];
|
||||
|
||||
public function __construct(SettingsRepository $inner)
|
||||
{
|
||||
$this->inner = $inner;
|
||||
}
|
||||
|
||||
public function all()
|
||||
{
|
||||
if (!$this->isCached) {
|
||||
$this->cache = $this->inner->all();
|
||||
$this->isCached = true;
|
||||
}
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
if (array_key_exists($key, $this->cache)) {
|
||||
return $this->cache[$key];
|
||||
} else if (!$this->isCached) {
|
||||
return array_get($this->all(), $key, $default);
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->cache[$key] = $value;
|
||||
|
||||
$this->inner->set($key, $value);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user