Add keywords cache

This commit is contained in:
Michel Roux 2019-12-07 18:31:10 +01:00
parent 374e24efc0
commit 820014e8d8
3 changed files with 22 additions and 5 deletions

View File

@ -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/*

2
app.py
View File

@ -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

View File

@ -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()