-
Notifications
You must be signed in to change notification settings - Fork 31
feature: initial PSR-6 cache implementation #61
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
chr15k
wants to merge
25
commits into
tigitz:master
Choose a base branch
from
chr15k:feature/initial-cache-implementation
base: master
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 15 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
5c3b13a
feature/initial-cache-implementation - initial implementation with ba…
chr15k 76af19d
feature/initial-cache-implementation - update gitignore
chr15k 394ed7e
feature/initial-cache-implementation - composer.json updated
chr15k 69bfc8a
feature/initial-cache-implementation - streamline cache directory res…
chr15k a2fb471
feature/initial-cache-implementation - decouple adapter args from abs…
chr15k d64bed3
feature/initial-cache-implementation - get phpstan passing
chr15k 2e3b173
feature/initial-cache-implementation - initial aspell cache implement…
chr15k 8554e45
feature/initial-cache-implementation - refactor cache methods and ins…
chr15k dfcadff
Merge branch 'master' into feature/initial-cache-implementation
chr15k eb2d58d
feature/initial-cache-implementation - phpcs fixer
chr15k 3942885
Merge branch 'feature/initial-cache-implementation' of github.com:chr…
chr15k 9e6f7b3
feature/initial-cache-implementation - fix phpcs deprecation warning
chr15k 7e4e17d
feature/initial-cache-implementation - fix phpcs deprecation warning
chr15k 0b87946
feature/initial-cache-implementation - revert phpcs native_function_i…
chr15k fc88d4c
feature/initial-cache-implementation - re-ran phpcs fixer
chr15k f1b8ffa
feature/initial-cache-implementation - replace symfony/cache with psr…
chr15k a7bc62a
feature/initial-cache-implementation - rebase master
chr15k 3ea7c67
feature/initial-cache-implementation - update Safe method namespace
chr15k 4d31033
feature/initial-cache-implementation - minor changes
chr15k c596eec
feature/initial-cache-implementation - minor changes
chr15k 5665dd3
feature/initial-cache-implementation - refactor to psr/cache implemen…
chr15k 9a64c36
feature/initial-cache-implementation - refactor to psr/cache implemen…
chr15k 51ab8f3
feature/initial-cache-implementation - phpstan fixes
chr15k bd2f4c4
feature/initial-cache-implementation - phpstan fixes
chr15k f21ccb5
feature/initial-cache-implementation - remove readonly class for php …
chr15k 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSpellcheck\Cache; | ||
|
||
use PhpSpellcheck\Cache\Stores\StoreInterface; | ||
use PhpSpellcheck\Exception\InvalidArgumentException; | ||
|
||
class Cache implements CacheFactoryInterface | ||
{ | ||
/** | ||
* Get a cache store instance by driver. | ||
* | ||
* @param array<string, mixed> $config | ||
*/ | ||
public static function create(?string $driver = null, array $config = []): StoreInterface | ||
{ | ||
$driver ??= self::getDefaultDriver(); | ||
|
||
$class = self::resolveStoreClass($driver); | ||
|
||
return $class::create(...$config); | ||
} | ||
|
||
/** | ||
* Resolve the cache store class. | ||
*/ | ||
public static function resolveStoreClass(string $driver): string | ||
{ | ||
$class = \sprintf('%s\%s\%s%s', __NAMESPACE__, 'Stores', ucfirst($driver), 'Store'); | ||
|
||
if (!class_exists($class)) { | ||
throw new InvalidArgumentException("Cache store [{$driver}] is not defined."); | ||
} | ||
|
||
return $class; | ||
} | ||
|
||
/** | ||
* Get the default cache driver name. | ||
*/ | ||
private static function getDefaultDriver(): string | ||
{ | ||
return 'file'; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSpellcheck\Cache; | ||
|
||
use PhpSpellcheck\Cache\Stores\StoreInterface; | ||
|
||
interface CacheFactoryInterface | ||
{ | ||
/** | ||
* Get a cache store instance by name. | ||
* | ||
* @param array<string, mixed> $config | ||
*/ | ||
public static function create(?string $name = null, array $config = []): StoreInterface; | ||
} |
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,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSpellcheck\Cache\Stores; | ||
|
||
use Composer\Autoload\ClassLoader; | ||
use Symfony\Contracts\Cache\ItemInterface; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
|
||
class FileStore implements StoreInterface | ||
{ | ||
/** | ||
* The filesystem adapter instance. | ||
*/ | ||
public function __construct(private FilesystemAdapter $filesystemAdapter) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Create a new file cache store instance. | ||
*/ | ||
public static function create(string $namespace = '', int $defaultLifetime = 3600, ?string $cacheDirectory = null): self | ||
{ | ||
return new self(new FilesystemAdapter($namespace, $defaultLifetime, $cacheDirectory ?? self::getDefaultCachePath())); | ||
} | ||
|
||
/** | ||
* Get the default cache directory for the file store. | ||
*/ | ||
public static function getDefaultCachePath(): string | ||
{ | ||
return \dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]).'/.phpspellcheck.cache'; | ||
} | ||
|
||
/** | ||
* Fetches an item from the cache. | ||
* | ||
* @param array<mixed> $metadata | ||
*/ | ||
public function get(string $key, callable $callback, float $beta = null, ?array &$metadata = null): mixed | ||
{ | ||
return $this->filesystemAdapter->get($key, $callback, $beta, $metadata); | ||
} | ||
|
||
/** | ||
* Set the value of the given key in the cache. | ||
chr15k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function set(string $key, mixed $value, ?int $lifetime = null): void | ||
{ | ||
$item = $this->filesystemAdapter->getItem($key); | ||
$item->set($value); | ||
|
||
if ($lifetime !== null) { | ||
$item->expiresAfter($lifetime); | ||
} | ||
|
||
$this->filesystemAdapter->save($item); | ||
} | ||
|
||
/** | ||
* Removed an item from the pool. | ||
*/ | ||
public function delete(string $key): bool | ||
{ | ||
return $this->filesystemAdapter->delete($key); | ||
} | ||
|
||
/** | ||
* Fetches an item from the cache. | ||
*/ | ||
public function getItem(string $key): ItemInterface | ||
{ | ||
return $this->filesystemAdapter->getItem($key); | ||
} | ||
|
||
/** | ||
* Clear data from cache. | ||
*/ | ||
public function clear(string $prefix = ''): bool | ||
{ | ||
return $this->filesystemAdapter->clear($prefix); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSpellcheck\Cache\Stores; | ||
|
||
use Symfony\Contracts\Cache\ItemInterface; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
|
||
interface StoreInterface extends CacheInterface | ||
{ | ||
/** | ||
* Clear data from cache. | ||
*/ | ||
public function clear(string $prefix = ''): bool; | ||
|
||
/** | ||
* Fetches an item from the cache. | ||
*/ | ||
public function getItem(string $key): ItemInterface; | ||
|
||
/** | ||
* Create a new cache store instance. | ||
*/ | ||
public static function create(string $namespace = '', int $defaultLifetime = 3600, ?string $cacheDirectory = null): self; | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSpellcheck\Traits; | ||
|
||
use PhpSpellcheck\Cache\Cache; | ||
use PhpSpellcheck\Cache\Stores\StoreInterface; | ||
|
||
trait SpellcheckerCacheTrait | ||
{ | ||
/** | ||
* The cache store instance. | ||
*/ | ||
private StoreInterface $cache; | ||
|
||
/** | ||
* The cache store instance. | ||
* | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function initCache(array $config = []): void | ||
{ | ||
$config['namespace'] ??= $this->getCacheNamespace(); | ||
|
||
$this->cache = Cache::create(config: $config); | ||
} | ||
|
||
/** | ||
* Get the cache key for the given text and languages. | ||
* | ||
* @param array<int, string> $languages | ||
*/ | ||
private function getCacheKey(string $text, array $languages): string | ||
{ | ||
return md5(\sprintf('%s_%s', $text, implode('_', $languages))); | ||
} | ||
|
||
/** | ||
* Get the cache namespace. | ||
*/ | ||
private function getCacheNamespace(): string | ||
{ | ||
$parts = explode('\\', \get_class($this)); | ||
|
||
return end($parts); | ||
} | ||
} |
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.