Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GreedyCacheStrategy #38

Merged
merged 1 commit into from
Jan 22, 2016
Merged

Add GreedyCacheStrategy #38

merged 1 commit into from
Jan 22, 2016

Conversation

jeromegamez
Copy link
Contributor

First of all: thank you for this middleware - it's well designed, and it was a pleasure integrating it (and saved me a lot of time) ❤️.

A propos ":heart:": it breaks mine to propose an addition that is not RFC-compliant, but I think it could still be useful for certain edge cases.

In my case, I had to access

  • a remote resource which provided no cache control headers
  • an unreliable remote resource which would not be accessible from time to time

This is where the Greedy Cache Strategy came to life - it stores responses in the cache for a fixed amount of time, and I use it in a second middleware inside the HandlerStack:

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\ChainCache;
use Doctrine\Common\Cache\FilesystemCache;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;

$caches = [
    new ArrayCache(),
    new FilesystemCache('/tmp/'),
];

$stack = HandlerStack::create();

// The normal cache
$stack->push(new CacheMiddleware(
    new PrivateCacheStrategy(
        new DoctrineCacheStorage(new ChainCache($caches))
    )
), 'cache');

// Notice the ->before() instead of ->push().
// We want the greedy cache to have a lesser priority
$stack->before('cache', new CacheMiddleware(
    new GreedyCacheStrategy(
        new DoctrineCacheStorage(new ChainCache($caches)), $ttl = 600
    )
), 'greedy-cache');

$client = new Client(['handler' => $stack]);

echo $stack;

At least it adds a "Warning" header (see #24) 😄

Cheers!
:octocat: Jérôme

@Kevinrob
Copy link
Owner

Thank you for your interest. It's nice to see that this is useful to someone!

I think that is a good thing to have this strategy. Sometimes we don't have the choice about following RFC 😉 .
I will review your PR ASAP.

{
$response = $response->withAddedHeader(
'Warning',
'This response is cached although the response headers indicate not to do it!'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe here we can follow the RFC 7234 fromating? Like:
299 - "This response is cached although the response headers indicate not to do it!"
https://tools.ietf.org/html/rfc7234#section-5.5

What do you think @jeromegamez ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darnit, I just saw "Warning header" and thought that this was all - didn't recognize that the following section is a detailed specification :D.

I will change it as soon as I can, thanks for the heads up!

@Kevinrob
Copy link
Owner

For me it's all good! Thanks 👍
(except for the warning header, cf. my comment)

@jeromegamez
Copy link
Contributor Author

@Kevinrob Done! I shortened the warning message a bit, because if we are in the response headers, we shouldn't need to say "This response is…" :)

Kevinrob added a commit that referenced this pull request Jan 22, 2016
@Kevinrob Kevinrob merged commit 2e7a1b7 into Kevinrob:master Jan 22, 2016
@jeromegamez jeromegamez deleted the greedy-cache-strategy branch January 22, 2016 21:22
@jeromegamez
Copy link
Contributor Author

Thanks for merging!

@Kevinrob
Copy link
Owner

You're welcome! Thanks for sharing your code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants