{"action":"created","comment":{"url":"https://api.github.com/repos/matrix-org/synapse/pulls/comments/48353098","id":48353098,"diff_hunk":"@@ -0,0 +1,71 @@\n+# -*- coding: utf-8 -*-\n+# Copyright 2015 OpenMarket Ltd\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+#\n+#     http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# See the License for the specific language governing permissions and\n+# limitations under the License.\n+\n+from synapse.util.async import ObservableDeferred\n+\n+\n+class SnapshotCache(object):\n+\n+    DURATION_MS = 5 * 60 * 1000  # Cache results for 2 minutes.\n+\n+    def __init__(self):\n+        self.pending_result_cache = {}  # Request that haven't finished yet.\n+        self.prev_result_cache = {}  # The older requests that have finished.\n+        self.next_result_cache = {}  # The newer requests that have finished.\n+        self.time_last_rotated_ms = 0\n+\n+    def rotate(self, time_now_ms):\n+        # Rotate once if the cache duration has passed since the last rotation.\n+        if time_now_ms - self.time_last_rotated_ms > self.DURATION_MS:\n+            self.prev_result_cache = self.next_result_cache\n+            self.next_result_cache = {}\n+            self.time_last_rotated_ms += self.DURATION_MS\n+\n+        # Rotate again if the cache duration has passed twice since the last\n+        # rotation.\n+        if time_now_ms - self.time_last_rotated_ms > self.DURATION_MS:\n+            self.prev_result_cache = self.next_result_cache\n+            self.next_result_cache = {}\n+            self.time_last_rotated_ms = time_now_ms\n+\n+    def get(self, time_now_ms, key):\n+        self.rotate(time_now_ms)\n+        # This cache is intended to deduplicate requests, so we expect it to be\n+        # missed most of the time. So we just lookup the key in all of the\n+        # dictionaries rather than trying to short circuit the lookup if the\n+        # key is found.\n+        result = self.prev_result_cache.get(key)\n+        result = self.next_result_cache.get(key, result)\n+        result = self.pending_result_cache.get(key, result)\n+        if result is not None:\n+            return result.observe()","path":"synapse/util/caches/snapshot_cache.py","position":73,"original_position":53,"commit_id":"d12c00bdc311bd0685aa7e7e70f1aa7787317164","original_commit_id":"9ac417fa88906d70de6a7c6f94d40fe11fc6d2fa","user":{"login":"NegativeMjark","id":904009,"avatar_url":"https://avatars.githubusercontent.com/u/904009?v=3","gravatar_id":"","url":"https://api.github.com/users/NegativeMjark","html_url":"https://github.com/NegativeMjark","followers_url":"https://api.github.com/users/NegativeMjark/followers","following_url":"https://api.github.com/users/NegativeMjark/following{/other_user}","gists_url":"https://api.github.com/users/NegativeMjark/gists{/gist_id}","starred_url":"https://api.github.com/users/NegativeMjark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NegativeMjark/subscriptions","organizations_url":"https://api.github.com/users/NegativeMjark/orgs","repos_url":"https://api.github.com/users/NegativeMjark/repos","events_url":"https://api.github.com/users/NegativeMjark/events{/privacy}","received_events_url":"https://api.github.com/users/NegativeMjark/received_events","type":"User","site_admin":false},"body":"Done d12c00b\r\n","created_at":"2015-12-23T15:26:48Z","updated_at":"2015-12-23T15:26:48Z","html_url":"https://github.com/matrix-org/synapse/pull/457#discussion_r48353098","pull_request_url":"https://api.github.com/repos/matrix-org/synapse/pulls/457","_links":{"self":{"href":"https://api.github.com/repos/matrix-org/synapse/pulls/comments/48353098"},"html":{"href":"https://github.com/matrix-org/synapse/pull/457#discussion_r48353098"},"pull_request":{"href":"https://api.github.com/repos/matrix-org/synapse/pulls/457"}}},"pull_request":{"url":"https://api.github.com/repos/matrix-org/synapse/pulls/457","id":54400058,"html_url":"https://github.com/matrix-org/synapse/pull/457","diff_url":"https://github.com/matrix-org/synapse/pull/457.diff","patch_url":"https://github.com/matrix-org/synapse/pull/457.patch","issue_url":"https://api.github.com/repos/matrix-org/synapse/issues/457","number":457,"state":"open","locked":false,"title":"Add a cache for initialSync responses that expires after 5 minutes","user":{"login":"NegativeMjark","id":904009,"avatar_url":"https://avatars.githubusercontent.com/u/904009?v=3","gravatar_id":"","url":"https://api.github.com/users/NegativeMjark","html_url":"https://github.com/NegativeMjark","followers_url":"https://api.github.com/users/NegativeMjark/followers","following_url":"https://api.github.com/users/NegativeMjark/following{/other_user}","gists_url":"https://api.github.com/users/NegativeMjark/gists{/gist_id}","starred_url":"https://api.github.com/users/NegativeMjark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NegativeMjark/subscriptions","organizations_url":"https://api.github.com/users/NegativeMjark/orgs","repos_url":"https://api.github.com/users/NegativeMjark/repos","events_url":"https://api.github.com/users/NegativeMjark/events{/privacy}","received_events_url":"https://api.github.com/users/NegativeMjark/received_events","type":"User","site_admin":false},"body":"Needs  matrix-org/sytest#124 to land first","created_at":"2015-12-22T18:28:52Z","updated_at":"2015-12-23T15:26:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d9630d216c0c52298a4e324f27c4cf71e1fbd04b","assignee":{"login":"leonerd","id":350941,"avatar_url":"https://avatars.githubusercontent.com/u/350941?v=3","gravatar_id":"","url":"https://api.github.com/users/leonerd","html_url":"https://github.com/leonerd","followers_url":"https://api.github.com/users/leonerd/followers","following_url":"https://api.github.com/users/leonerd/following{/other_user}","gists_url":"https://api.github.com/users/leonerd/gists{/gist_id}","starred_url":"https://api.github.com/users/leonerd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leonerd/subscriptions","organizations_url":"https://api.github.com/users/leonerd/orgs","repos_url":"https://api.github.com/users/leonerd/repos","events_url":"https://api.github.com/users/leonerd/events{/privacy}","received_events_url":"https://api.github.com/users/leonerd/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/matrix-org/synapse/pulls/457/commits","review_comments_url":"https://api.github.com/repos/matrix-org/synapse/pulls/457/comments","review_comment_url":"https://api.github.com/repos/matrix-org/synapse/pulls/comments{/number}","comments_url":"https://api.github.com/repos/matrix-org/synapse/issues/457/comments","statuses_url":"https://api.github.com/repos/matrix-org/synapse/statuses/d12c00bdc311bd0685aa7e7e70f1aa7787317164","head":{"label":"matrix-org:markjh/cached_sync","ref":"markjh/cached_sync","sha":"d12c00bdc311bd0685aa7e7e70f1aa7787317164","user":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"repo":{"id":22844864,"name":"synapse","full_name":"matrix-org/synapse","owner":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/matrix-org/synapse","description":"Synapse: Matrix reference homeserver","fork":false,"url":"https://api.github.com/repos/matrix-org/synapse","forks_url":"https://api.github.com/repos/matrix-org/synapse/forks","keys_url":"https://api.github.com/repos/matrix-org/synapse/keys{/key_id}","collaborators_url":"https://api.github.com/repos/matrix-org/synapse/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/matrix-org/synapse/teams","hooks_url":"https://api.github.com/repos/matrix-org/synapse/hooks","issue_events_url":"https://api.github.com/repos/matrix-org/synapse/issues/events{/number}","events_url":"https://api.github.com/repos/matrix-org/synapse/events","assignees_url":"https://api.github.com/repos/matrix-org/synapse/assignees{/user}","branches_url":"https://api.github.com/repos/matrix-org/synapse/branches{/branch}","tags_url":"https://api.github.com/repos/matrix-org/synapse/tags","blobs_url":"https://api.github.com/repos/matrix-org/synapse/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/matrix-org/synapse/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/matrix-org/synapse/git/refs{/sha}","trees_url":"https://api.github.com/repos/matrix-org/synapse/git/trees{/sha}","statuses_url":"https://api.github.com/repos/matrix-org/synapse/statuses/{sha}","languages_url":"https://api.github.com/repos/matrix-org/synapse/languages","stargazers_url":"https://api.github.com/repos/matrix-org/synapse/stargazers","contributors_url":"https://api.github.com/repos/matrix-org/synapse/contributors","subscribers_url":"https://api.github.com/repos/matrix-org/synapse/subscribers","subscription_url":"https://api.github.com/repos/matrix-org/synapse/subscription","commits_url":"https://api.github.com/repos/matrix-org/synapse/commits{/sha}","git_commits_url":"https://api.github.com/repos/matrix-org/synapse/git/commits{/sha}","comments_url":"https://api.github.com/repos/matrix-org/synapse/comments{/number}","issue_comment_url":"https://api.github.com/repos/matrix-org/synapse/issues/comments{/number}","contents_url":"https://api.github.com/repos/matrix-org/synapse/contents/{+path}","compare_url":"https://api.github.com/repos/matrix-org/synapse/compare/{base}...{head}","merges_url":"https://api.github.com/repos/matrix-org/synapse/merges","archive_url":"https://api.github.com/repos/matrix-org/synapse/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/matrix-org/synapse/downloads","issues_url":"https://api.github.com/repos/matrix-org/synapse/issues{/number}","pulls_url":"https://api.github.com/repos/matrix-org/synapse/pulls{/number}","milestones_url":"https://api.github.com/repos/matrix-org/synapse/milestones{/number}","notifications_url":"https://api.github.com/repos/matrix-org/synapse/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/matrix-org/synapse/labels{/name}","releases_url":"https://api.github.com/repos/matrix-org/synapse/releases{/id}","created_at":"2014-08-11T15:51:42Z","updated_at":"2015-12-22T09:49:25Z","pushed_at":"2015-12-23T15:18:17Z","git_url":"git://github.com/matrix-org/synapse.git","ssh_url":"git@github.com:matrix-org/synapse.git","clone_url":"https://github.com/matrix-org/synapse.git","svn_url":"https://github.com/matrix-org/synapse","homepage":"http://matrix.org","size":8595,"stargazers_count":632,"watchers_count":632,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":61,"mirror_url":null,"open_issues_count":7,"forks":61,"open_issues":7,"watchers":632,"default_branch":"master"}},"base":{"label":"matrix-org:develop","ref":"develop","sha":"7df276d219218a41f40079fd6728f5c15b2e1934","user":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"repo":{"id":22844864,"name":"synapse","full_name":"matrix-org/synapse","owner":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/matrix-org/synapse","description":"Synapse: Matrix reference homeserver","fork":false,"url":"https://api.github.com/repos/matrix-org/synapse","forks_url":"https://api.github.com/repos/matrix-org/synapse/forks","keys_url":"https://api.github.com/repos/matrix-org/synapse/keys{/key_id}","collaborators_url":"https://api.github.com/repos/matrix-org/synapse/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/matrix-org/synapse/teams","hooks_url":"https://api.github.com/repos/matrix-org/synapse/hooks","issue_events_url":"https://api.github.com/repos/matrix-org/synapse/issues/events{/number}","events_url":"https://api.github.com/repos/matrix-org/synapse/events","assignees_url":"https://api.github.com/repos/matrix-org/synapse/assignees{/user}","branches_url":"https://api.github.com/repos/matrix-org/synapse/branches{/branch}","tags_url":"https://api.github.com/repos/matrix-org/synapse/tags","blobs_url":"https://api.github.com/repos/matrix-org/synapse/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/matrix-org/synapse/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/matrix-org/synapse/git/refs{/sha}","trees_url":"https://api.github.com/repos/matrix-org/synapse/git/trees{/sha}","statuses_url":"https://api.github.com/repos/matrix-org/synapse/statuses/{sha}","languages_url":"https://api.github.com/repos/matrix-org/synapse/languages","stargazers_url":"https://api.github.com/repos/matrix-org/synapse/stargazers","contributors_url":"https://api.github.com/repos/matrix-org/synapse/contributors","subscribers_url":"https://api.github.com/repos/matrix-org/synapse/subscribers","subscription_url":"https://api.github.com/repos/matrix-org/synapse/subscription","commits_url":"https://api.github.com/repos/matrix-org/synapse/commits{/sha}","git_commits_url":"https://api.github.com/repos/matrix-org/synapse/git/commits{/sha}","comments_url":"https://api.github.com/repos/matrix-org/synapse/comments{/number}","issue_comment_url":"https://api.github.com/repos/matrix-org/synapse/issues/comments{/number}","contents_url":"https://api.github.com/repos/matrix-org/synapse/contents/{+path}","compare_url":"https://api.github.com/repos/matrix-org/synapse/compare/{base}...{head}","merges_url":"https://api.github.com/repos/matrix-org/synapse/merges","archive_url":"https://api.github.com/repos/matrix-org/synapse/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/matrix-org/synapse/downloads","issues_url":"https://api.github.com/repos/matrix-org/synapse/issues{/number}","pulls_url":"https://api.github.com/repos/matrix-org/synapse/pulls{/number}","milestones_url":"https://api.github.com/repos/matrix-org/synapse/milestones{/number}","notifications_url":"https://api.github.com/repos/matrix-org/synapse/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/matrix-org/synapse/labels{/name}","releases_url":"https://api.github.com/repos/matrix-org/synapse/releases{/id}","created_at":"2014-08-11T15:51:42Z","updated_at":"2015-12-22T09:49:25Z","pushed_at":"2015-12-23T15:18:17Z","git_url":"git://github.com/matrix-org/synapse.git","ssh_url":"git@github.com:matrix-org/synapse.git","clone_url":"https://github.com/matrix-org/synapse.git","svn_url":"https://github.com/matrix-org/synapse","homepage":"http://matrix.org","size":8595,"stargazers_count":632,"watchers_count":632,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":61,"mirror_url":null,"open_issues_count":7,"forks":61,"open_issues":7,"watchers":632,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/matrix-org/synapse/pulls/457"},"html":{"href":"https://github.com/matrix-org/synapse/pull/457"},"issue":{"href":"https://api.github.com/repos/matrix-org/synapse/issues/457"},"comments":{"href":"https://api.github.com/repos/matrix-org/synapse/issues/457/comments"},"review_comments":{"href":"https://api.github.com/repos/matrix-org/synapse/pulls/457/comments"},"review_comment":{"href":"https://api.github.com/repos/matrix-org/synapse/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/matrix-org/synapse/pulls/457/commits"},"statuses":{"href":"https://api.github.com/repos/matrix-org/synapse/statuses/d12c00bdc311bd0685aa7e7e70f1aa7787317164"}}}}