Repository navigation
Commit 2bcaeb9
committed
Update CachedHttpClient.php
I used your implementation of the filesystem cache.
- Delete all files under the cache directory `github-api-cache`.
- Set properly your authentication method (I used token).
- Run the following code once after a proper Client instantiation with cache:
```
$commits_article_1 = ResponseMediator::getContent($client->getHttpClient()->get("repos/jrean/askjong.com/commits?sha=master&path=articles/hello-world.md"));
$rateLimit = ResponseMediator::getContent($client->getHttpClient()->get("rate_limit"));
var_dump($rateLimit);
$commits_article_2 = ResponseMediator::getContent($client->getHttpClient()->get("repos/jrean/askjong.com/commits?sha=master&path=articles/this-is-a-first-article.md"));
$rateLimit = ResponseMediator::getContent($client->getHttpClient()->get("rate_limit"));
var_dump($rateLimit);
```
4 requests against Github Api:
- 1 query to fetch the commits for the `articles/hello-world.md` file. (`Guzzle\Http\Message\Response` status code is 200)
- 1 query to fetch the commits for the `articles/this-is-a-first-article.md` file. (`Guzzle\Http\Message\Response` status code is 200)
- 2 queries to fetch the rate_limit (doesn't count again the rate limit). (`Guzzle\Http\Message\Response` status code is 200)
Once this code has run the cache directory `github-api-cache` will have 6 files (3 for each queries including the `rate_limit` query + 3 `.etag` files)
**The `rate_limit` should / must decrease by 2** (a call to `rate_limit` doesn't count against Github).
--
- Run the same code again.
4 requests against Github Api:
- 1 query to fetch the commits for the `articles/hello-world.md` file. (`Guzzle\Http\Message\Response` status code is 304, cache is used)
- 1 query to fetch the commits for the `articles/this-is-a-first-article.md` file. (`Guzzle\Http\Message\Response` status code is 304, cache is user)
- 2 queries to fetch the rate_limit (doesn't count again the rate limit). (`Guzzle\Http\Message\Response` status code is 200)
**The `rate_limit` don't move** (a call to `rate_limit` doesn't count against Github).
--
- Run the following code which use your wrapping methods against Github Api (Notice we are requesting the same informations):
**It should use the cache but it will not.** (I explain why just after).
```
$commits_article_1 = $client->api('repo')->commits()->all('jrean', 'askjong.com', array('sha' => 'master', 'path' => 'articles/hello-world.md'));
$rateLimit = ResponseMediator::getContent($client->getHttpClient()->get("rate_limit"));
var_dump($rateLimit);
$commits_article_2 = $client->api('repo')->commits()->all('jrean', 'askjong.com', array('sha' => 'master', 'path' => 'articles/this-is-a-first-article.md'));
$rateLimit = ResponseMediator::getContent($client->getHttpClient()->get("rate_limit"));
var_dump($rateLimit);
```
4 requests against Github Api:
- 1 query to fetch the commits for the `articles/hello-world.md` file. (`Guzzle\Http\Message\Response` status code is 200)
- 1 query to fetch the commits for the `articles/this-is-a-first-article.md` file. (`Guzzle\Http\Message\Response` status code is 200)
- 2 queries to fetch the rate_limit (doesn't count again the rate limit). (`Guzzle\Http\Message\Response` status code is 200)
Once this code has run the cache directory `github-api-cache` will have new files...
**The `rate_limit` decreases by 2** (a call to `rate_limit` doesn't count against Github). It should not decrease because we should use the cache!
--
- Run the same code again.
4 requests against Github Api:
- 1 query to fetch the commits for the `articles/hello-world.md` file. (`Guzzle\Http\Message\Response` status code is 200)
- 1 query to fetch the commits for the `articles/this-is-a-first-article.md` file. (`Guzzle\Http\Message\Response` status code is 200)
- 2 queries to fetch the rate_limit (doesn't count again the rate limit). (`Guzzle\Http\Message\Response` status code is 200)
**The `rate_limit` decreases by 2 again** (a call to `rate_limit` doesn't count against Github). It should not decrease because we should use the cache!
- Run the same code again and it will still decrease your `rate_limit`...
--
# Solution
This issue comes from the use of `$path`:
`protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())`
Then here:
`if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {`
And:
`if ($etag = $this->getCache()->getETag($path)) {`
`$path` doesn't take care of the `$options` array which contains the sub array for the `query` parameters!
So each time `getModifiedSince($path)` and `getETag($path)` will work with an incomplete path because it doesn't know query parameters (if they exists). In that exemple `$path` value will always be `repos/jrean/askjong.com/commits` and it should be `repos/jrean/askjong.com/commits?sha=master&path=articles/hello-world.md`.
See my code modifications to fix the problem.
(I didn't use `$request->getQuery()` because it returns query parameters but encoded and it is not acceptable because when we want to construct our own queries like `repos/jrean/askjong.com/commits?sha=master&path=articles/hello-world.md` we won't encode parameters)
By applying this patch we can now use the cache as it should be :)
I started this issue a few days ago.
KnpLabs#2621 parent a03bae7 commit 2bcaeb9
1 file changed
Lines changed: 40 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
29 | 36 | | |
30 | 37 | | |
31 | 38 | | |
| |||
51 | 58 | | |
52 | 59 | | |
53 | 60 | | |
| 61 | + | |
| 62 | + | |
54 | 63 | | |
55 | 64 | | |
56 | 65 | | |
57 | | - | |
| 66 | + | |
58 | 67 | | |
59 | 68 | | |
60 | 69 | | |
61 | 70 | | |
62 | 71 | | |
63 | | - | |
| 72 | + | |
64 | 73 | | |
65 | 74 | | |
66 | 75 | | |
| |||
74 | 83 | | |
75 | 84 | | |
76 | 85 | | |
77 | | - | |
| 86 | + | |
78 | 87 | | |
79 | 88 | | |
80 | 89 | | |
| |||
83 | 92 | | |
84 | 93 | | |
85 | 94 | | |
86 | | - | |
| 95 | + | |
87 | 96 | | |
88 | 97 | | |
89 | 98 | | |
| |||
105 | 114 | | |
106 | 115 | | |
107 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
108 | 144 | | |
0 commit comments