|
5 | 5 | [](https://styleci.io/repos/175029173)
|
6 | 6 | [](https://packagist.org/packages/brightfish/caching-guzzle)
|
7 | 7 |
|
8 |
| -Simple caching middleware for Guzzle, works well with Laravel or with any cache system |
9 |
| -implementing the PSR-16 caching interface. |
| 8 | +Simple caching middleware for [Guzzle](https://github.com/guzzle/guzzle/), works well with [Laravel](https://github.com/laravel) or with any cache system |
| 9 | +implementing the [PSR-16 caching interface](https://www.php-fig.org/psr/psr-16/). |
10 | 10 |
|
11 | 11 | ## Installation
|
12 | 12 | ```
|
13 | 13 | composer require brightfish/caching-guzzle
|
14 | 14 | ```
|
15 | 15 |
|
16 |
| -## Using the middleware |
| 16 | +## Usage |
| 17 | +The registration of the caching middleware follows [Guzzle's documentation](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#handlers). |
| 18 | + |
17 | 19 | ```
|
18 | 20 | use GuzzleHttp\Client;
|
19 | 21 | use GuzzleHttp\HandlerStack;
|
20 | 22 | use Brightfish\CachingGuzzle\Middleware;
|
21 | 23 |
|
| 24 | +/** @var \Psr\SimpleCache\CacheInterface $store */ |
22 | 25 | $store = app('cache')->store('database'); // Laravel
|
23 |
| -$handler = new Middleware($store, 3600); |
| 26 | +
|
| 27 | +$handler = new Middleware($store); |
| 28 | +
|
24 | 29 | $stack = HandlerStack::create();
|
| 30 | +
|
25 | 31 | $stack->push($handler);
|
| 32 | +
|
26 | 33 | $client = new Client([
|
27 | 34 | 'handler' => $stack,
|
28 | 35 | 'base_uri' => 'https://example.org/api/'
|
29 | 36 | ]);
|
30 | 37 | ```
|
31 | 38 |
|
32 |
| -## Making requests and retrieving from cache |
| 39 | +### Instantiation parameters |
| 40 | +Instantiating the middleware takes 3 parameters: `new Middleware($store, $ttl = 60, $log = true)`, where only `$store`, a `SimpleCache` implementation is required. `$ttl` is the default cache duration, which can be overridden by each request. And finally, if `$log` is true, cache hits will be written to Laravel's log or the `error_log` defined by PHP (see [source](https://github.com/brightfish-be/caching-guzzle/blob/c0e96ae157b4e17363eb76ee5996995fbf0bd4a5/src/Middleware.php#L168)). |
| 41 | + |
| 42 | + |
| 43 | +## Making requests |
| 44 | + |
| 45 | +**Available options:** |
| 46 | + |
| 47 | +| Option | Type | Default | Description | |
| 48 | +|:-------|------|---------|:------------| |
| 49 | +|`cache` | bool | `true` | Completely disable the cache for this request | |
| 50 | +|`cache_anew` | bool | `false` | Bypass the cache and replace it with the new response | |
| 51 | +|`cache_ttl` | int | `60` | Cache duration in seconds, use `-1` to cache forever | |
| 52 | +|`cache_key` | string | `true` | Cache key to override the default one based on the request URI (see [Cache retrieval](https://github.com/brightfish-be/caching-guzzle#cache-retrieval)) | |
| 53 | + |
| 54 | +### Cache the response for 60s (default) |
33 | 55 | ```
|
34 |
| -# This response will be cached for 60s (same as default). |
35 | 56 | $response_1 = $client->get('resource', [
|
36 | 57 | 'cache_ttl' => 60
|
37 | 58 | ]);
|
38 |
| -
|
39 |
| -# This response will not be cached. |
| 59 | +``` |
| 60 | +### Request anew and update the cache |
| 61 | +``` |
| 62 | +$response_3 = $client->post('resource/84', [ |
| 63 | + 'cache_anew' => false |
| 64 | +]); |
| 65 | +``` |
| 66 | +### Disable caching |
| 67 | +``` |
40 | 68 | $response_2 = $client->post('resource/84', [
|
41 | 69 | 'cache' => false
|
42 | 70 | ]);
|
43 |
| -
|
44 |
| -# This response will be cached forever with a custom key. |
45 |
| -$response_3 = $client->post('resource/84', [ |
| 71 | +``` |
| 72 | +### Cache forever with a custom key |
| 73 | +``` |
| 74 | +$response_4 = $client->post('resource/84', [ |
46 | 75 | 'cache_key' => 'my-key',
|
47 | 76 | 'cache_ttl' => -1
|
48 | 77 | ]);
|
| 78 | +``` |
| 79 | +If `cache_ttl` is set to `0` the response will not be cached, but, contrary to `'cache' => false`, it may be retrieved from it. |
49 | 80 |
|
| 81 | +## Cache retrieval |
| 82 | +``` |
50 | 83 | # Get response_1 from cache.
|
51 | 84 | $cached_response_1 = $store->get('//example.org/api/resource');
|
52 | 85 |
|
53 |
| -# Get response_3 from cache. |
54 |
| -$cached_response_3 = $store->get('my-key'); |
| 86 | +# Get response_4 from cache. |
| 87 | +$cached_response_4 = $store->get('my-key'); |
55 | 88 | ```
|
56 | 89 |
|
57 | 90 | ## Using the wrapper
|
58 |
| -Instead of manually configuring the Guzzle client and the caching middleware, it is also possible |
59 |
| -to instantiate the Client class provided in this package. This way, the binding of the middleware is done for you. |
60 |
| -``` |
61 |
| -use Brightfish\CachingGuzzle\Client; |
62 |
| -
|
63 |
| -$client = new Client($psrCompatibleCache, [ |
64 |
| - 'cache_ttl' => 3600, |
65 |
| - 'cache_log' => false, |
66 |
| - 'base_uri' => 'https://example.org/api/' |
67 |
| -]); |
68 |
| -``` |
69 |
| - |
70 |
| -## Available options |
71 |
| - |
72 |
| -### Per request |
73 |
| - |
74 |
| -- `$cache` (bool): whether to disable the cache for this specific request. |
75 |
| -- `$cache_ttl` (int): cache duration in seconds for this response, use `-1` to cache forever. |
76 |
| -- `$cache_key` (string): custom cache key to override the default one based on the request URI. |
77 |
| - |
78 |
| -``` |
79 |
| -$response_1 = $client->get('resource', [ |
80 |
| - 'cache' => false, |
81 |
| - 'cache_ttl' => 3600 |
82 |
| -]); |
83 |
| -``` |
84 |
| - |
85 |
| -### When instantiating the middleware |
86 |
| - |
87 |
| -- `$cache` (\Psr\SimpleCache\CacheInterface): cache handler implementation. |
88 |
| -- `$ttl` (int): default cache duration in seconds [default: 60]. |
89 |
| -- `$log` (bool): whether to log the cache requests [default: false]. |
| 91 | +Instead of manually configuring Guzzle's client and the caching middleware, it is also possible to instantiate the `Client` class provided by this package. This way, the binding of the middleware is done for you. |
90 | 92 |
|
91 | 93 | ```
|
92 |
| -$handler = new CacheMiddleware($cache, $ttl, $log); |
93 |
| -``` |
94 |
| - |
95 |
| -### When instantiating the wrapper, pass options along with the Guzzle ones |
| 94 | +use Brightfish\CachingGuzzle\Client; |
96 | 95 |
|
97 |
| -- `$cache` (\Psr\SimpleCache\CacheInterface): cache handler implementation. |
98 |
| -- `$cache_ttl` (int): default cache duration in seconds [default: 60]. |
99 |
| -- `$cache_log` (bool): whether to log the cache requests [default: false]. |
| 96 | +/** @var \Psr\SimpleCache\CacheInterface $store */ |
| 97 | +$psrCompatibleCache = new Cache(); |
100 | 98 |
|
101 |
| -``` |
102 |
| -$client = new Client($cache, [ |
103 |
| - 'cache_ttl' => 12345, |
104 |
| - 'cache_log' => true, |
| 99 | +$client = new Client($psrCompatibleCache, [ |
| 100 | + 'cache_ttl' => 60, // default cache duration |
| 101 | + 'cache_log' => false, // log the cache hits |
| 102 | + // Guzzle options: |
105 | 103 | 'base_uri' => 'https://example.org/api/'
|
| 104 | + // ... |
106 | 105 | ]);
|
107 | 106 | ```
|
108 | 107 |
|
|
0 commit comments