From 0b02640b4fec6c2ab33fb47e6b971dc69a801af4 Mon Sep 17 00:00:00 2001 From: Baptiste Lafontaine Date: Wed, 3 Oct 2018 09:52:33 +0200 Subject: [PATCH] Add some caching strategies explanation in readme Relates to #42 --- README.md | 64 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0e9daa29..3399c087 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,43 @@ $stack->push(new CacheMiddleware(), 'cache'); $client = new Client(['handler' => $stack]); ``` +# Caching strategies + +## Public and private +Theses two strategies automatically respects `Cache-Control` headers containing: +* `no-cache`, `no-store`: response won't be cached +* `max-age`: response will be cached for the corresponding time +It will also honor the `Expires` header will the corresponding TTL. + +The `PublicCacheStrategy` won't cache responses with `Cache-Control` header containing `private`. + +## Greedy +In some cases servers might send insufficient or no caching headers at all. +Using the greedy caching strategy allows defining an expiry TTL on your own while +disregarding any possibly present caching headers: +```php +[...] +use Kevinrob\GuzzleCache\KeyValueHttpHeader; +use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy; +use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; +use Doctrine\Common\Cache\FilesystemCache; + +[...] +// Greedy caching +$stack->push( + new CacheMiddleware( + new GreedyCacheStrategy( + new DoctrineCacheStorage( + new FilesystemCache('/tmp/') + ), + 1800, // the TTL in seconds + new KeyValueHttpHeader(['Authorization']) // Optionnal - specify the headers that can change the cache key + ) + ), + 'greedy-cache' +); +``` + # Examples ## Doctrine/Cache @@ -189,33 +226,6 @@ $stack->push( ); ``` -## Greedy caching -In some cases servers might send insufficient or no caching headers at all. -Using the greedy caching strategy allows defining an expiry TTL on your own while -disregarding any possibly present caching headers: -```php -[...] -use Kevinrob\GuzzleCache\KeyValueHttpHeader; -use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy; -use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; -use Doctrine\Common\Cache\FilesystemCache; - -[...] -// Greedy caching -$stack->push( - new CacheMiddleware( - new GreedyCacheStrategy( - new DoctrineCacheStorage( - new FilesystemCache('/tmp/') - ), - 1800, // the TTL in seconds - new KeyValueHttpHeader(['Authorization']) // Optionnal - specify the headers that can change the cache key - ) - ), - 'greedy-cache' -); -``` - ## Delegate caching Because your client may call different apps, on different domains, you may need to define which strategy is suitable to your requests.