-
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.
Merge pull request #7696 from kenjis/feat-Factories-config-cache
feat: [Factories] Config caching
- Loading branch information
Showing
17 changed files
with
506 additions
and
10 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
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,65 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Cache; | ||
|
||
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; | ||
use CodeIgniter\Config\Factories; | ||
|
||
final class FactoriesCache | ||
{ | ||
/** | ||
* @var CacheInterface|FileVarExportHandler | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @param CacheInterface|FileVarExportHandler|null $cache | ||
*/ | ||
public function __construct($cache = null) | ||
{ | ||
$this->cache = $cache ?? new FileVarExportHandler(); | ||
} | ||
|
||
public function save(string $component): void | ||
{ | ||
if (! Factories::isUpdated($component)) { | ||
return; | ||
} | ||
|
||
$data = Factories::getComponentInstances($component); | ||
|
||
$this->cache->save($this->getCacheKey($component), $data, 3600 * 24); | ||
} | ||
|
||
private function getCacheKey(string $component): string | ||
{ | ||
return 'FactoriesCache_' . $component; | ||
} | ||
|
||
public function load(string $component): bool | ||
{ | ||
$key = $this->getCacheKey($component); | ||
|
||
if (! $data = $this->cache->get($key)) { | ||
return false; | ||
} | ||
|
||
Factories::setComponentInstances($component, $data); | ||
|
||
return true; | ||
} | ||
|
||
public function delete(string $component): void | ||
{ | ||
$this->cache->delete($this->getCacheKey($component)); | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Cache\FactoriesCache; | ||
|
||
final class FileVarExportHandler | ||
{ | ||
private string $path = WRITEPATH . 'cache'; | ||
|
||
/** | ||
* @param array|bool|float|int|object|string|null $val | ||
*/ | ||
public function save(string $key, $val): void | ||
{ | ||
$val = var_export($val, true); | ||
|
||
// Write to temp file first to ensure atomicity | ||
$tmp = $this->path . "/{$key}." . uniqid('', true) . '.tmp'; | ||
file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX); | ||
|
||
rename($tmp, $this->path . "/{$key}"); | ||
} | ||
|
||
public function delete(string $key): void | ||
{ | ||
@unlink($this->path . "/{$key}"); | ||
} | ||
|
||
/** | ||
* @return array|bool|float|int|object|string|null | ||
*/ | ||
public function get(string $key) | ||
{ | ||
return @include $this->path . "/{$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
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
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
Oops, something went wrong.