Skip to content

Commit

Permalink
Upd cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fomvasss committed Aug 3, 2020
1 parent 59f5258 commit a4e0f39
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/VariableManager.php
Expand Up @@ -54,17 +54,25 @@ public function get(string $key, $default = null, bool $useCache = true)
return $var ? $var->value : $default;
}

if (isset($this->all()[$key])) {
return $this->all()[$key];
if ($collection = $this->getCollection()) {
if ($var = $collection
->where('key', $key)
->whereIn('locale', [$this->locale, null])
//->orWhere('locale', null)
->first()) {

return $var->value;
}
}

return $default;
}

public function set(string $key, $value = null)
public function set(string $key, $value = null, $locale = null)
{
return $this->variableModel->updateOrCreate([
'key' => $key,
'locale' => $locale ?: $this->locale,
], [
'value' => $value,
]);
Expand All @@ -75,13 +83,9 @@ public function set(string $key, $value = null)
*/
public function all(): array
{
$all = $this->cacheRepository->remember($this->config['cache']['name'], $this->config['cache']['time'], function () {
return $this->getCollection();
});

if ($all) {
if ($all = $this->getCollection()) {
$allByLocale = $this->locale ? $all->where('locale', $this->locale) : $all;
$this->variables = $allByLocale->pluck('value', 'key')->toArray();
$this->variables = $allByLocale->toArray();
} else {
$this->variables = [];
}
Expand Down Expand Up @@ -116,7 +120,9 @@ public function cacheClear()
protected function getCollection()
{
try {
return $this->variableModel->select('key', 'value', 'locale')->get();
return $this->cacheRepository->remember($this->config['cache']['name'], $this->config['cache']['time'], function () {
return $this->variableModel->select('key', 'value', 'locale')->get();
});
} catch (\Exception $exception) {
$this->app['log']->info(__CLASS__ . ': ' . $exception->getMessage());

Expand Down

0 comments on commit a4e0f39

Please sign in to comment.