{"action":"created","comment":{"url":"https://api.github.com/repos/php-http/plugins/pulls/comments/46046555","id":46046555,"diff_hunk":"@@ -0,0 +1,162 @@\n+<?php\n+\n+namespace Http\\Client\\Plugin;\n+\n+use Psr\\Cache\\CacheItemInterface;\n+use Psr\\Cache\\CacheItemPoolInterface;\n+use Psr\\Http\\Message\\RequestInterface;\n+use Psr\\Http\\Message\\ResponseInterface;\n+\n+/**\n+ * Allow for caching a response.\n+ *\n+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>\n+ */\n+class CachePlugin implements Plugin\n+{\n+    /**\n+     * @var CacheItemPoolInterface\n+     */\n+    private $pool;\n+\n+    /**\n+     * Default time to store object in cache. This value is used if CachePlugin::respectCacheHeaders is false or\n+     * if cache headers are missing.\n+     *\n+     * @var int\n+     */\n+    private $defaultTtl;\n+\n+    /**\n+     * Look at the cache headers to know how long this response is going to be cached.\n+     *\n+     * @var bool\n+     */\n+    private $respectCacheHeaders;\n+\n+    /**\n+     * @param CacheItemPoolInterface $pool\n+     * @param int                    $defaultTtl\n+     * @param bool                   $respectCacheHeaders\n+     */\n+    public function __construct(CacheItemPoolInterface $pool, $defaultTtl = 60, $respectCacheHeaders = true)\n+    {\n+        $this->pool = $pool;\n+        $this->defaultTtl = $defaultTtl;\n+        $this->respectCacheHeaders = $respectCacheHeaders;\n+    }\n+\n+    /**\n+     * @param RequestInterface $request\n+     * @param callable         $next\n+     * @param callable         $first\n+     *\n+     * @return mixed\n+     */\n+    public function handleRequest(RequestInterface $request, callable $next, callable $first)\n+    {\n+        if (!$this->isCacheableRequest($request)) {\n+            return $next($request);\n+        }\n+\n+        // If we can cache the request\n+        $key = $this->createCacheKey($request);\n+        $cacheItem = $this->pool->getItem($key);\n+        if ($cacheItem->exists()) {\n+            // return cached response\n+            return $cacheItem->get();\n+        }\n+\n+        return $next($request)->then(function (ResponseInterface $response) use ($key) {\n+            if ($this->isCacheableResponse($response)) {\n+                $cacheItem = $this->createCacheItem($response);\n+                $this->pool->save($cacheItem);\n+            }\n+\n+            return $response;\n+        });\n+    }\n+\n+    /**\n+     * We do only cache GET and HEAD requests.\n+     *\n+     * @param RequestInterface $request\n+     *\n+     * @return bool\n+     */\n+    private function isCacheableRequest(RequestInterface $request)","path":"src/CachePlugin.php","position":87,"original_position":87,"commit_id":"4cb971437f633919e6fb192fea81f4fa35b57717","original_commit_id":"4cb971437f633919e6fb192fea81f4fa35b57717","user":{"login":"Nyholm","id":1275206,"avatar_url":"https://avatars.githubusercontent.com/u/1275206?v=3","gravatar_id":"","url":"https://api.github.com/users/Nyholm","html_url":"https://github.com/Nyholm","followers_url":"https://api.github.com/users/Nyholm/followers","following_url":"https://api.github.com/users/Nyholm/following{/other_user}","gists_url":"https://api.github.com/users/Nyholm/gists{/gist_id}","starred_url":"https://api.github.com/users/Nyholm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nyholm/subscriptions","organizations_url":"https://api.github.com/users/Nyholm/orgs","repos_url":"https://api.github.com/users/Nyholm/repos","events_url":"https://api.github.com/users/Nyholm/events{/privacy}","received_events_url":"https://api.github.com/users/Nyholm/received_events","type":"User","site_admin":false},"body":"Are you suggesting that I should rename `isCacheableRequest` to `isCacheable`. How can I make a difference between `isCacheableRequest` and `isCacheableResponse´?","created_at":"2015-11-27T13:27:35Z","updated_at":"2015-11-27T13:27:35Z","html_url":"https://github.com/php-http/plugins/pull/15#discussion_r46046555","pull_request_url":"https://api.github.com/repos/php-http/plugins/pulls/15","_links":{"self":{"href":"https://api.github.com/repos/php-http/plugins/pulls/comments/46046555"},"html":{"href":"https://github.com/php-http/plugins/pull/15#discussion_r46046555"},"pull_request":{"href":"https://api.github.com/repos/php-http/plugins/pulls/15"}}},"pull_request":{"url":"https://api.github.com/repos/php-http/plugins/pulls/15","id":51986693,"html_url":"https://github.com/php-http/plugins/pull/15","diff_url":"https://github.com/php-http/plugins/pull/15.diff","patch_url":"https://github.com/php-http/plugins/pull/15.patch","issue_url":"https://api.github.com/repos/php-http/plugins/issues/15","number":15,"state":"open","locked":false,"title":"[WIP] Added a PSR6 cache plugin","user":{"login":"Nyholm","id":1275206,"avatar_url":"https://avatars.githubusercontent.com/u/1275206?v=3","gravatar_id":"","url":"https://api.github.com/users/Nyholm","html_url":"https://github.com/Nyholm","followers_url":"https://api.github.com/users/Nyholm/followers","following_url":"https://api.github.com/users/Nyholm/following{/other_user}","gists_url":"https://api.github.com/users/Nyholm/gists{/gist_id}","starred_url":"https://api.github.com/users/Nyholm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nyholm/subscriptions","organizations_url":"https://api.github.com/users/Nyholm/orgs","repos_url":"https://api.github.com/users/Nyholm/repos","events_url":"https://api.github.com/users/Nyholm/events{/privacy}","received_events_url":"https://api.github.com/users/Nyholm/received_events","type":"User","site_admin":false},"body":"Added a draft on a cache plugin. This can not be merged until PSR6 is final (currently voting). There should also be a way of creating an instance of CacheItemInterface. ATM there is just a `stdClass`. \r\n\r\nShould I use a concrete implementation instead of PSR6?","created_at":"2015-11-27T11:45:25Z","updated_at":"2015-11-27T13:27:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b0347b3ccb169ec35ca09bb0f65eea49cc79c0f9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/php-http/plugins/pulls/15/commits","review_comments_url":"https://api.github.com/repos/php-http/plugins/pulls/15/comments","review_comment_url":"https://api.github.com/repos/php-http/plugins/pulls/comments{/number}","comments_url":"https://api.github.com/repos/php-http/plugins/issues/15/comments","statuses_url":"https://api.github.com/repos/php-http/plugins/statuses/4cb971437f633919e6fb192fea81f4fa35b57717","head":{"label":"Nyholm:psr6","ref":"psr6","sha":"4cb971437f633919e6fb192fea81f4fa35b57717","user":{"login":"Nyholm","id":1275206,"avatar_url":"https://avatars.githubusercontent.com/u/1275206?v=3","gravatar_id":"","url":"https://api.github.com/users/Nyholm","html_url":"https://github.com/Nyholm","followers_url":"https://api.github.com/users/Nyholm/followers","following_url":"https://api.github.com/users/Nyholm/following{/other_user}","gists_url":"https://api.github.com/users/Nyholm/gists{/gist_id}","starred_url":"https://api.github.com/users/Nyholm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nyholm/subscriptions","organizations_url":"https://api.github.com/users/Nyholm/orgs","repos_url":"https://api.github.com/users/Nyholm/repos","events_url":"https://api.github.com/users/Nyholm/events{/privacy}","received_events_url":"https://api.github.com/users/Nyholm/received_events","type":"User","site_admin":false},"repo":{"id":46977267,"name":"plugins","full_name":"Nyholm/plugins","owner":{"login":"Nyholm","id":1275206,"avatar_url":"https://avatars.githubusercontent.com/u/1275206?v=3","gravatar_id":"","url":"https://api.github.com/users/Nyholm","html_url":"https://github.com/Nyholm","followers_url":"https://api.github.com/users/Nyholm/followers","following_url":"https://api.github.com/users/Nyholm/following{/other_user}","gists_url":"https://api.github.com/users/Nyholm/gists{/gist_id}","starred_url":"https://api.github.com/users/Nyholm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nyholm/subscriptions","organizations_url":"https://api.github.com/users/Nyholm/orgs","repos_url":"https://api.github.com/users/Nyholm/repos","events_url":"https://api.github.com/users/Nyholm/events{/privacy}","received_events_url":"https://api.github.com/users/Nyholm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Nyholm/plugins","description":"Plugin client, decorate http client with middlewares","fork":true,"url":"https://api.github.com/repos/Nyholm/plugins","forks_url":"https://api.github.com/repos/Nyholm/plugins/forks","keys_url":"https://api.github.com/repos/Nyholm/plugins/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Nyholm/plugins/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Nyholm/plugins/teams","hooks_url":"https://api.github.com/repos/Nyholm/plugins/hooks","issue_events_url":"https://api.github.com/repos/Nyholm/plugins/issues/events{/number}","events_url":"https://api.github.com/repos/Nyholm/plugins/events","assignees_url":"https://api.github.com/repos/Nyholm/plugins/assignees{/user}","branches_url":"https://api.github.com/repos/Nyholm/plugins/branches{/branch}","tags_url":"https://api.github.com/repos/Nyholm/plugins/tags","blobs_url":"https://api.github.com/repos/Nyholm/plugins/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Nyholm/plugins/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Nyholm/plugins/git/refs{/sha}","trees_url":"https://api.github.com/repos/Nyholm/plugins/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Nyholm/plugins/statuses/{sha}","languages_url":"https://api.github.com/repos/Nyholm/plugins/languages","stargazers_url":"https://api.github.com/repos/Nyholm/plugins/stargazers","contributors_url":"https://api.github.com/repos/Nyholm/plugins/contributors","subscribers_url":"https://api.github.com/repos/Nyholm/plugins/subscribers","subscription_url":"https://api.github.com/repos/Nyholm/plugins/subscription","commits_url":"https://api.github.com/repos/Nyholm/plugins/commits{/sha}","git_commits_url":"https://api.github.com/repos/Nyholm/plugins/git/commits{/sha}","comments_url":"https://api.github.com/repos/Nyholm/plugins/comments{/number}","issue_comment_url":"https://api.github.com/repos/Nyholm/plugins/issues/comments{/number}","contents_url":"https://api.github.com/repos/Nyholm/plugins/contents/{+path}","compare_url":"https://api.github.com/repos/Nyholm/plugins/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Nyholm/plugins/merges","archive_url":"https://api.github.com/repos/Nyholm/plugins/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Nyholm/plugins/downloads","issues_url":"https://api.github.com/repos/Nyholm/plugins/issues{/number}","pulls_url":"https://api.github.com/repos/Nyholm/plugins/pulls{/number}","milestones_url":"https://api.github.com/repos/Nyholm/plugins/milestones{/number}","notifications_url":"https://api.github.com/repos/Nyholm/plugins/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Nyholm/plugins/labels{/name}","releases_url":"https://api.github.com/repos/Nyholm/plugins/releases{/id}","created_at":"2015-11-27T11:40:56Z","updated_at":"2015-11-27T11:40:57Z","pushed_at":"2015-11-27T11:41:35Z","git_url":"git://github.com/Nyholm/plugins.git","ssh_url":"git@github.com:Nyholm/plugins.git","clone_url":"https://github.com/Nyholm/plugins.git","svn_url":"https://github.com/Nyholm/plugins","homepage":"http://httplug.io","size":39,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"php-http:master","ref":"master","sha":"821e8a3f9be27eec2d10ea3b7798274d2cec1ce1","user":{"login":"php-http","id":12104859,"avatar_url":"https://avatars.githubusercontent.com/u/12104859?v=3","gravatar_id":"","url":"https://api.github.com/users/php-http","html_url":"https://github.com/php-http","followers_url":"https://api.github.com/users/php-http/followers","following_url":"https://api.github.com/users/php-http/following{/other_user}","gists_url":"https://api.github.com/users/php-http/gists{/gist_id}","starred_url":"https://api.github.com/users/php-http/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php-http/subscriptions","organizations_url":"https://api.github.com/users/php-http/orgs","repos_url":"https://api.github.com/users/php-http/repos","events_url":"https://api.github.com/users/php-http/events{/privacy}","received_events_url":"https://api.github.com/users/php-http/received_events","type":"Organization","site_admin":false},"repo":{"id":45717095,"name":"plugins","full_name":"php-http/plugins","owner":{"login":"php-http","id":12104859,"avatar_url":"https://avatars.githubusercontent.com/u/12104859?v=3","gravatar_id":"","url":"https://api.github.com/users/php-http","html_url":"https://github.com/php-http","followers_url":"https://api.github.com/users/php-http/followers","following_url":"https://api.github.com/users/php-http/following{/other_user}","gists_url":"https://api.github.com/users/php-http/gists{/gist_id}","starred_url":"https://api.github.com/users/php-http/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php-http/subscriptions","organizations_url":"https://api.github.com/users/php-http/orgs","repos_url":"https://api.github.com/users/php-http/repos","events_url":"https://api.github.com/users/php-http/events{/privacy}","received_events_url":"https://api.github.com/users/php-http/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/php-http/plugins","description":"Plugin client, decorate http client with middlewares","fork":false,"url":"https://api.github.com/repos/php-http/plugins","forks_url":"https://api.github.com/repos/php-http/plugins/forks","keys_url":"https://api.github.com/repos/php-http/plugins/keys{/key_id}","collaborators_url":"https://api.github.com/repos/php-http/plugins/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/php-http/plugins/teams","hooks_url":"https://api.github.com/repos/php-http/plugins/hooks","issue_events_url":"https://api.github.com/repos/php-http/plugins/issues/events{/number}","events_url":"https://api.github.com/repos/php-http/plugins/events","assignees_url":"https://api.github.com/repos/php-http/plugins/assignees{/user}","branches_url":"https://api.github.com/repos/php-http/plugins/branches{/branch}","tags_url":"https://api.github.com/repos/php-http/plugins/tags","blobs_url":"https://api.github.com/repos/php-http/plugins/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/php-http/plugins/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/php-http/plugins/git/refs{/sha}","trees_url":"https://api.github.com/repos/php-http/plugins/git/trees{/sha}","statuses_url":"https://api.github.com/repos/php-http/plugins/statuses/{sha}","languages_url":"https://api.github.com/repos/php-http/plugins/languages","stargazers_url":"https://api.github.com/repos/php-http/plugins/stargazers","contributors_url":"https://api.github.com/repos/php-http/plugins/contributors","subscribers_url":"https://api.github.com/repos/php-http/plugins/subscribers","subscription_url":"https://api.github.com/repos/php-http/plugins/subscription","commits_url":"https://api.github.com/repos/php-http/plugins/commits{/sha}","git_commits_url":"https://api.github.com/repos/php-http/plugins/git/commits{/sha}","comments_url":"https://api.github.com/repos/php-http/plugins/comments{/number}","issue_comment_url":"https://api.github.com/repos/php-http/plugins/issues/comments{/number}","contents_url":"https://api.github.com/repos/php-http/plugins/contents/{+path}","compare_url":"https://api.github.com/repos/php-http/plugins/compare/{base}...{head}","merges_url":"https://api.github.com/repos/php-http/plugins/merges","archive_url":"https://api.github.com/repos/php-http/plugins/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/php-http/plugins/downloads","issues_url":"https://api.github.com/repos/php-http/plugins/issues{/number}","pulls_url":"https://api.github.com/repos/php-http/plugins/pulls{/number}","milestones_url":"https://api.github.com/repos/php-http/plugins/milestones{/number}","notifications_url":"https://api.github.com/repos/php-http/plugins/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/php-http/plugins/labels{/name}","releases_url":"https://api.github.com/repos/php-http/plugins/releases{/id}","created_at":"2015-11-07T01:13:49Z","updated_at":"2015-11-20T17:03:41Z","pushed_at":"2015-11-27T11:45:25Z","git_url":"git://github.com/php-http/plugins.git","ssh_url":"git@github.com:php-http/plugins.git","clone_url":"https://github.com/php-http/plugins.git","svn_url":"https://github.com/php-http/plugins","homepage":"http://httplug.io","size":39,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":8,"forks":2,"open_issues":8,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/php-http/plugins/pulls/15"},"html":{"href":"https://github.com/php-http/plugins/pull/15"},"issue":{"href":"https://api.github.com/repos/php-http/plugins/issues/15"},"comments":{"href":"https://api.github.com/repos/php-http/plugins/issues/15/comments"},"review_comments":{"href":"https://api.github.com/repos/php-http/plugins/pulls/15/comments"},"review_comment":{"href":"https://api.github.com/repos/php-http/plugins/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/php-http/plugins/pulls/15/commits"},"statuses":{"href":"https://api.github.com/repos/php-http/plugins/statuses/4cb971437f633919e6fb192fea81f4fa35b57717"}}}}