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:
Steve Howell 2018-10-14 12:41:15 +00:00 committed by Tim Abbott
parent d933779477
commit 76deb30312
3 changed files with 9 additions and 9 deletions

View File

@ -293,6 +293,9 @@ def cache(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
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:
return "display_recipient_dict:%d" % (recipient_id,)

View File

@ -4,7 +4,7 @@ import traceback
from typing import Any, Optional, Dict
from typing.re import Match
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.parsers import OpenGraphParser, GenericParser
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))
def cache_key_func(url: str) -> str:
return url
@cache_with_key(cache_key_func, cache_name=CACHE_NAME, with_statsd_key="urlpreview_data")
@cache_with_key(preview_url_cache_key, cache_name=CACHE_NAME, with_statsd_key="urlpreview_data")
def get_link_embed_data(url: str,
maxwidth: Optional[int]=640,
maxheight: Optional[int]=480) -> Optional[Dict[str, Any]]:
@ -58,6 +54,6 @@ def get_link_embed_data(url: str,
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:
return

View File

@ -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.parsers import (
OpenGraphParser, GenericParser)
from zerver.lib.cache import cache_set, NotFoundInCache
from zerver.lib.cache import cache_set, NotFoundInCache, preview_url_cache_key
TEST_CACHES = {
@ -369,5 +369,6 @@ class PreviewTestCase(ZulipTestCase):
link_embed_data_from_cache(url)
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))