-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: [Factories] Config caching #7696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
92ecc14
feat: add methods for caching
kenjis ccc532d
feat: add FactoriesCache for Config caching
kenjis f9b95e3
feat: add __set_state() for Config caching
kenjis db612fa
refactor: move definition of ENVIRONMENT to index.php/spark
kenjis 9e4225b
feat: add property to stop Config property override
kenjis b01bb38
refactor: use config() instead of new keyword
kenjis 47af9c0
docs: add Config caching code as comments
kenjis f49a557
chore: update psalm-baseline.xml
kenjis eb59d2b
refactor: by rector
kenjis 1e81980
refactor: remove $val
kenjis 0404ae9
docs: fix @param
kenjis b8a4286
refactor: remove __set_state() in Config\Paths
kenjis 8c2fdc6
refactor: add return types
kenjis c78f8dd
test: fix out-of-dated test code
kenjis 8de664f
docs: add docs
kenjis 82328c4
docs: fix by proofreading
kenjis ef045ce
refactor: early return
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 <[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\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 hidden or 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 <[email protected]> | ||
| * | ||
| * 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.