From f3dc4995f1845e1d149ccac8ce5ced77df942296 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sat, 17 Aug 2019 10:11:57 +0200 Subject: [PATCH] Fix the media URL cache --- requirements.txt | 1 + utils/template_filters.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 6440d98..3e81189 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +cachetools poussetaches python-dateutil libsass diff --git a/utils/template_filters.py b/utils/template_filters.py index aa10097..7eb3701 100644 --- a/utils/template_filters.py +++ b/utils/template_filters.py @@ -2,7 +2,6 @@ import logging import urllib from datetime import datetime from datetime import timezone -from functools import lru_cache from urllib.parse import urlparse import bleach @@ -10,6 +9,7 @@ import emoji_unicode import flask import html2text import timeago +from cachetools import LRUCache from little_boxes import activitypub as ap from little_boxes.activitypub import _to_list from little_boxes.errors import ActivityGoneError @@ -232,11 +232,20 @@ def get_total_answers_count(obj, meta): return cnt -@lru_cache(512) +_FILE_URL_CACHE = LRUCache(4096) + + def _get_file_url(url, size, kind) -> str: - doc = MEDIA_CACHE.get_file(url, size, kind) + k = (url, size, kind) + cached = _FILE_URL_CACHE.get(k) + if cached: + return cached + + doc = MEDIA_CACHE.get_file(*k) if doc: - return f"/media/{str(doc._id)}" + out = f"/media/{str(doc._id)}" + _FILE_URL_CACHE[k] = out + return out # MEDIA_CACHE.cache(url, kind) _logger.error(f"cache not available for {url}/{size}/{kind}")