-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from mik3y/mikey/s3-cache
feature: add optional cache layer to `S3Dao`
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
import logging | ||
import hashlib | ||
from pathlib import Path | ||
from cmoncrawl.common.types import DomainRecord | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def cache_key(record: DomainRecord): | ||
"""Returns an opaque key / filename for caching a `DomainRecord`.""" | ||
h = hashlib.sha256() | ||
h.update(record.filename.encode()) | ||
h.update("|".encode()) | ||
h.update(str(record.offset).encode()) | ||
h.update("|".encode()) | ||
h.update(str(record.length).encode()) | ||
return f"{h.hexdigest()}.bin" | ||
|
||
|
||
class AbstractDomainRecordCache: | ||
"""Cache interface for DomainRecords.""" | ||
|
||
def get(self, record: DomainRecord) -> bytes | None: | ||
raise NotImplementedError | ||
|
||
def set(self, record: DomainRecord, data: bytes) -> None: | ||
raise NotImplementedError | ||
|
||
|
||
class DomainRecordFilesystemCache(AbstractDomainRecordCache): | ||
"""A local filesystem cache. | ||
If `cache_dir` does not exist, the implementation will attempt | ||
to create it upon first `set()` using `os.makedirs`. | ||
Entries are never pruned (no TTL support currently). | ||
""" | ||
|
||
def __init__(self, cache_dir: Path): | ||
super().__init__() | ||
self.cache_dir = cache_dir | ||
|
||
def get(self, record: DomainRecord) -> bytes | None: | ||
cache_path = self.cache_dir / Path(cache_key(record)) | ||
if cache_path.exists(): | ||
with open(cache_path, "rb") as fp: | ||
logger.debug(f"reading data for {record.url} from filesystem cache") | ||
return fp.read() | ||
return None | ||
|
||
def set(self, record: DomainRecord, data: bytes) -> None: | ||
if not self.cache_dir.exists(): | ||
logger.info(f"Creating cache dir {self.cache_dir}") | ||
os.makedirs(str(self.cache_dir)) | ||
cache_path = self.cache_dir / Path(cache_key(record)) | ||
with open(cache_path, "wb") as fp: | ||
logger.debug(f"writing data for {record.url} to filesystem cache") | ||
fp.write(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters