{"action":"created","comment":{"url":"https://api.github.com/repos/guardian/frontend/pulls/comments/61292880","id":61292880,"diff_hunk":"@@ -29,41 +31,55 @@ object Cached extends implicits.Dates {\n \n   private val tenDaysInSeconds = 864000\n \n-  def apply(seconds: Int)(result: Result): Result = {\n-    if (cacheableStatusCodes.contains(result.header.status)) cacheHeaders(seconds, result) else result\n-  }\n-\n-  def apply(duration: Duration)(result: Result): Result = {\n-    apply(duration.toSeconds.toInt)(result)\n-  }\n+  case class Hash(string: String)\n \n-  def apply(page: Page)(result: Result): Result = {\n-    val cacheSeconds = page.metadata.cacheTime.cacheSeconds\n-    if (cacheableStatusCodes.contains(result.header.status)) cacheHeaders(cacheSeconds, result) else result\n-  }\n+  sealed trait CacheableResult\n+  case class RevalidatableResult(result: Result, hash: Hash) extends CacheableResult\n+  case class WithoutRevalidationResult(result: Result) extends CacheableResult\n \n-  case class Hash(string: String)\n-  case class RevalidatableResult(result: Result, hash: Hash)\n   object RevalidatableResult {\n-    def apply(result: Result, body: String) = {\n+    def apply[C](result: Result, content: C)(implicit writeable: Writeable[C]) = {\n       // hashing function from Arrays.java\n-      val hashLong: Long = body.getBytes(\"UTF-8\").foldLeft(z = 1L){\n+      val hashLong: Long = writeable.transform(content).foldLeft(z = 1L){\n         case (accu, nextByte) => 31 * accu + nextByte\n       }\n       new RevalidatableResult(result, Hash(hashLong.toString))\n     }\n+\n+    def Ok[C](content: C)(implicit writeable: Writeable[C]) = {\n+      apply(Results.Ok(content), content)\n+    }\n+  }\n+\n+\n+  def apply(seconds: Int)(result: CacheableResult)(implicit request: RequestHeader): Result = {\n+    apply(seconds, result, request.headers.get(\"If-None-Match\"))//FIXME could be comma separated\n   }\n \n-  def withRevalidation(page: Page)(stringResult: RevalidatableResult)(implicit request: RequestHeader): Result = {\n+  def apply(duration: Duration)(result: CacheableResult)(implicit request: RequestHeader): Result = {\n+    apply(duration.toSeconds.toInt, result, request.headers.get(\"If-None-Match\"))\n+  }\n+\n+  def apply(page: Page)(revalidatableResult: CacheableResult)(implicit request: RequestHeader): Result = {\n     val cacheSeconds = page.metadata.cacheTime.cacheSeconds\n-    if (cacheableStatusCodes.contains(stringResult.result.header.status)) cacheHeaders(cacheSeconds, stringResult.result, Some((stringResult.hash, request.headers.get(\"If-None-Match\")))) else stringResult.result\n+    apply(cacheSeconds, revalidatableResult, request.headers.get(\"If-None-Match\"))\n   }\n \n   // Use this when you are sure your result needs caching headers, even though the result status isn't\n   // conventionally cacheable. Typically we only cache 200 and 404 responses.\n-  def explicitlyCache(seconds: Int)(result: Result): Result = cacheHeaders(seconds, result)\n+  def explicitlyCache(seconds: Int)(result: Result): Result = cacheHeaders(seconds, result, None)\n+\n+  def apply(seconds: Int, cacheableResult: CacheableResult, ifNoneMatch: Option[String]) = cacheableResult match {\n+    case RevalidatableResult(result, hash) =>\n+      if (cacheableStatusCodes.contains(result.header.status)) {\n+        val hashMaybeRequestHash = (hash, ifNoneMatch)//FIXME could be comma separated\n+        cacheHeaders(seconds, result, Some(hashMaybeRequestHash))\n+      } else\n+        result","path":"common/app/model/Cached.scala","position":76,"original_position":76,"commit_id":"7e13d7e91ada0926e9e478d3ac7e515f21fad20c","original_commit_id":"7e13d7e91ada0926e9e478d3ac7e515f21fad20c","user":{"login":"jamesgorrie","id":31692,"avatar_url":"https://avatars.githubusercontent.com/u/31692?v=3","gravatar_id":"","url":"https://api.github.com/users/jamesgorrie","html_url":"https://github.com/jamesgorrie","followers_url":"https://api.github.com/users/jamesgorrie/followers","following_url":"https://api.github.com/users/jamesgorrie/following{/other_user}","gists_url":"https://api.github.com/users/jamesgorrie/gists{/gist_id}","starred_url":"https://api.github.com/users/jamesgorrie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamesgorrie/subscriptions","organizations_url":"https://api.github.com/users/jamesgorrie/orgs","repos_url":"https://api.github.com/users/jamesgorrie/repos","events_url":"https://api.github.com/users/jamesgorrie/events{/privacy}","received_events_url":"https://api.github.com/users/jamesgorrie/received_events","type":"User","site_admin":false},"body":"MIght be nicer if `result` was on the same line?","created_at":"2016-04-27T16:42:31Z","updated_at":"2016-04-27T16:42:31Z","html_url":"https://github.com/guardian/frontend/pull/12654#discussion_r61292880","pull_request_url":"https://api.github.com/repos/guardian/frontend/pulls/12654","_links":{"self":{"href":"https://api.github.com/repos/guardian/frontend/pulls/comments/61292880"},"html":{"href":"https://github.com/guardian/frontend/pull/12654#discussion_r61292880"},"pull_request":{"href":"https://api.github.com/repos/guardian/frontend/pulls/12654"}}},"pull_request":{"url":"https://api.github.com/repos/guardian/frontend/pulls/12654","id":67535787,"html_url":"https://github.com/guardian/frontend/pull/12654","diff_url":"https://github.com/guardian/frontend/pull/12654.diff","patch_url":"https://github.com/guardian/frontend/pull/12654.patch","issue_url":"https://api.github.com/repos/guardian/frontend/issues/12654","number":12654,"state":"open","locked":false,"title":"use etags/if-none-match for all responses","user":{"login":"johnduffell","id":7304387,"avatar_url":"https://avatars.githubusercontent.com/u/7304387?v=3","gravatar_id":"","url":"https://api.github.com/users/johnduffell","html_url":"https://github.com/johnduffell","followers_url":"https://api.github.com/users/johnduffell/followers","following_url":"https://api.github.com/users/johnduffell/following{/other_user}","gists_url":"https://api.github.com/users/johnduffell/gists{/gist_id}","starred_url":"https://api.github.com/users/johnduffell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnduffell/subscriptions","organizations_url":"https://api.github.com/users/johnduffell/orgs","repos_url":"https://api.github.com/users/johnduffell/repos","events_url":"https://api.github.com/users/johnduffell/events{/privacy}","received_events_url":"https://api.github.com/users/johnduffell/received_events","type":"User","site_admin":false},"body":"This PR follows on from https://github.com/guardian/frontend/pull/12610 and extends the etag and revalidation logic across the whole site.\r\n\r\nThis will remove any unnecessary resending of responses and therefore save us plenty of bandwidth out from the apps and the router.\r\n\r\n@cb372 @jamespamplin @piuccio take a look!","created_at":"2016-04-22T16:08:21Z","updated_at":"2016-04-27T16:42:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"07fab8207b526b086574b66d8795113938704910","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/guardian/frontend/pulls/12654/commits","review_comments_url":"https://api.github.com/repos/guardian/frontend/pulls/12654/comments","review_comment_url":"https://api.github.com/repos/guardian/frontend/pulls/comments{/number}","comments_url":"https://api.github.com/repos/guardian/frontend/issues/12654/comments","statuses_url":"https://api.github.com/repos/guardian/frontend/statuses/7e13d7e91ada0926e9e478d3ac7e515f21fad20c","head":{"label":"guardian:etags-everywhere","ref":"etags-everywhere","sha":"7e13d7e91ada0926e9e478d3ac7e515f21fad20c","user":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"repo":{"id":4370755,"name":"frontend","full_name":"guardian/frontend","owner":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/guardian/frontend","description":"Source for theguardian.com","fork":false,"url":"https://api.github.com/repos/guardian/frontend","forks_url":"https://api.github.com/repos/guardian/frontend/forks","keys_url":"https://api.github.com/repos/guardian/frontend/keys{/key_id}","collaborators_url":"https://api.github.com/repos/guardian/frontend/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/guardian/frontend/teams","hooks_url":"https://api.github.com/repos/guardian/frontend/hooks","issue_events_url":"https://api.github.com/repos/guardian/frontend/issues/events{/number}","events_url":"https://api.github.com/repos/guardian/frontend/events","assignees_url":"https://api.github.com/repos/guardian/frontend/assignees{/user}","branches_url":"https://api.github.com/repos/guardian/frontend/branches{/branch}","tags_url":"https://api.github.com/repos/guardian/frontend/tags","blobs_url":"https://api.github.com/repos/guardian/frontend/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/guardian/frontend/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/guardian/frontend/git/refs{/sha}","trees_url":"https://api.github.com/repos/guardian/frontend/git/trees{/sha}","statuses_url":"https://api.github.com/repos/guardian/frontend/statuses/{sha}","languages_url":"https://api.github.com/repos/guardian/frontend/languages","stargazers_url":"https://api.github.com/repos/guardian/frontend/stargazers","contributors_url":"https://api.github.com/repos/guardian/frontend/contributors","subscribers_url":"https://api.github.com/repos/guardian/frontend/subscribers","subscription_url":"https://api.github.com/repos/guardian/frontend/subscription","commits_url":"https://api.github.com/repos/guardian/frontend/commits{/sha}","git_commits_url":"https://api.github.com/repos/guardian/frontend/git/commits{/sha}","comments_url":"https://api.github.com/repos/guardian/frontend/comments{/number}","issue_comment_url":"https://api.github.com/repos/guardian/frontend/issues/comments{/number}","contents_url":"https://api.github.com/repos/guardian/frontend/contents/{+path}","compare_url":"https://api.github.com/repos/guardian/frontend/compare/{base}...{head}","merges_url":"https://api.github.com/repos/guardian/frontend/merges","archive_url":"https://api.github.com/repos/guardian/frontend/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/guardian/frontend/downloads","issues_url":"https://api.github.com/repos/guardian/frontend/issues{/number}","pulls_url":"https://api.github.com/repos/guardian/frontend/pulls{/number}","milestones_url":"https://api.github.com/repos/guardian/frontend/milestones{/number}","notifications_url":"https://api.github.com/repos/guardian/frontend/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/guardian/frontend/labels{/name}","releases_url":"https://api.github.com/repos/guardian/frontend/releases{/id}","deployments_url":"https://api.github.com/repos/guardian/frontend/deployments","created_at":"2012-05-18T15:39:15Z","updated_at":"2016-04-27T04:20:14Z","pushed_at":"2016-04-27T16:34:01Z","git_url":"git://github.com/guardian/frontend.git","ssh_url":"git@github.com:guardian/frontend.git","clone_url":"https://github.com/guardian/frontend.git","svn_url":"https://github.com/guardian/frontend","homepage":"http://www.theguardian.com","size":471277,"stargazers_count":3171,"watchers_count":3171,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":326,"mirror_url":null,"open_issues_count":43,"forks":326,"open_issues":43,"watchers":3171,"default_branch":"master"}},"base":{"label":"guardian:master","ref":"master","sha":"ec12811e537b542158e1ba148b5b78f70ed7f216","user":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"repo":{"id":4370755,"name":"frontend","full_name":"guardian/frontend","owner":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/guardian/frontend","description":"Source for theguardian.com","fork":false,"url":"https://api.github.com/repos/guardian/frontend","forks_url":"https://api.github.com/repos/guardian/frontend/forks","keys_url":"https://api.github.com/repos/guardian/frontend/keys{/key_id}","collaborators_url":"https://api.github.com/repos/guardian/frontend/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/guardian/frontend/teams","hooks_url":"https://api.github.com/repos/guardian/frontend/hooks","issue_events_url":"https://api.github.com/repos/guardian/frontend/issues/events{/number}","events_url":"https://api.github.com/repos/guardian/frontend/events","assignees_url":"https://api.github.com/repos/guardian/frontend/assignees{/user}","branches_url":"https://api.github.com/repos/guardian/frontend/branches{/branch}","tags_url":"https://api.github.com/repos/guardian/frontend/tags","blobs_url":"https://api.github.com/repos/guardian/frontend/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/guardian/frontend/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/guardian/frontend/git/refs{/sha}","trees_url":"https://api.github.com/repos/guardian/frontend/git/trees{/sha}","statuses_url":"https://api.github.com/repos/guardian/frontend/statuses/{sha}","languages_url":"https://api.github.com/repos/guardian/frontend/languages","stargazers_url":"https://api.github.com/repos/guardian/frontend/stargazers","contributors_url":"https://api.github.com/repos/guardian/frontend/contributors","subscribers_url":"https://api.github.com/repos/guardian/frontend/subscribers","subscription_url":"https://api.github.com/repos/guardian/frontend/subscription","commits_url":"https://api.github.com/repos/guardian/frontend/commits{/sha}","git_commits_url":"https://api.github.com/repos/guardian/frontend/git/commits{/sha}","comments_url":"https://api.github.com/repos/guardian/frontend/comments{/number}","issue_comment_url":"https://api.github.com/repos/guardian/frontend/issues/comments{/number}","contents_url":"https://api.github.com/repos/guardian/frontend/contents/{+path}","compare_url":"https://api.github.com/repos/guardian/frontend/compare/{base}...{head}","merges_url":"https://api.github.com/repos/guardian/frontend/merges","archive_url":"https://api.github.com/repos/guardian/frontend/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/guardian/frontend/downloads","issues_url":"https://api.github.com/repos/guardian/frontend/issues{/number}","pulls_url":"https://api.github.com/repos/guardian/frontend/pulls{/number}","milestones_url":"https://api.github.com/repos/guardian/frontend/milestones{/number}","notifications_url":"https://api.github.com/repos/guardian/frontend/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/guardian/frontend/labels{/name}","releases_url":"https://api.github.com/repos/guardian/frontend/releases{/id}","deployments_url":"https://api.github.com/repos/guardian/frontend/deployments","created_at":"2012-05-18T15:39:15Z","updated_at":"2016-04-27T04:20:14Z","pushed_at":"2016-04-27T16:34:01Z","git_url":"git://github.com/guardian/frontend.git","ssh_url":"git@github.com:guardian/frontend.git","clone_url":"https://github.com/guardian/frontend.git","svn_url":"https://github.com/guardian/frontend","homepage":"http://www.theguardian.com","size":471277,"stargazers_count":3171,"watchers_count":3171,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":326,"mirror_url":null,"open_issues_count":43,"forks":326,"open_issues":43,"watchers":3171,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/guardian/frontend/pulls/12654"},"html":{"href":"https://github.com/guardian/frontend/pull/12654"},"issue":{"href":"https://api.github.com/repos/guardian/frontend/issues/12654"},"comments":{"href":"https://api.github.com/repos/guardian/frontend/issues/12654/comments"},"review_comments":{"href":"https://api.github.com/repos/guardian/frontend/pulls/12654/comments"},"review_comment":{"href":"https://api.github.com/repos/guardian/frontend/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/guardian/frontend/pulls/12654/commits"},"statuses":{"href":"https://api.github.com/repos/guardian/frontend/statuses/7e13d7e91ada0926e9e478d3ac7e515f21fad20c"}}}}