Skip to content

Added CacheKeyGenerator #35

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

Merged
merged 10 commits into from
Apr 3, 2017
Merged

Added CacheKeyGenerator #35

merged 10 commits into from
Apr 3, 2017

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Mar 31, 2017

Introducing CacheKeyGenerator interface. This will make sure one can control how a cache key is generated.

This will fix #30

// Default will use RequestLineAndBodyGenerator
$cachePlugin = new CachePlugin($pool, $streamFactory);
$config = [
  'cache_key_generator' => new MyCustomKeyGenerator(), 
];

$cachePlugin = new CachePlugin($pool, $streamFactory, $config);

Also added a default generator
@Nyholm
Copy link
Member Author

Nyholm commented Mar 31, 2017

Im done with this PR now. It is ready for review.

PS: The commit "Support PSR-4" was a typo. I ment to write "Support PHP 5.4"

@Nyholm Nyholm mentioned this pull request Mar 31, 2017
@@ -338,12 +342,14 @@ private function configureOptions(OptionsResolver $resolver)
'hash_algo' => 'sha1',
'methods' => ['GET', 'HEAD'],
'respect_response_cache_directives' => ['no-cache', 'private', 'max-age', 'no-store'],
'cache_key_generator' => null,
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if I should use null here or if I should instanciate the default generator. Using null will make sure we do not instanceiate unneeded objects.

Copy link
Member

@sagikazarmark sagikazarmark left a comment

Choose a reason for hiding this comment

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

I had a few small comments, but looks ok. My fear is that we are putting too much logic here. Do you think the caching logic could be used elsewhere too? If so, should we extract it and move it to message for example?

$body = (string) $request->getBody();
if (!empty($body)) {
$body = ' '.$body;
if (null === $this->config['cache_key_generator']) {
Copy link
Member

Choose a reason for hiding this comment

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

I would move this check right after the option resolution. I don't see any benefit in timing here, this will be resolved sooner or later.

*
* @author Tobias Nyholm <[email protected]>
*/
class RequestLineAndBodyGenerator implements CacheKeyGenerator
Copy link
Member

Choose a reason for hiding this comment

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

Can we find a shorter name for this?

Copy link
Member Author

@Nyholm Nyholm Mar 31, 2017

Choose a reason for hiding this comment

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

Yes please! Any suggestions?

@@ -2,6 +2,7 @@

namespace spec\Http\Client\Common\Plugin;

use Http\Client\Common\Plugin\Generator\RequestLineAndBodyGenerator;
Copy link
Member

Choose a reason for hiding this comment

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

Hm, maybe put this under ...\Plugin\Cache\...

@Nyholm
Copy link
Member Author

Nyholm commented Mar 31, 2017

Thank you for the feedback. I've updated. I renamed the generator to "SimpleGenerator" since it uses an naive approach.

My fear is that we are putting too much logic here. Do you think the caching logic could be used elsewhere too? If so, should we extract it and move it to message for example?

The CachePlugin is pretty fat, yes. One could refactor it to more helper classes and strategies.

Im not sure it make sense to try to reuse the cache logic. Not at this point, because the code it too coupled to this specific use case. But I will not close that door forever.

If I recall correctly we decided to not have cache logic in message because we did not want to have so many external dependencies in php-http/message. But you may be referring to the HTTP specific parts... Again, not sure.

I think we can let this library grow and mature. It is an implementation (and not an abstraction like most of our other libraries) so it is fine to bump it to 2.x and 3.x when we see a need for it.

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

i like this and think its fine to put in here at least for now. lets improve the cache plugin and if we realize we need the cache key elsewhere we can always push it upstream in a BC way as the cache depends on client-common

@Nyholm
Copy link
Member Author

Nyholm commented Apr 3, 2017

Thank you for the reviews.

@dbu
Copy link
Contributor

dbu commented Apr 3, 2017

please add documentation for this. i created php-http/documentation#187 so we don't forget.

@Nyholm
Copy link
Member Author

Nyholm commented Apr 3, 2017

Thank you

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.

Customize cache key generation
3 participants