Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Aug 4, 2023
1 parent 5f9b22c commit 364f7ed
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
94 changes: 93 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,96 @@ to the "require" section of your composer.json.
Usage
-----

This extension allows integration with PSR-6 compatible cache for Yii1.
This extension allows integration with [PSR-6](https://www.php-fig.org/psr/psr-6/) compatible cache for Yii1.
It provides several instruments for that. Please choose the one suitable for your particular needs.


### Wrap PSR cache pool into Yii cache <span id="wrap-psr-cache-pool-into-yii-cache"></span>

The most common use case for PSR caceh involvement into Yii application is usage of 3rd party cache library.
This can be achieved using `\yii1tech\psr\cache\Cache` as Yii cache component.

Application configuration example:

```php
<?php

return [
'components' => [
'cache' => [
'class' => \yii1tech\psr\cache\Cache::class,
'psrCachePool' => function () {
// ...
return new ExamplePsrCachePool(); // instantiate 3rd party cache library
},
],
// ...
],
// ...
];
```


### Wrap Yii cache into PSR cache pool <span id="wrap-yii-cache-into-psr-cache-pool"></span>

There is another use case related to PSR cache besides bootstrapping eternal cache storage.
Sometimes 3rd party libraries may require PSR cache pool instance to be passed to them in order to function.
`\Psr\Cache\CacheItemPoolInterface` allows wrapping standard Yii cache component into a PSR compatible cache pool.

Application configuration example:

```php
<?php

return [
'components' => [
'cache' => [
'class' => \CMemCache::class,
'servers' => [
// ...
],
],
\Psr\Cache\CacheItemPoolInterface::class => [
'class' => \yii1tech\psr\cache\CacheItemPool::class,
'cache' => 'cache',
],
// ...
],
// ...
];
```

Usage example:

```php
<?php

use Psr\Cache\CacheItemPoolInterface;

function getCategoriesCount()
{
/** @var CacheItemPoolInterface $pool */
$pool = Yii::app()->getComponent(CacheItemPoolInterface::class);

$item = $pool->getItem('categories-count');
if ($item->isHit()) {
return $item->get();
}

$value = Category::model()->count();

$item->set($value)
->expiresAfter(DateInterval::createFromDateString('1 hour'));

$pool->save($item);

return $value;
}
```


### Simplified interface <span id="simplified-interface"></span>


### Using cache tags <span id="using-cache-tags"></span>

23 changes: 23 additions & 0 deletions src/CacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
use Yii;

/**
* CacheItemPool allows wrapping standard Yii cache component into a PSR compatible cache pool.
*
* Application configuration example:
*
* ```php
* return [
* 'components' => [
* 'cache' => [
* 'class' => \CMemCache::class,
* 'servers' => [
* // ...
* ],
* ],
* \Psr\Cache\CacheItemPoolInterface::class => [
* 'class' => \yii1tech\psr\cache\CacheItemPool::class,
* 'cache' => 'cache',
* ],
* // ...
* ],
* // ...
* ];
* ```
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
Expand Down

0 comments on commit 364f7ed

Please sign in to comment.