Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Nov 7, 2023
1 parent a70a432 commit 0eb947d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Examples:
* [PR #59](https://github.com/SonsOfPHP/sonsofphp/pull/59) Added new HttpMessage component
* [PR #59](https://github.com/SonsOfPHP/sonsofphp/pull/59) Added new HttpFactory component
* [PR #70](https://github.com/SonsOfPHP/sonsofphp/pull/70) Added new Core contract
* [PR #112](https://github.com/SonsOfPHP/sonsofphp/pull/112) [Cache] Added new component

## [0.3.8]

Expand Down
55 changes: 55 additions & 0 deletions docs/components/cache/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,58 @@ Used for cache stuff
```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.
8 changes: 7 additions & 1 deletion src/SonsOfPHP/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public function __construct(
*/
public function getItem(string $key): CacheItemInterface
{
return $this->adapters[0]->getItem($key);
foreach ($this->adapters as $adapter) {
if ($adapter->hasItem($key)) {
return $adapter->getItem($key);
}
}

return new CacheItem($key, false);
}

/**
Expand Down

0 comments on commit 0eb947d

Please sign in to comment.