This repository has been archived on 2020-11-22. You can view files and clone it, but cannot push or open issues or pull requests.
mangareader/disk_cache.py
Simon Bruder 79a348acc9
All checks were successful
continuous-integration/drone/push Build is passing
Add disk cache
2019-08-07 20:54:55 +00:00

34 lines
848 B
Python

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)