Skip to content

Commit

Permalink
Add some caching strategies explanation in readme
Browse files Browse the repository at this point in the history
Relates to Kevinrob#42
  • Loading branch information
magnetik authored Oct 3, 2018
1 parent 6cdaad0 commit 0b02640
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 0b02640

Please sign in to comment.