-
-
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.
- Loading branch information
1 parent
e89d747
commit b67565d
Showing
25 changed files
with
711 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,82 @@ | ||
--- | ||
title: Cache Adapters | ||
--- | ||
|
||
# Adapters | ||
|
||
All adapters implement the PSR-6 `\Psr\Cache\CacheItemPoolInterface` and can be used as | ||
standalone cache pools. | ||
|
||
## ApcuAdapter | ||
|
||
Requires the APCu extension is loaded and enabled. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Adapter\ApcuAdapter; | ||
|
||
$cache = new ApcuAdapter(); | ||
|
||
// You can set a default TTL in seconds and Marshaller as well. | ||
$cache = new ApcuAdapter(60, new CustomMarshaller()); | ||
``` | ||
|
||
## ArrayAdapter | ||
|
||
Stores cache items in an internal PHP array. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Adapter\ArrayAdapter; | ||
|
||
$cache = new ArrayAdapter(); | ||
``` | ||
|
||
## ChainAdapter | ||
|
||
The Chain Adapter will take one or more adapters. It will WRITE to all adapters | ||
and will READ from each adapter until it finds a hit and return that cache item. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Adapter\ApcuAdapter; | ||
use SonsOfPHP\Componenet\Cache\Adapter\ArrayAdapter; | ||
use SonsOfPHP\Componenet\Cache\Adapter\ChainAdapter; | ||
|
||
$cache = new ChainAdapter([ | ||
new ArrayAdapter(), | ||
new ApcuAdapter(), | ||
]); | ||
``` | ||
|
||
## FilesystemAdapter | ||
|
||
Stores cache files on disk | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Adapter\FilesystemAdapter; | ||
|
||
$cache = new FilesystemAdapter(); | ||
|
||
// You can configure the directory, default permissions, default ttl, and | ||
// marshaller | ||
$cache = new FilesystemAdapter('/path/to/cache', 0777, 60, new CustomMarshaller()); | ||
``` | ||
|
||
## NullAdapter | ||
|
||
Mainly used for testing, however you could have some checks in place and if | ||
those checks fail, you could fallback to this adapter. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Adapter\NullAdapter; | ||
|
||
$cache = new NullAdapter(); | ||
``` |
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,38 @@ | ||
--- | ||
title: Cache Marshallers | ||
--- | ||
|
||
# Marshallers | ||
|
||
Using marshallers will allow you to serialize the cache values differently. | ||
|
||
## SerializableMarshaller | ||
|
||
This marshaller will use php's native `serialize` and `unserialize` functions on | ||
the values. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Marshaller\SerializableMarshaller; | ||
|
||
$marshaller = new SerializableMarshaller(); | ||
``` | ||
|
||
## JsonMarshaller | ||
|
||
The json marshaller will use php's native `json_encode` and `json_decode` | ||
functions to serialize data. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Componenet\Cache\Marshaller\JsonMarshaller; | ||
|
||
$marshaller = new JsonMarshaller(); | ||
``` | ||
|
||
# Custom Marshaller | ||
|
||
You can create a custom marshaller. For example, you could create one the | ||
encrypts/decrypts the values. |
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,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Cache\Adapter; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use SonsOfPHP\Component\Cache\Marshaller\MarshallerInterface; | ||
use SonsOfPHP\Component\Cache\Marshaller\SerializableMarshaller; | ||
|
||
/** | ||
* @author Joshua Estes <joshua@sonsofphp.com> | ||
*/ | ||
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
protected array $deferred = []; | ||
|
||
public function __construct( | ||
protected int $defaultTTL = 0, | ||
protected ?MarshallerInterface $marshaller = null, | ||
) { | ||
if (!$this->marshaller instanceof MarshallerInterface) { | ||
$this->marshaller = new SerializableMarshaller(); | ||
} | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->commit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems(array $keys = []): iterable | ||
{ | ||
foreach ($keys as $key) { | ||
yield $key => $this->getItem($key); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItems(array $keys): bool | ||
{ | ||
$isOk = true; | ||
foreach ($keys as $key) { | ||
if (!$this->deleteItem($key)) { | ||
$this->logger?->debug(sprintf('Unable to delete key "%s".', $key)); | ||
$isOk = false; | ||
} | ||
} | ||
|
||
return $isOk; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item): bool | ||
{ | ||
$this->deferred[$item->getKey()] = $item; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit(): bool | ||
{ | ||
$isOk = true; | ||
|
||
foreach ($this->deferred as $item) { | ||
if (!$this->save($item)) { | ||
$this->logger?->debug(sprintf('Unable to save key "%s".', $item->getKey())); | ||
$isOk = false; | ||
} | ||
} | ||
|
||
$this->deferred = []; | ||
|
||
return $isOk; | ||
} | ||
} |
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
Oops, something went wrong.