{"action":"created","comment":{"url":"https://api.github.com/repos/rails/rails/pulls/comments/34738563","id":34738563,"diff_hunk":"@@ -298,6 +298,35 @@ def many?\n       limit_value ? to_a.many? : size > 1\n     end\n \n+    # Returns a cache key that can be used to identify the records fetched by\n+    # this query. The cache key is built with a fingerprint of the sql query,\n+    # the number of records matched by the query and a timestamp of the last\n+    # updated record. When a new record comes to match the query, or any of\n+    # the existing records is updated or deleted, the cache key changes.\n+    #\n+    #   Product.where(\"name like ?\", \"%Cosmic Encounter%\").cache_key\n+    #   => \"products/query-1850ab3d302391b85b8693e941286659-1-20150714212553907087000\"\n+    #\n+    # Under the hood this triggers two SQL queries:\n+    #\n+    #    SELECT COUNT(*) FROM \"products\" WHERE (name like '%Cosmic Encounter%')\n+    #    SELECT MAX(\"products\".\"updated_at\") FROM \"products\" WHERE (name like '%Cosmic Encounter%')\n+    #\n+    # You can also pass a custom timestamp column to fetch the timestamp of the\n+    # last updated record.\n+    #\n+    #   Product.where(\"name like ?\", \"%Game%\").cache_key(:last_reviewed_at)\n+    def cache_key(timestamp_column = :updated_at)\n+      query_signature = Digest::MD5.hexdigest(to_sql)","path":"activerecord/lib/active_record/relation.rb","position":23,"original_position":23,"commit_id":"5593e94d679046758cefc98db49e00b3952e648d","original_commit_id":"5593e94d679046758cefc98db49e00b3952e648d","user":{"login":"afcapel","id":150107,"avatar_url":"https://avatars.githubusercontent.com/u/150107?v=3","gravatar_id":"","url":"https://api.github.com/users/afcapel","html_url":"https://github.com/afcapel","followers_url":"https://api.github.com/users/afcapel/followers","following_url":"https://api.github.com/users/afcapel/following{/other_user}","gists_url":"https://api.github.com/users/afcapel/gists{/gist_id}","starred_url":"https://api.github.com/users/afcapel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/afcapel/subscriptions","organizations_url":"https://api.github.com/users/afcapel/orgs","repos_url":"https://api.github.com/users/afcapel/repos","events_url":"https://api.github.com/users/afcapel/events{/privacy}","received_events_url":"https://api.github.com/users/afcapel/received_events","type":"User","site_admin":false},"body":"@egilburg thanks, for your feedback, here are my thoughts:\r\n\r\n1. Although MD5 can have some issues for cryptography I think it is fine for fingerprinting. It's somewhat less CPU intensive and it's what the Asset Pipeline uses for fingerprints.\r\n\r\n2. That's a good idea, although I'll have to check if I can create the query in a DB independent manner.\r\n\r\n3. It will fail hard with an missing column exception. I think that's the right behaviour.\r\n\r\n4. The string length it's not an issue, AFAICT. Average key length ~ 75 chars (depending on the model name), while memcached keys can be up to 255 chars. I'd prefer to optimise for the readability of the key.","created_at":"2015-07-15T22:44:05Z","updated_at":"2015-07-15T22:44:05Z","html_url":"https://github.com/rails/rails/pull/20884#discussion_r34738563","pull_request_url":"https://api.github.com/repos/rails/rails/pulls/20884","_links":{"self":{"href":"https://api.github.com/repos/rails/rails/pulls/comments/34738563"},"html":{"href":"https://github.com/rails/rails/pull/20884#discussion_r34738563"},"pull_request":{"href":"https://api.github.com/repos/rails/rails/pulls/20884"}}},"pull_request":{"url":"https://api.github.com/repos/rails/rails/pulls/20884","id":39972522,"html_url":"https://github.com/rails/rails/pull/20884","diff_url":"https://github.com/rails/rails/pull/20884.diff","patch_url":"https://github.com/rails/rails/pull/20884.patch","issue_url":"https://api.github.com/repos/rails/rails/issues/20884","number":20884,"state":"open","locked":false,"title":"Add #cache_key to ActiveRecord::Relation.","user":{"login":"afcapel","id":150107,"avatar_url":"https://avatars.githubusercontent.com/u/150107?v=3","gravatar_id":"","url":"https://api.github.com/users/afcapel","html_url":"https://github.com/afcapel","followers_url":"https://api.github.com/users/afcapel/followers","following_url":"https://api.github.com/users/afcapel/following{/other_user}","gists_url":"https://api.github.com/users/afcapel/gists{/gist_id}","starred_url":"https://api.github.com/users/afcapel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/afcapel/subscriptions","organizations_url":"https://api.github.com/users/afcapel/orgs","repos_url":"https://api.github.com/users/afcapel/repos","events_url":"https://api.github.com/users/afcapel/events{/privacy}","received_events_url":"https://api.github.com/users/afcapel/received_events","type":"User","site_admin":false},"body":"The Rails caching guide [suggests](http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching) to create a cache key for collections of objects using the collection size and the timestamp of the last updated record. We could automate the process baking this logic into `ActiveRecord::Relation`. \r\n\r\n```ruby\r\n    @users = User.where(\"name like ?\", \"%Alberto%\")\r\n    @users.cache_key\r\n    => \"/users/query-5942b155a43b139f2471b872ac54251f-3-20150714212107656125000\"\r\n```\r\n\r\nThe end result is that you can have cache keys in any relationship that will expire when the any of records matching the relation query changes.\r\n\r\n","created_at":"2015-07-14T23:00:42Z","updated_at":"2015-07-15T22:44:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rails/rails/pulls/20884/commits","review_comments_url":"https://api.github.com/repos/rails/rails/pulls/20884/comments","review_comment_url":"https://api.github.com/repos/rails/rails/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rails/rails/issues/20884/comments","statuses_url":"https://api.github.com/repos/rails/rails/statuses/5593e94d679046758cefc98db49e00b3952e648d","head":{"label":"afcapel:ar-relation-cache-key","ref":"ar-relation-cache-key","sha":"5593e94d679046758cefc98db49e00b3952e648d","user":{"login":"afcapel","id":150107,"avatar_url":"https://avatars.githubusercontent.com/u/150107?v=3","gravatar_id":"","url":"https://api.github.com/users/afcapel","html_url":"https://github.com/afcapel","followers_url":"https://api.github.com/users/afcapel/followers","following_url":"https://api.github.com/users/afcapel/following{/other_user}","gists_url":"https://api.github.com/users/afcapel/gists{/gist_id}","starred_url":"https://api.github.com/users/afcapel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/afcapel/subscriptions","organizations_url":"https://api.github.com/users/afcapel/orgs","repos_url":"https://api.github.com/users/afcapel/repos","events_url":"https://api.github.com/users/afcapel/events{/privacy}","received_events_url":"https://api.github.com/users/afcapel/received_events","type":"User","site_admin":false},"repo":{"id":39104187,"name":"rails","full_name":"afcapel/rails","owner":{"login":"afcapel","id":150107,"avatar_url":"https://avatars.githubusercontent.com/u/150107?v=3","gravatar_id":"","url":"https://api.github.com/users/afcapel","html_url":"https://github.com/afcapel","followers_url":"https://api.github.com/users/afcapel/followers","following_url":"https://api.github.com/users/afcapel/following{/other_user}","gists_url":"https://api.github.com/users/afcapel/gists{/gist_id}","starred_url":"https://api.github.com/users/afcapel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/afcapel/subscriptions","organizations_url":"https://api.github.com/users/afcapel/orgs","repos_url":"https://api.github.com/users/afcapel/repos","events_url":"https://api.github.com/users/afcapel/events{/privacy}","received_events_url":"https://api.github.com/users/afcapel/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/afcapel/rails","description":"Ruby on Rails","fork":true,"url":"https://api.github.com/repos/afcapel/rails","forks_url":"https://api.github.com/repos/afcapel/rails/forks","keys_url":"https://api.github.com/repos/afcapel/rails/keys{/key_id}","collaborators_url":"https://api.github.com/repos/afcapel/rails/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/afcapel/rails/teams","hooks_url":"https://api.github.com/repos/afcapel/rails/hooks","issue_events_url":"https://api.github.com/repos/afcapel/rails/issues/events{/number}","events_url":"https://api.github.com/repos/afcapel/rails/events","assignees_url":"https://api.github.com/repos/afcapel/rails/assignees{/user}","branches_url":"https://api.github.com/repos/afcapel/rails/branches{/branch}","tags_url":"https://api.github.com/repos/afcapel/rails/tags","blobs_url":"https://api.github.com/repos/afcapel/rails/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/afcapel/rails/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/afcapel/rails/git/refs{/sha}","trees_url":"https://api.github.com/repos/afcapel/rails/git/trees{/sha}","statuses_url":"https://api.github.com/repos/afcapel/rails/statuses/{sha}","languages_url":"https://api.github.com/repos/afcapel/rails/languages","stargazers_url":"https://api.github.com/repos/afcapel/rails/stargazers","contributors_url":"https://api.github.com/repos/afcapel/rails/contributors","subscribers_url":"https://api.github.com/repos/afcapel/rails/subscribers","subscription_url":"https://api.github.com/repos/afcapel/rails/subscription","commits_url":"https://api.github.com/repos/afcapel/rails/commits{/sha}","git_commits_url":"https://api.github.com/repos/afcapel/rails/git/commits{/sha}","comments_url":"https://api.github.com/repos/afcapel/rails/comments{/number}","issue_comment_url":"https://api.github.com/repos/afcapel/rails/issues/comments{/number}","contents_url":"https://api.github.com/repos/afcapel/rails/contents/{+path}","compare_url":"https://api.github.com/repos/afcapel/rails/compare/{base}...{head}","merges_url":"https://api.github.com/repos/afcapel/rails/merges","archive_url":"https://api.github.com/repos/afcapel/rails/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/afcapel/rails/downloads","issues_url":"https://api.github.com/repos/afcapel/rails/issues{/number}","pulls_url":"https://api.github.com/repos/afcapel/rails/pulls{/number}","milestones_url":"https://api.github.com/repos/afcapel/rails/milestones{/number}","notifications_url":"https://api.github.com/repos/afcapel/rails/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/afcapel/rails/labels{/name}","releases_url":"https://api.github.com/repos/afcapel/rails/releases{/id}","created_at":"2015-07-14T22:44:22Z","updated_at":"2015-07-14T22:44:51Z","pushed_at":"2015-07-14T22:49:33Z","git_url":"git://github.com/afcapel/rails.git","ssh_url":"git@github.com:afcapel/rails.git","clone_url":"https://github.com/afcapel/rails.git","svn_url":"https://github.com/afcapel/rails","homepage":"http://rubyonrails.org","size":144685,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"rails:master","ref":"master","sha":"4d4d764ab688af4a0d213ab2d0dc7f9b811ab451","user":{"login":"rails","id":4223,"avatar_url":"https://avatars.githubusercontent.com/u/4223?v=3","gravatar_id":"","url":"https://api.github.com/users/rails","html_url":"https://github.com/rails","followers_url":"https://api.github.com/users/rails/followers","following_url":"https://api.github.com/users/rails/following{/other_user}","gists_url":"https://api.github.com/users/rails/gists{/gist_id}","starred_url":"https://api.github.com/users/rails/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails/subscriptions","organizations_url":"https://api.github.com/users/rails/orgs","repos_url":"https://api.github.com/users/rails/repos","events_url":"https://api.github.com/users/rails/events{/privacy}","received_events_url":"https://api.github.com/users/rails/received_events","type":"Organization","site_admin":false},"repo":{"id":8514,"name":"rails","full_name":"rails/rails","owner":{"login":"rails","id":4223,"avatar_url":"https://avatars.githubusercontent.com/u/4223?v=3","gravatar_id":"","url":"https://api.github.com/users/rails","html_url":"https://github.com/rails","followers_url":"https://api.github.com/users/rails/followers","following_url":"https://api.github.com/users/rails/following{/other_user}","gists_url":"https://api.github.com/users/rails/gists{/gist_id}","starred_url":"https://api.github.com/users/rails/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails/subscriptions","organizations_url":"https://api.github.com/users/rails/orgs","repos_url":"https://api.github.com/users/rails/repos","events_url":"https://api.github.com/users/rails/events{/privacy}","received_events_url":"https://api.github.com/users/rails/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rails/rails","description":"Ruby on Rails","fork":false,"url":"https://api.github.com/repos/rails/rails","forks_url":"https://api.github.com/repos/rails/rails/forks","keys_url":"https://api.github.com/repos/rails/rails/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rails/rails/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rails/rails/teams","hooks_url":"https://api.github.com/repos/rails/rails/hooks","issue_events_url":"https://api.github.com/repos/rails/rails/issues/events{/number}","events_url":"https://api.github.com/repos/rails/rails/events","assignees_url":"https://api.github.com/repos/rails/rails/assignees{/user}","branches_url":"https://api.github.com/repos/rails/rails/branches{/branch}","tags_url":"https://api.github.com/repos/rails/rails/tags","blobs_url":"https://api.github.com/repos/rails/rails/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rails/rails/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rails/rails/git/refs{/sha}","trees_url":"https://api.github.com/repos/rails/rails/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rails/rails/statuses/{sha}","languages_url":"https://api.github.com/repos/rails/rails/languages","stargazers_url":"https://api.github.com/repos/rails/rails/stargazers","contributors_url":"https://api.github.com/repos/rails/rails/contributors","subscribers_url":"https://api.github.com/repos/rails/rails/subscribers","subscription_url":"https://api.github.com/repos/rails/rails/subscription","commits_url":"https://api.github.com/repos/rails/rails/commits{/sha}","git_commits_url":"https://api.github.com/repos/rails/rails/git/commits{/sha}","comments_url":"https://api.github.com/repos/rails/rails/comments{/number}","issue_comment_url":"https://api.github.com/repos/rails/rails/issues/comments{/number}","contents_url":"https://api.github.com/repos/rails/rails/contents/{+path}","compare_url":"https://api.github.com/repos/rails/rails/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rails/rails/merges","archive_url":"https://api.github.com/repos/rails/rails/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rails/rails/downloads","issues_url":"https://api.github.com/repos/rails/rails/issues{/number}","pulls_url":"https://api.github.com/repos/rails/rails/pulls{/number}","milestones_url":"https://api.github.com/repos/rails/rails/milestones{/number}","notifications_url":"https://api.github.com/repos/rails/rails/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rails/rails/labels{/name}","releases_url":"https://api.github.com/repos/rails/rails/releases{/id}","created_at":"2008-04-11T02:19:47Z","updated_at":"2015-07-15T22:02:49Z","pushed_at":"2015-07-15T20:43:30Z","git_url":"git://github.com/rails/rails.git","ssh_url":"git@github.com:rails/rails.git","clone_url":"https://github.com/rails/rails.git","svn_url":"https://github.com/rails/rails","homepage":"http://rubyonrails.org","size":428096,"stargazers_count":26910,"watchers_count":26910,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":10748,"mirror_url":null,"open_issues_count":858,"forks":10748,"open_issues":858,"watchers":26910,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails/rails/pulls/20884"},"html":{"href":"https://github.com/rails/rails/pull/20884"},"issue":{"href":"https://api.github.com/repos/rails/rails/issues/20884"},"comments":{"href":"https://api.github.com/repos/rails/rails/issues/20884/comments"},"review_comments":{"href":"https://api.github.com/repos/rails/rails/pulls/20884/comments"},"review_comment":{"href":"https://api.github.com/repos/rails/rails/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rails/rails/pulls/20884/commits"},"statuses":{"href":"https://api.github.com/repos/rails/rails/statuses/5593e94d679046758cefc98db49e00b3952e648d"}}}}