-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
171 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Cache; | ||
|
||
use CodeIgniter\Autoloader\FileLocator; | ||
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; | ||
|
||
final class FileLocatorCache | ||
{ | ||
/** | ||
* @var CacheInterface|FileVarExportHandler | ||
*/ | ||
private $cache; | ||
|
||
private FileLocator $locator; | ||
|
||
/** | ||
* @param CacheInterface|FileVarExportHandler|null $cache | ||
*/ | ||
public function __construct($cache = null) | ||
{ | ||
$this->cache = $cache ?? new FileVarExportHandler(); | ||
} | ||
|
||
public function setLocator(FileLocator $fileLocator) | ||
{ | ||
$this->locator = $fileLocator; | ||
} | ||
|
||
public function save(): void | ||
{ | ||
if (! $this->locator->isCacheUpdated()) { | ||
return; | ||
} | ||
|
||
$data = $this->locator->getCache(); | ||
|
||
$this->cache->save($this->getCacheKey(), $data, 3600 * 24); | ||
} | ||
|
||
private function getCacheKey(): string | ||
{ | ||
return 'FileLocatorCache'; | ||
} | ||
|
||
public function load(): bool | ||
{ | ||
$key = $this->getCacheKey(); | ||
|
||
if (! $data = $this->cache->get($key)) { | ||
return false; | ||
} | ||
|
||
$this->locator->setCache($data); | ||
|
||
return true; | ||
} | ||
|
||
public function delete(): void | ||
{ | ||
$this->cache->delete($this->getCacheKey()); | ||
} | ||
} |