This repository has been archived on 2020-11-22. You can view files and clone it, but cannot push or open issues/pull-requests.
mangareader/disk_cache.py

34 lines
848 B
Python
Raw Permalink Normal View History

2019-08-07 22:35:22 +02:00
from io import BytesIO
import os
class DiskCache:
def __init__(self, path=None, enabled=True):
if path is None:
2020-02-15 15:41:11 +01:00
self.path = os.path.join(os.path.dirname(__file__), "cache")
2019-08-07 22:35:22 +02:00
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
2020-02-15 15:41:11 +01:00
with open(self._build_path(identifier), "wb") as f:
2019-08-07 22:35:22 +02:00
f.write(buf.read())
def get(self, identifier):
if self.enabled is False:
raise FileNotFoundError
2020-02-15 15:41:11 +01:00
with open(self._build_path(identifier), "rb") as f:
2019-08-07 22:35:22 +02:00
return BytesIO(f.read())
def _build_path(self, identifier):
return os.path.join(self.path, identifier)