Skip to content

Added VaryGenerator #36

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 9 commits into from
Apr 5, 2017
Merged

Added VaryGenerator #36

merged 9 commits into from
Apr 5, 2017

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Mar 31, 2017

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #30
Documentation
License MIT

What's in this PR?

This is an other generator. This PR is blocked by #35

Why?

This generator will be used by KNP Github API. Related to KnpLabs/php-github-api#557

@sagikazarmark sagikazarmark changed the title Added VeryGenerator Added VaryGenerator Mar 31, 2017
*
* @author Tobias Nyholm <[email protected]>
*/
class VaryGenerator 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.

Should probably be final

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the review.
I cannot make it final because PHP spec need to extend this class to be able to run the tests.

@Nyholm
Copy link
Member Author

Nyholm commented Apr 3, 2017

Reading @dbu's ticket #31, this might be the wrong approach. Or at least redundant. One could argue that we should use ClientKeyGenerator instead.

@dbu
Copy link
Contributor

dbu commented Apr 3, 2017

not sure about this one. the "proper" use case would be to work around a broken server that is not sending the correct vary header for its content. as i understand, you want it to use the github api which likely sends correct headers.

i think what i don't like about VaryGenerator that this is suggesting a relation with the Vary response header, which is not the case. could we call it HeaderHashGenerator? building the hash on Cookie and Authorization sounds ok. the proxy mode of the cache plugin could then use this generator by default.

that we should use ClientKeyGenerator instead.

i think for the client mode, we don't need to differentiate on credentials, as its assuming we send requests for the same client, and we cache things with the private cache-control. the problem comes when you are in a multiple users mode, where the http client acts as some sort of proxy between the user and the service that is cached.

@Nyholm
Copy link
Member Author

Nyholm commented Apr 3, 2017

Thank you. I've renamed the client.

i think for the client mode, we don't need to differentiate on credentials

Don't we? How would we solve the Gitbub issue?

// Request 1
$gitbub->setAuthenticationToken('foo');
$response = $github->me(); // MISS
// This will be stored in PSR6 cache

// Request 2, 2 days later
$gitbub->setAuthenticationToken('bar');
$response = $github->me(); // HIT
// This will fetch the cached response. 
// Github servers will never see this request. 

Should this be considered "Client mode" or "Server mode"?

@dbu
Copy link
Contributor

dbu commented Apr 4, 2017

to me, that would be server mode. but i realize that the naming could be confusing... my idea was that client mode would set up things like when you use a web browser, you are one single user and don't change authentication for example.

do you have an idea for better names than client and server? single and shared maybe?

@Nyholm
Copy link
Member Author

Nyholm commented Apr 4, 2017

You are correct. This is server mode. We need some good docs for this.

Do we want to merge a generator like this now, or do we just one the ServerCacheGenerator and ClientCacheGenerator?

@dbu
Copy link
Contributor

dbu commented Apr 5, 2017

i actually quite like my idea to move from the words client and server to single and shared. it might be more intuitive what this is about, or at least avoid assumptions that people get when we talk about server and client. then this could be a SharedCacheKeyGenerator and default to differentiate on cookie and authentication header. wdyt?

@Nyholm
Copy link
Member Author

Nyholm commented Apr 5, 2017

I like it. I'll make some updates now

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.

just a nitpick on the phpdoc, otherwise as per our slack discussion i think this is useful to have.

{
$concatenatedHeaders = [];
foreach ($this->headerNames as $headerName) {
$concatenatedHeaders[] = sprintf(' %s:"%s"', $headerName, $request->getHeaderLine($headerName));
Copy link
Contributor

Choose a reason for hiding this comment

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

calling getHeaderLine for a header that is not defined simply returns an empty string. just double-checked in the psr-7 spec. so this is fine

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

use Psr\Http\Message\RequestInterface;

/**
* Generate a cache key by specify what headers you want to vary on.
Copy link
Contributor

Choose a reason for hiding this comment

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

i'd like to use a different word than "vary" here to avoid confusion with the Vary response header.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the review

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.

sorry, another thing. noticed that this generator is not constistent with the simple generator in not using the body.

$concatenatedHeaders[] = sprintf(' %s:"%s"', $headerName, $request->getHeaderLine($headerName));
}

return $request->getMethod().' '.$request->getUri().implode('', $concatenatedHeaders);
Copy link
Contributor

Choose a reason for hiding this comment

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

to be consistent with the simple generator, we should also add the body. we might just extend the simple generator and concat parent::generate with the headers

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe in cohesion over inheritance here. We should create some kind of chaining or use a separate class for chaining

Copy link
Contributor

Choose a reason for hiding this comment

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

we can also just add the body from simple generator. its just one line of code, i think we don't need to get too complicated with this. but we should have the body to not have inconsistent behaviour between the two generators

Copy link
Member Author

Choose a reason for hiding this comment

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

Okey, done.

We do not need the if(empty()) check because it was added in the SimpleGenerator because of BC.

@dbu dbu merged commit 4e60554 into php-http:master Apr 5, 2017
@dbu dbu removed the in progress label Apr 5, 2017
@dbu
Copy link
Contributor

dbu commented Apr 5, 2017

great, thanks a lot! can you update the doc for this as well please? and should we start using this in the plugin when in server mode?

@Nyholm
Copy link
Member Author

Nyholm commented Apr 5, 2017

great, thanks a lot! can you update the doc for this as well please?

Yes!

and should we start using this in the plugin when in server mode?

That would be a BC break, right?

@dbu
Copy link
Contributor

dbu commented Apr 5, 2017 via email

@dbu
Copy link
Contributor

dbu commented Apr 5, 2017 via email

@Nyholm
Copy link
Member Author

Nyholm commented Apr 5, 2017

👍

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