-
Notifications
You must be signed in to change notification settings - Fork 16
Adding response mutator. #48
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This question has came up before. I do not really see the value of it. But this is a good implementation and I do not see why we should not allow it.
src/CachePlugin.php
Outdated
@@ -357,6 +367,7 @@ private function configureOptions(OptionsResolver $resolver) | |||
|
|||
return empty($matches); | |||
}); | |||
$resolver->setAllowedTypes('response_mutator', ['null', 'Http\Client\Common\Plugin\Cache\Mutator\ResponseMutator']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use the ::class
constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but I was following the style of the other lines in this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets use it here and we fix it elsewhere.
(but as saying below, i think this should be an array, while we are at it.
/** | ||
* @param string $headerName | ||
*/ | ||
public function __construct($headerName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding a default value here? I guess most people will be happy with any default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I'll update with a sane default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for this one! the architecture looks clean to me.
a way to achieve this with existing code features would be to put a plugin into the chain after the cache, and one before the cache. the after adds the MISS value, the one before checks if X-Cache is set and if not, its a HIT. but i agree that that would be quite an incidental way of doing things.
are there any uses cases other than adding an X-Cache: HIT/MISS header? i think either we should find reasons why additional flexibility is needed, or simply have a flag (or header name config) to make the cache plugin itself add the hit or miss header. other plugins could then look at that header to know if they need to further change the response, if you need to change a response based on whether it comes from the cache. if such a scenario exists at all... maybe its better to just have the header, and if somebody has a really special need, they can always implement their own cache plugin based on this repo?
src/CachePlugin.php
Outdated
private function handleResponseMutator(RequestInterface $request, ResponseInterface $response, $cacheHit) | ||
{ | ||
if (null !== $this->config['response_mutator']) { | ||
$response = $this->config['response_mutator']->mutate($request, $response, $cacheHit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this should be a list of mutators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, will update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though, not totally familliar with the resolver... is there any better way of stating that the array must be of generic type CacheListener
? Right now I just have $resolver->setAllowedTypes('cache_listeners', ['array']);
.
src/CachePlugin.php
Outdated
@@ -357,6 +367,7 @@ private function configureOptions(OptionsResolver $resolver) | |||
|
|||
return empty($matches); | |||
}); | |||
$resolver->setAllowedTypes('response_mutator', ['null', 'Http\Client\Common\Plugin\Cache\Mutator\ResponseMutator']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets use it here and we fix it elsewhere.
(but as saying below, i think this should be an array, while we are at it.
/** | ||
* @param string $headerName | ||
*/ | ||
public function __construct($headerName = 'X-From-Php-Http-Cache') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a semi-official way to mark cache hits is to use "X-Cache: HIT" (resp. MISS). can we find a different example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, updated.
* | ||
* @return string | ||
*/ | ||
public function mutate(RequestInterface $request, ResponseInterface $response, $cacheHit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any more information that we could pass in instead of the boolean? like the $cacheItem or such? then this could be used to extract additional debugging information if wanted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, I'm going to keep the cacheHit
parameter as well, so the consumer doesn't have to reverse-engineer the logic regarding expiry. Checking cacheItem->isHit()
won't be sufficient.
* | ||
* @author Iain Connor <[email protected]> | ||
*/ | ||
interface ResponseMutator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am not happy with this name. every plugin has the possibility to change a response. this thing is specifically about the cache. but unfortunately i don't have a great name either. CacheListener
is not too good, but would be more specific to what this does. can we be even more specific?
the class doc could be "XXX is called by the cache plugin with information on the cache status to change information on the response based on whether it was a cache hit or a miss."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't think of anything significantly better. Will rename this for the time being and see how it feels.
The other use-case that my project is currently adding is logging of the hit/miss rate. I realize I could probably accomplish that in another way (by adding the header and then checking for the presence of that header, for example), but adding it the same plugin seemed clearer. |
@Nyholm want another look? also, should we support this in the symfony bundle too? |
ResponseMutator documentation for php-http/cache-plugin#48
Saw the documentation get merged in. Anything else needed for this code? |
ups, somehow missed to merge this. the changelog is still open. can you please update composer.json to make the branch alias 1.6 and add a changelog entry for 1.6.0 that says this was added (short, but say what its for)? |
Sure thing; done. |
yay, thanks a lot! |
What's in this PR?
Adds an optional mutator callback so you can mutate the response depending whether it was from cache or not.
Why?
Right now, there's no way to tell whether a response came from the cache or not, other than reverse-engineering the cache logic. It may be useful for debugging or tweaking your cache to know this, so this PR provides an optional way of retrieving this information. An
AddHeaderResponseMutator
is provided as an example way of adding a custom header to the response identifying whether it came from cache or not.Example Usage
Checklist
To Do