-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: APCu caching driver #9874
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
Open
sk757a
wants to merge
9
commits into
codeigniter4:4.7
Choose a base branch
from
sk757a:apcucache
base: 4.7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4d6b6be
feat: APCu caching driver implementation
sk757a 79942cb
feat: APCu caching driver: changelog, documentation
sk757a 9770d8d
feat: APCu caching driver: keys iterator improvement, fixes
sk757a cbb69b9
feat: APCu caching driver: header fix
sk757a f26b2ce
feat: APCu caching driver: phpunit workflow extra-ini-options
sk757a aef434f
feat: APCu caching driver: phpunit workflow APCu extension
sk757a 8551ed1
feat: APCu caching driver: extension suggestion
sk757a 6db7a82
feat: APCu caching driver: improvements
sk757a 43b177a
feat: APCu caching driver: improvements
sk757a 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
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,163 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * 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\Handlers; | ||
|
|
||
| use APCUIterator; | ||
| use Closure; | ||
| use CodeIgniter\I18n\Time; | ||
| use Config\Cache; | ||
|
|
||
| /** | ||
| * APCu cache handler | ||
| * | ||
| * @see \CodeIgniter\Cache\Handlers\ApcuHandlerTest | ||
| */ | ||
| class ApcuHandler extends BaseHandler | ||
| { | ||
| /** | ||
| * Note: Use `CacheFactory::getHandler()` to instantiate. | ||
| */ | ||
| public function __construct(Cache $config) | ||
| { | ||
| $this->prefix = $config->prefix; | ||
| } | ||
|
|
||
| public function initialize(): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function get(string $key): mixed | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
| $success = false; | ||
|
|
||
| $data = apcu_fetch($key, $success); | ||
|
|
||
| // Success returned by reference from apcu_fetch() | ||
| return $success ? $data : null; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function save(string $key, $value, int $ttl = 60): bool | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
|
|
||
| return apcu_store($key, $value, $ttl); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function remember(string $key, int $ttl, Closure $callback): mixed | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
|
|
||
| return apcu_entry($key, $callback, $ttl); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function delete(string $key): bool | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
|
|
||
| return apcu_delete($key); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function deleteMatching(string $pattern): int | ||
| { | ||
| $matchedKeys = array_filter( | ||
| array_keys(iterator_to_array(new APCUIterator(null, APC_ITER_KEY))), | ||
| static fn ($key): bool => fnmatch($pattern, $key), | ||
| ); | ||
|
|
||
| if ($matchedKeys !== []) { | ||
| return count($matchedKeys) - count(apcu_delete($matchedKeys)); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function increment(string $key, int $offset = 1): false|int | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
|
|
||
| return apcu_inc($key, $offset); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function decrement(string $key, int $offset = 1): false|int | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
|
|
||
| return apcu_dec($key, $offset); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function clean(): bool | ||
| { | ||
| return apcu_clear_cache(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getCacheInfo(): array|false | ||
| { | ||
| return apcu_cache_info(true); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getMetaData(string $key): ?array | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
| $metadata = apcu_key_info($key); | ||
|
|
||
| if ($metadata !== null) { | ||
| return [ | ||
| 'expire' => $metadata['ttl'] > 0 ? Time::now()->getTimestamp() + $metadata['ttl'] : null, | ||
| 'mtime' => $metadata['mtime'], | ||
| 'data' => apcu_fetch($key), | ||
| ]; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function isSupported(): bool | ||
| { | ||
| return extension_loaded('apcu') && apcu_enabled(); | ||
| } | ||
| } |
Oops, something went wrong.
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.