From 820014e8d8b93e9705bde279edc83686a0a3e7d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?X=C3=A9fir=20Destiny?= Date: Sat, 7 Dec 2019 18:31:10 +0100 Subject: [PATCH] Add keywords cache --- Dockerfile | 7 +++++-- app.py | 2 +- connectors.py | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 831c15a..60b83c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,8 @@ FROM debian -RUN apt-get update ; apt-get -y upgrade ; \ - apt-get -y install python3 python3-flask python3-flask-sqlalchemy python3-flask-httpauth python3-flaskext.wtf python3-pymysql python3-requests python3-bs4 phantomjs ; \ +RUN apt-get update ; apt-get -y upgrade && \ + apt-get -y install python3 python3-pip \ + python3-flask python3-flask-sqlalchemy python3-flask-httpauth python3-flaskext.wtf \ + python3-pymysql python3-requests python3-bs4 && \ + apt-get -y --no-install-recommends install phantomjs && \ rm -rf /var/lib/apt/lists/* diff --git a/app.py b/app.py index 74cc1a0..752740c 100644 --- a/app.py +++ b/app.py @@ -16,7 +16,7 @@ def verify_password(username, password): def boldify(name): query = request.args.get('q') name = Connector.boldify(name, query) - for title in AnimeTitle.query.all(): + for title in ConnectorCache.get_keywords(): if title.keyword is not query: name = Connector.boldify(name, title.keyword) return name diff --git a/connectors.py b/connectors.py index be80ff0..9c2b328 100644 --- a/connectors.py +++ b/connectors.py @@ -10,7 +10,7 @@ from sys import platform import requests from bs4 import BeautifulSoup -from models import AnimeLink +from models import AnimeLink, AnimeTitle class ConnectorReturn(Enum): @@ -26,6 +26,9 @@ class ConnectorLang(Enum): class Cache: CACHE_TIMEOUT = 60 * 60 CACHE_DATA = {} + CACHE_KEYWORDS = { + 'timeout': 0.0 + } def cache_data(self, f): @wraps(f) @@ -41,7 +44,7 @@ class Cache: self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query] = {} if connector.page not in self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query]: self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page] = { - 'timeout': 0 + 'timeout': 0.0 } cached_data = self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page] @@ -61,6 +64,17 @@ class Cache: return wrapper + def get_keywords(self): + timestamp = datetime.now().timestamp() + if self.CACHE_KEYWORDS['timeout'] > timestamp: + self.CACHE_KEYWORDS['data'] = AnimeTitle.query.all() + self.CACHE_KEYWORDS['timeout'] = timestamp + + def clear_keywords(self): + self.CACHE_KEYWORDS = { + 'timeout': 0.0 + } + ConnectorCache = Cache()