From 2a9f24aee326d60601e83058f467b0f9fe74a656 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 14 Jan 2025 16:32:04 -0800 Subject: [PATCH] cache Protocol.for_id in memcache for #1149 --- protocol.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/protocol.py b/protocol.py index f14ae4b7..38a4f315 100644 --- a/protocol.py +++ b/protocol.py @@ -283,7 +283,21 @@ def key_for(cls, id, allow_opt_out=False): return cls(id=id).key + @staticmethod + def _for_id_memcache_key(id, remote=None): + """If id is a URL, uses its domain, otherwise returns None. + + Args: + id (str) + + Returns: + (str domain, bool remote) or None + """ + if remote and util.is_web(id): + return util.domain_from_link(id) + @cached(LRUCache(20000), lock=Lock()) + @memcache.memoize(key=_for_id_memcache_key, write=lambda id, remote: remote) @staticmethod def for_id(id, remote=True): """Returns the protocol for a given id.