mirror of https://github.com/zulip/zulip.git
preview: Hash cache keys for preview urls.
We don't want really long urls to lead to truncated keys, or we could theoretically have two different urls get mixed up previews. Also, this suppresses warnings about exceeding the 250 char limit. Finally, this gives the key a proper prefix.
This commit is contained in:
parent
d933779477
commit
76deb30312
|
@ -293,6 +293,9 @@ def cache(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
|
||||||
|
|
||||||
return cache_with_key(keyfunc)(func)
|
return cache_with_key(keyfunc)(func)
|
||||||
|
|
||||||
|
def preview_url_cache_key(url: str) -> str:
|
||||||
|
return "preview_url:%s" % (make_safe_digest(url))
|
||||||
|
|
||||||
def display_recipient_cache_key(recipient_id: int) -> str:
|
def display_recipient_cache_key(recipient_id: int) -> str:
|
||||||
return "display_recipient_dict:%d" % (recipient_id,)
|
return "display_recipient_dict:%d" % (recipient_id,)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import traceback
|
||||||
from typing import Any, Optional, Dict
|
from typing import Any, Optional, Dict
|
||||||
from typing.re import Match
|
from typing.re import Match
|
||||||
import requests
|
import requests
|
||||||
from zerver.lib.cache import cache_with_key, get_cache_with_key
|
from zerver.lib.cache import cache_with_key, get_cache_with_key, preview_url_cache_key
|
||||||
from zerver.lib.url_preview.oembed import get_oembed_data
|
from zerver.lib.url_preview.oembed import get_oembed_data
|
||||||
from zerver.lib.url_preview.parsers import OpenGraphParser, GenericParser
|
from zerver.lib.url_preview.parsers import OpenGraphParser, GenericParser
|
||||||
from django.utils.encoding import smart_text
|
from django.utils.encoding import smart_text
|
||||||
|
@ -24,11 +24,7 @@ def is_link(url: str) -> Match[str]:
|
||||||
return link_regex.match(smart_text(url))
|
return link_regex.match(smart_text(url))
|
||||||
|
|
||||||
|
|
||||||
def cache_key_func(url: str) -> str:
|
@cache_with_key(preview_url_cache_key, cache_name=CACHE_NAME, with_statsd_key="urlpreview_data")
|
||||||
return url
|
|
||||||
|
|
||||||
|
|
||||||
@cache_with_key(cache_key_func, cache_name=CACHE_NAME, with_statsd_key="urlpreview_data")
|
|
||||||
def get_link_embed_data(url: str,
|
def get_link_embed_data(url: str,
|
||||||
maxwidth: Optional[int]=640,
|
maxwidth: Optional[int]=640,
|
||||||
maxheight: Optional[int]=480) -> Optional[Dict[str, Any]]:
|
maxheight: Optional[int]=480) -> Optional[Dict[str, Any]]:
|
||||||
|
@ -58,6 +54,6 @@ def get_link_embed_data(url: str,
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
@get_cache_with_key(cache_key_func, cache_name=CACHE_NAME)
|
@get_cache_with_key(preview_url_cache_key, cache_name=CACHE_NAME)
|
||||||
def link_embed_data_from_cache(url: str, maxwidth: Optional[int]=640, maxheight: Optional[int]=480) -> Any:
|
def link_embed_data_from_cache(url: str, maxwidth: Optional[int]=640, maxheight: Optional[int]=480) -> Any:
|
||||||
return
|
return
|
||||||
|
|
|
@ -16,7 +16,7 @@ from zerver.lib.url_preview.preview import (
|
||||||
from zerver.lib.url_preview.oembed import get_oembed_data
|
from zerver.lib.url_preview.oembed import get_oembed_data
|
||||||
from zerver.lib.url_preview.parsers import (
|
from zerver.lib.url_preview.parsers import (
|
||||||
OpenGraphParser, GenericParser)
|
OpenGraphParser, GenericParser)
|
||||||
from zerver.lib.cache import cache_set, NotFoundInCache
|
from zerver.lib.cache import cache_set, NotFoundInCache, preview_url_cache_key
|
||||||
|
|
||||||
|
|
||||||
TEST_CACHES = {
|
TEST_CACHES = {
|
||||||
|
@ -369,5 +369,6 @@ class PreviewTestCase(ZulipTestCase):
|
||||||
link_embed_data_from_cache(url)
|
link_embed_data_from_cache(url)
|
||||||
|
|
||||||
with self.settings(CACHES=TEST_CACHES):
|
with self.settings(CACHES=TEST_CACHES):
|
||||||
cache_set(url, link_embed_data, 'database')
|
key = preview_url_cache_key(url)
|
||||||
|
cache_set(key, link_embed_data, 'database')
|
||||||
self.assertEqual(link_embed_data, link_embed_data_from_cache(url))
|
self.assertEqual(link_embed_data, link_embed_data_from_cache(url))
|
||||||
|
|
Loading…
Reference in New Issue