summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
Diffstat (limited to 'synapse')
-rw-r--r--synapse/appservice/api.py32
1 files changed, 21 insertions, 11 deletions
diff --git a/synapse/appservice/api.py b/synapse/appservice/api.py
index acef09609c..9de5c6e47a 100644
--- a/synapse/appservice/api.py
+++ b/synapse/appservice/api.py
@@ -17,6 +17,7 @@ from twisted.internet import defer
 from synapse.api.errors import CodeMessageException
 from synapse.http.client import SimpleHttpClient
 from synapse.events.utils import serialize_event
+from synapse.util.caches.response_cache import ResponseCache
 from synapse.types import ThirdPartyEntityKind
 
 import logging
@@ -25,6 +26,9 @@ import urllib
 logger = logging.getLogger(__name__)
 
 
+HOUR_IN_MS = 60 * 60 * 1000
+
+
 def _is_valid_3pe_result(r, field):
     if not isinstance(r, dict):
         return False
@@ -56,6 +60,8 @@ class ApplicationServiceApi(SimpleHttpClient):
         super(ApplicationServiceApi, self).__init__(hs)
         self.clock = hs.get_clock()
 
+        self.protocol_meta_cache = ResponseCache(hs, timeout_ms=1*HOUR_IN_MS)
+
     @defer.inlineCallbacks
     def query_user(self, service, user_id):
         uri = service.url + ("/users/%s" % urllib.quote(user_id))
@@ -131,18 +137,22 @@ class ApplicationServiceApi(SimpleHttpClient):
             logger.warning("query_3pe to %s threw exception %s", uri, ex)
             defer.returnValue([])
 
-    @defer.inlineCallbacks
     def get_3pe_protocol(self, service, protocol):
-        # TODO: cache
-        uri = "%s/thirdparty/protocol/%s" % (service.url, urllib.quote(protocol))
-        try:
-            response = yield self.get_json(uri, {})
-            defer.returnValue(response)
-        except Exception as ex:
-            logger.warning("query_3pe_protocol to %s threw exception %s",
-                uri, ex
-            )
-            defer.returnValue({})
+        @defer.inlineCallbacks
+        def _get():
+            uri = "%s/thirdparty/protocol/%s" % (service.url, urllib.quote(protocol))
+            try:
+                defer.returnValue((yield self.get_json(uri, {})))
+            except Exception as ex:
+                logger.warning("query_3pe_protocol to %s threw exception %s",
+                    uri, ex
+                )
+                defer.returnValue({})
+
+        key = (service.id, protocol)
+        return self.protocol_meta_cache.get(key) or (
+            self.protocol_meta_cache.set(key, _get())
+        )
 
     @defer.inlineCallbacks
     def push_bulk(self, service, events, txn_id=None):