From 2a726b7dfb96e0fc7ad3aea6ef797769e9fe31cf Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 11 Aug 2019 13:56:18 +0200 Subject: [PATCH] Make a query before spawning cache actor task --- core/indexes.py | 4 ++++ core/tasks.py | 6 +++++- utils/media.py | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/core/indexes.py b/core/indexes.py index 9f5a7db..d57b006 100644 --- a/core/indexes.py +++ b/core/indexes.py @@ -1,6 +1,7 @@ import pymongo from config import DB +from config import MEDIA_CACHE from core.meta import MetaKey from core.meta import _meta @@ -73,3 +74,6 @@ def create_indexes(): ("meta.deleted", pymongo.ASCENDING), ] ) + + # For the is_actor_icon_cached query + MEDIA_CACHE.fs._GridFS__files.create_index([("url", 1), ("kind", 1)]) diff --git a/core/tasks.py b/core/tasks.py index 13a9d04..131dc60 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -4,6 +4,7 @@ from datetime import timezone from poussetaches import PousseTaches +from config import MEDIA_CACHE from utils import parse_datetime p = PousseTaches( @@ -25,7 +26,10 @@ class Tasks: ) @staticmethod - def cache_actor_icon(icon_url: str, actor_iri: str): + def cache_actor_icon(icon_url: str, actor_iri: str) -> None: + if MEDIA_CACHE.is_actor_icon_cached(icon_url): + return None + p.push({"icon_url": icon_url, "actor_iri": actor_iri}, "/task/cache_actor_icon") @staticmethod diff --git a/utils/media.py b/utils/media.py index d26c822..54b7876 100644 --- a/utils/media.py +++ b/utils/media.py @@ -136,8 +136,11 @@ class MediaCache(object): remote_id=remote_id, ) + def is_actor_icon_cached(self, url: str) -> bool: + return bool(self.fs.find_one({"url": url, "kind": Kind.ACTOR_ICON.value})) + def cache_actor_icon(self, url: str) -> None: - if self.fs.find_one({"url": url, "kind": Kind.ACTOR_ICON.value}): + if self.is_actor_icon_cached(url): return i = load(url, self.user_agent) for size in [50, 80]: