-
-
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.
- [x] php-cs-fixer - [x] Documentation - [x] psalm
- Loading branch information
1 parent
e645565
commit 7656a53
Showing
35 changed files
with
2,019 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
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,66 @@ | ||
--- | ||
title: Cache - Overview | ||
--- | ||
|
||
Used for cache stuff | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require sonsofphp/cache | ||
``` | ||
|
||
### PSR-16 - Simple Cache | ||
|
||
To add support for PSR-16 Simple Cache, require the PSR. | ||
|
||
```shell | ||
composer require psr/simple-cache | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\Cache\Adapter\ApcuAdapter; | ||
|
||
$pool = new ApcuAdapter(); | ||
``` | ||
|
||
All Adapters implement `Psr\Cache\CacheItemPoolInterface` | ||
|
||
### PSR-16 Simple Cache Wrapper | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\Cache\Adapter\ApcuAdapter; | ||
use SonsOfPHP\Component\Cache\SimpleCache; | ||
|
||
$pool = new ApcuAdapter(); | ||
$cache = new SimpleCache($pool); | ||
``` | ||
|
||
`SimpleCache` implements `Psr\SimpleCache\CacheInterface`. | ||
|
||
### Setting up a multi layer cache | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\Cache\Adapter\ArrayAdapter; | ||
use SonsOfPHP\Component\Cache\Adapter\ApcuAdapter; | ||
use SonsOfPHP\Component\Cache\Adapter\ChainAdapter; | ||
|
||
$pool = new ChainAdapter([ | ||
new ArrayAdapter(), | ||
new ApcuAdapter(), | ||
]); | ||
``` | ||
|
||
The `ChainAdapter` will read from all pools and return the first result it | ||
finds. So in the above example, if the cache item is not found in | ||
`ArrayAdapter`, it will look for it in the `ApcuAdapter` pool. | ||
|
||
The `ChainAdapter` will also write to and delete from all pools. |
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
vendor/ |
14 changes: 14 additions & 0 deletions
14
src/SonsOfPHP/Component/Cache/Adapter/AdapterInterface.php
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Cache\Adapter; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
|
||
/** | ||
* All Adapters MUST implement this interface | ||
* | ||
* @author Joshua Estes <joshua@sonsofphp.com> | ||
*/ | ||
interface AdapterInterface extends CacheItemPoolInterface {} |
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,122 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Cache\Adapter; | ||
|
||
use SonsOfPHP\Component\Cache\CacheItem; | ||
use Psr\Cache\CacheItemInterface; | ||
use SonsOfPHP\Component\Cache\Exception\CacheException; | ||
|
||
/** | ||
* @author Joshua Estes <joshua@sonsofphp.com> | ||
*/ | ||
class ApcuAdapter implements AdapterInterface | ||
{ | ||
private array $deferred = []; | ||
|
||
public function __construct() | ||
{ | ||
if (!extension_loaded('apcu') || !filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOL) || false === apcu_enabled()) { | ||
throw new CacheException('APCu extension is required.'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem(string $key): CacheItemInterface | ||
{ | ||
if ($this->hasItem($key)) { | ||
return (new CacheItem($key, true))->set(apcu_fetch($key)); | ||
} | ||
|
||
return new CacheItem($key, false); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems(array $keys = []): iterable | ||
{ | ||
foreach ($keys as $key) { | ||
yield $key => $this->getItem($key); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasItem(string $key): bool | ||
{ | ||
CacheItem::validateKey($key); | ||
|
||
return apcu_exists($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear(): bool | ||
{ | ||
return apcu_clear_cache(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItem(string $key): bool | ||
{ | ||
CacheItem::validateKey($key); | ||
|
||
return apcu_delete($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItems(array $keys): bool | ||
{ | ||
$ret = true; | ||
foreach ($keys as $key) { | ||
if (!$this->deleteItem($key)) { | ||
$ret = false; | ||
} | ||
} | ||
|
||
return $ret; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(CacheItemInterface $item): bool | ||
{ | ||
$this->saveDeferred($item); | ||
|
||
return $this->commit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item): bool | ||
{ | ||
$this->deferred[$item->getKey()] = $item; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit(): bool | ||
{ | ||
foreach ($this->deferred as $key => $item) { | ||
apcu_store($key, $item->get(), 0); | ||
} | ||
$this->deferred = []; | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.