Add disk cache
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Simon Bruder 2019-08-07 20:35:22 +00:00
parent 7c8ee5b70f
commit 79a348acc9
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
4 changed files with 55 additions and 7 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
cache
node_modules node_modules
/frontend/dist /frontend/dist

View file

@ -41,7 +41,9 @@ RUN apk add --no-cache --virtual .deps \
COPY --from=frontend /usr/src/app/frontend/dist/ frontend/dist COPY --from=frontend /usr/src/app/frontend/dist/ frontend/dist
COPY [^frontend]* ./ COPY *.py ./
VOLUME ["/usr/src/app/cache/"]
ENTRYPOINT ["gunicorn", "mangareader:app", "--bind", "0.0.0.0:8000", "--chdir", "/library"] ENTRYPOINT ["gunicorn", "mangareader:app", "--bind", "0.0.0.0:8000", "--chdir", "/library"]

View file

@ -1,4 +1,5 @@
from PIL import Image from PIL import Image
from disk_cache import DiskCache
from io import BytesIO from io import BytesIO
from mimetypes import types_map as mimetypes from mimetypes import types_map as mimetypes
from zipfile import ZipFile from zipfile import ZipFile
@ -10,6 +11,10 @@ import werkzeug.exceptions as exceptions
mimetypes['.webp'] = 'image/webp' mimetypes['.webp'] = 'image/webp'
if os.environ.get('DISK_CACHE', '1') == '0':
disk_cache = DiskCache(enabled=False)
else:
disk_cache = DiskCache()
thumbnail_cache = {} thumbnail_cache = {}
@ -231,12 +236,18 @@ class CalibreDB:
if self.webp is True and mimetype != 'image/webp': if self.webp is True and mimetype != 'image/webp':
mimetype = 'image/webp' mimetype = 'image/webp'
with volume.open(page_filename) as orig_page_data: try:
image = Image.open(orig_page_data) page_data = disk_cache.get(f'{volume_id}-{page_number}')
image.thumbnail((self.webp_size, self.webp_size)) except FileNotFoundError:
image = image.convert('RGB') with volume.open(page_filename) as orig_page_data:
image = webp.WebPPicture.from_pil(image) image = Image.open(orig_page_data)
page_data = BytesIO(image.encode(self.webp_config).buffer()) image.thumbnail((self.webp_size, self.webp_size))
image = image.convert('RGB')
image = webp.WebPPicture.from_pil(image)
page_data = BytesIO(image.encode(self.webp_config).buffer())
disk_cache.set(f'{volume_id}-{page_number}', page_data)
page_data.seek(0)
else: else:
page_data = BytesIO() page_data = BytesIO()
page_data.write(volume.read(page_filename)) page_data.write(volume.read(page_filename))

33
disk_cache.py Normal file
View file

@ -0,0 +1,33 @@
from io import BytesIO
import os
class DiskCache:
def __init__(self, path=None, enabled=True):
if path is None:
self.path = os.path.join(os.path.dirname(__file__), 'cache')
else:
self.path = path
self.enabled = enabled
if self.enabled is False:
return
os.makedirs(self.path, exist_ok=True)
def set(self, identifier, buf):
if self.enabled is False:
return
with open(self._build_path(identifier), 'wb') as f:
f.write(buf.read())
def get(self, identifier):
if self.enabled is False:
raise FileNotFoundError
with open(self._build_path(identifier), 'rb') as f:
return BytesIO(f.read())
def _build_path(self, identifier):
return os.path.join(self.path, identifier)