-
Notifications
You must be signed in to change notification settings - Fork 3
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 #14 from ray-di/ps6-local-cache
Create Psr6LocalCacheModule
- Loading branch information
Showing
4 changed files
with
69 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\PsrCacheModule; | ||
|
||
use Ray\Di\ProviderInterface; | ||
use Ray\PsrCacheModule\Annotation\CacheDir; | ||
use Ray\PsrCacheModule\Annotation\CacheNamespace; | ||
use Symfony\Component\Cache\Adapter\AbstractAdapter; | ||
|
||
use function sys_get_temp_dir; | ||
|
||
/** | ||
* Provide APCu cache adapter if available, otherwise file cache adapter | ||
* | ||
* @implements ProviderInterface<ApcuAdapter|FilesystemAdapter> | ||
*/ | ||
final class LocalCacheProvider implements ProviderInterface | ||
{ | ||
/** @var string */ | ||
private $cacheDir; | ||
|
||
/** @var string */ | ||
private $namespace; | ||
|
||
#[CacheDir('cacheDir')] | ||
#[CacheNamespace('namespace')] | ||
public function __construct(string $cacheDir = '', string $namespace = '') | ||
{ | ||
$this->cacheDir = $cacheDir ?: sys_get_temp_dir(); | ||
$this->namespace = $namespace; | ||
} | ||
|
||
public function get(): AbstractAdapter | ||
{ | ||
return ApcuAdapter::isSupported() ? | ||
new ApcuAdapter($this->namespace) : | ||
new FilesystemAdapter($this->namespace, 0, $this->cacheDir); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\PsrCacheModule; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Ray\Di\AbstractModule; | ||
use Ray\Di\Scope; | ||
use Ray\PsrCacheModule\Annotation\Local; | ||
use Ray\PsrCacheModule\Annotation\Shared; | ||
|
||
final class Psr6LocalCacheModule extends AbstractModule | ||
{ | ||
protected function configure(): void | ||
{ | ||
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->toProvider(LocalCacheProvider::class)->in(Scope::SINGLETON); | ||
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toProvider(LocalCacheProvider::class)->in(Scope::SINGLETON); | ||
} | ||
} |
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