Skip to content
Snippets Groups Projects
Commit e7b98422 authored by Aleksandr Borodulin's avatar Aleksandr Borodulin
Browse files

Cache changes

parent de10c732
No related branches found
Tags v5.1.2
No related merge requests found
......@@ -15,26 +15,18 @@ use Symfony\Component\Cache\Adapter\FilesystemAdapter;
class SettingsLoader
{
const DEFAULT_LIFE_TIME = 3600;
/**
* @var Repository
*/
protected $repository;
/**
* @var Cache
*/
protected $cacheProvider;
protected Repository $repository;
protected FilesystemAdapter $cacheProvider;
/**
* Construct
*
* @param Repository $repository
* @param Repository $repository
*/
public function __construct(Repository $repository)
{
$this->repository = $repository;
$this->cacheProvider = new FilesystemAdapter();
$this->cacheProvider = new FilesystemAdapter('settings');
}
/**
......@@ -44,9 +36,9 @@ class SettingsLoader
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
public function get(string $key, $default = null)
{
return $this->load($key, $default)->get();
return $this->load($key, $default);
}
/**
......@@ -55,7 +47,7 @@ class SettingsLoader
* @param string $key
* @return bool
*/
public function remove($key)
public function remove(string $key): bool
{
if ($this->cacheProvider->hasItem($key)) {
return $this->cacheProvider->delete($key);
......@@ -68,13 +60,13 @@ class SettingsLoader
* Load setting value
*
* @param string $key
* @param mixed $default
* @param mixed $default
* @return mixed
*/
protected function load($key, $default)
{
if ($this->cacheProvider->hasItem($key)) {
return $this->cacheProvider->getItem($key);
return $this->cacheProvider->getItem($key)->get();
}
$value = $this->repository->findOneByKey($key);
......@@ -84,6 +76,7 @@ class SettingsLoader
// Save value to cache storage
$this->save($key, $value->getValue());
return $value->getValue();
}
......@@ -98,16 +91,16 @@ class SettingsLoader
* @param int $lifeTime
* @return bool
*/
protected function save($key, $value, $lifeTime = self::DEFAULT_LIFE_TIME)
protected function save(string $key, $value, int $lifeTime = self::DEFAULT_LIFE_TIME): bool
{
if ($this->cacheProvider->hasItem($key)) {
// Remove old value
$this->cacheProvider->delete($key);
}
$item = $this->cacheProvider->getItem($key);
$item->set($value);
$item->expiresAfter($lifeTime);
return $this->cacheProvider->save($item);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment