-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from watchenterprise/cache-resolution
Cache resolution
- Loading branch information
1 parent
f9678fc
commit 23579c7
Showing
7 changed files
with
233 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaSettings; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
|
||
use function collect; | ||
use function is_array; | ||
use function is_string; | ||
|
||
class NovaSettingsCacheStore extends NovaSettingsStore | ||
{ | ||
/** @var \Illuminate\Contracts\Cache\Repository */ | ||
private $cache; | ||
|
||
public function __construct() | ||
{ | ||
$this->cache = Cache::store(config('nova-settings.cache.store')); | ||
} | ||
|
||
public function clearCache($keyNames = null) | ||
{ | ||
// Clear whole cache | ||
if (empty($keyNames)) { | ||
$this->getSettingsModelClass()::all(['key'])->each(function ($setting) { | ||
$this->cache->forget($this->getCacheKey($setting->key)); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// Clear specific keys | ||
if (is_string($keyNames)) $keyNames = [$keyNames]; | ||
foreach ($keyNames as $key) { | ||
$this->cache->forget($this->getCacheKey($key)); | ||
} | ||
} | ||
|
||
protected function getCached($keyNames = null) | ||
{ | ||
if (is_string($keyNames)) { | ||
return $this->cache->get($this->getCacheKey($keyNames)); | ||
} | ||
|
||
if (is_array($keyNames)) { | ||
return collect($keyNames) | ||
->mapWithKeys(function ($key) { | ||
if (!$this->cache->has($this->getCacheKey($key))) return []; | ||
|
||
return [$key => $this->getCached($key)]; | ||
}) | ||
->toArray(); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
protected function setCached($keyName, $value) | ||
{ | ||
$this->cache->forever($this->getCacheKey($keyName), $value); | ||
} | ||
|
||
private function getCacheKey($key) | ||
{ | ||
return config('nova-settings.cache.prefix', 'nova-settings:') . $key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaSettings; | ||
|
||
use function collect; | ||
use function is_array; | ||
use function is_string; | ||
|
||
class NovaSettingsInMemoryStore extends NovaSettingsStore | ||
{ | ||
protected $cache = []; | ||
|
||
public function clearCache($keyNames = null) | ||
{ | ||
// Clear whole cache | ||
if (empty($keyNames)) { | ||
$this->cache = []; | ||
return; | ||
} | ||
|
||
// Clear specific keys | ||
if (is_string($keyNames)) $keyNames = [$keyNames]; | ||
foreach ($keyNames as $key) { | ||
unset($this->cache[$key]); | ||
} | ||
} | ||
|
||
protected function getCached($keyNames = null) | ||
{ | ||
if (is_string($keyNames)) return $this->cache[$keyNames] ?? null; | ||
|
||
return is_array($keyNames) && !empty($keyNames) | ||
? collect($this->cache)->only($keyNames)->toArray() | ||
: $this->cache; | ||
} | ||
|
||
protected function setCached($keyName, $value) | ||
{ | ||
$this->cache[$keyName] = $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaSettings; | ||
|
||
use function is_string; | ||
|
||
class NovaSettingsNoCacheStore extends NovaSettingsStore | ||
{ | ||
public function clearCache($keyNames = null) | ||
{ | ||
} | ||
|
||
protected function getCached($keyNames = null) | ||
{ | ||
if (is_string($keyNames)) return null; | ||
|
||
return []; | ||
} | ||
|
||
protected function setCached($keyName, $value) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters