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 FROM debian
RUN apt-get update ; apt-get -y upgrade ; \ 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 ; \ 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/* rm -rf /var/lib/apt/lists/*

2
app.py
View File

@ -16,7 +16,7 @@ def verify_password(username, password):
def boldify(name): def boldify(name):
query = request.args.get('q') query = request.args.get('q')
name = Connector.boldify(name, query) name = Connector.boldify(name, query)
for title in AnimeTitle.query.all(): for title in ConnectorCache.get_keywords():
if title.keyword is not query: if title.keyword is not query:
name = Connector.boldify(name, title.keyword) name = Connector.boldify(name, title.keyword)
return name return name

View File

@ -10,7 +10,7 @@ from sys import platform
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from models import AnimeLink from models import AnimeLink, AnimeTitle
class ConnectorReturn(Enum): class ConnectorReturn(Enum):
@ -26,6 +26,9 @@ class ConnectorLang(Enum):
class Cache: class Cache:
CACHE_TIMEOUT = 60 * 60 CACHE_TIMEOUT = 60 * 60
CACHE_DATA = {} CACHE_DATA = {}
CACHE_KEYWORDS = {
'timeout': 0.0
}
def cache_data(self, f): def cache_data(self, f):
@wraps(f) @wraps(f)
@ -41,7 +44,7 @@ class Cache:
self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query] = {} 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]: 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] = { 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] cached_data = self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page]
@ -61,6 +64,17 @@ class Cache:
return wrapper 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() ConnectorCache = Cache()