Skip to content

Commit 0cdd868

Browse files
committed
docs: added cache section
1 parent ffee2a6 commit 0cdd868

1 file changed

Lines changed: 99 additions & 9 deletions

File tree

README.md

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,10 @@ class YourApi extends Api
446446
> The methods in this section are all public.
447447
> The purpose for that is to allow the end user to configure their own HTTP client, HTTP factories and plugins.
448448
449-
- [HTTP client and HTTP factories adapters](#http-client-and-http-factories-adapters)
449+
- [HTTP client and HTTP factory adapters](#http-client-and-http-factory-adapters)
450450
- [Plugin system](#plugin-system)
451451

452-
#### HTTP Client and HTTP Factories Adapters
452+
#### HTTP Client and HTTP Factory Adapters
453453

454454
By default, this library makes use of the [HTTPlug's Discovery](https://github.com/php-http/discovery) library.
455455
This means that it will automatically find and install a well-known PSR-18 client and PSR-17 factory implementation for you
@@ -461,16 +461,16 @@ This means that it will automatically find and install a well-known PSR-18 clien
461461
use ProgrammatorDev\Api\Builder\ClientBuilder;
462462

463463
new ClientBuilder(
464-
?ClientInterface $client = null,
465-
?RequestFactoryInterface $requestFactory = null,
466-
?StreamFactoryInterface $streamFactory = null
464+
?ClientInterface $client = null, // a PSR-18 client
465+
?RequestFactoryInterface $requestFactory = null, // a PSR-17 request factory
466+
?StreamFactoryInterface $streamFactory = null // a PSR-17 stream factory
467467
);
468468
```
469469

470470
```php
471471
use ProgrammatorDev\Api\Builder\ClientBuilder;
472472

473-
$this->setClientBuilder(ClientBuilder $clientBuilder): ClientBuilder;
473+
$this->setClientBuilder(ClientBuilder $clientBuilder): self;
474474
```
475475

476476
```php
@@ -484,6 +484,7 @@ If you don't want to rely on the discovery of implementations, you can set the o
484484
```php
485485
use ProgrammatorDev\Api\Api;
486486
use ProgrammatorDev\Api\Builder\ClientBuilder;
487+
use Http\Client\Common\EmulatedHttpAsyncClient
487488
use Symfony\Component\HttpClient\Psr18Client;
488489
use Nyholm\Psr7\Factory\Psr17Factory;
489490

@@ -496,7 +497,9 @@ class YourApi extends Api
496497
$client = new Psr18Client();
497498
$requestFactory = $streamFactory = new Psr17Factory();
498499

499-
$this->setClientBuilder(new ClientBuilder($client, $requestFactory, $streamFactory));
500+
$this->setClientBuilder(
501+
new ClientBuilder($client, $requestFactory, $streamFactory)
502+
);
500503
}
501504
}
502505
```
@@ -509,7 +512,9 @@ $api = new YourApi();
509512
$client = new Psr18Client();
510513
$requestFactory = $streamFactory = new Psr17Factory();
511514

512-
$api->setClientBuilder(new ClientBuilder($client, $requestFactory, $streamFactory));
515+
$api->setClientBuilder(
516+
new ClientBuilder($client, $requestFactory, $streamFactory)
517+
);
513518
```
514519

515520
#### Plugin System
@@ -556,7 +561,7 @@ class YourApi extends Api
556561

557562
// if a request fails, it will retry at least 3 times
558563
// priority is 12 to execute the plugin between the cache and logger plugins
559-
// (check the above plugin order for more information)
564+
// (check the above plugin order list for more information)
560565
$this->getClientBuilder()->addPlugin(
561566
plugin: new RetryPlugin(['retries' => 3])
562567
priority: 12
@@ -576,6 +581,91 @@ $api->getClientBuilder()->addPlugin(
576581
);
577582
```
578583

584+
### Cache (PSR-6)
585+
586+
> [!IMPORTANT]
587+
> The methods in this section are all public.
588+
> The purpose for that is to allow the end user to configure their own cache adapter.
589+
590+
This library allows configuring the cache-layer of the client for making API requests.
591+
It uses a standard PSR-6 implementation and provides methods to fine-tune how HTTP caching behaves:
592+
- [PSR-6 compatible implementations](https://packagist.org/providers/psr/cache-implementation)
593+
594+
```php
595+
use ProgrammatorDev\Api\Builder\CacheBuilder;
596+
use Psr\Cache\CacheItemPoolInterface;
597+
598+
new CacheBuilder(
599+
CacheItemPoolInterface $pool, // a PSR-6 cache adapter
600+
?int $ttl = 60, // default lifetime (in seconds) of cache items
601+
$methods = ['GET', 'HEAD'], // An array of HTTP methods for which caching should be applied
602+
$responseCacheDirectives = ['no-cache', 'max-age'] // An array of cache directives to be compared with the headers of the HTTP response, in order to determine cacheability
603+
);
604+
```
605+
606+
```php
607+
use ProgrammatorDev\Api\Builder\CacheBuilder;
608+
609+
$this->setCacheBuilder(CacheBuilder $cacheBuilder): self;
610+
```
611+
612+
```php
613+
use ProgrammatorDev\Api\Builder\CacheBuilder;
614+
615+
$this->getCacheBuilder(): CacheBuilder;
616+
```
617+
618+
For example, if you wanted to set a file-based cache adapter:
619+
620+
```php
621+
use ProgrammatorDev\Api\Api;
622+
use ProgrammatorDev\Api\Builder\CacheBuilder;
623+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
624+
625+
class YourApi extends Api
626+
{
627+
public function __construct()
628+
{
629+
// ...
630+
631+
$pool = new FilesystemAdapter();
632+
633+
// file-based cache adapter with a 1-hour default cache lifetime
634+
$this->setClientBuilder(
635+
new CacheBuilder($pool, 3600)
636+
);
637+
}
638+
639+
public function getPosts(): string
640+
{
641+
// you can change the lifetime (and all other parameters)
642+
// for this specific endpoint
643+
$this->getCacheBuilder()->setTtl(600);
644+
645+
return $this->request(
646+
method: 'GET',
647+
path: '/posts'
648+
);
649+
}
650+
}
651+
```
652+
653+
The same for the end user:
654+
655+
```php
656+
$api = new YourApi();
657+
658+
$pool = new FilesystemAdapter();
659+
660+
$api->setCacheBuilder(
661+
new CacheBuilder($pool, 3600)
662+
);
663+
```
664+
665+
## Cookbook
666+
667+
TODO
668+
579669
## Contributing
580670

581671
Any form of contribution to improve this library (including requests) will be welcome and appreciated.

0 commit comments

Comments
 (0)