-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
76 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,5 +109,6 @@ venv.bak/ | |
.idea | ||
noresults.txt | ||
*.ini | ||
*.bin | ||
*.txt | ||
*.json |
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,38 @@ | ||
import bz2 | ||
import json | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
|
||
class Cache: | ||
_cache: Dict[str, str] | ||
filepath: Path = Path(__file__).parent.joinpath("cache.bin") | ||
|
||
def __init__(self): | ||
self._cache = {} | ||
if self.filepath.is_file(): | ||
self._load(self.filepath) | ||
|
||
def __contains__(self, item): | ||
return item in self._cache | ||
|
||
def __getitem__(self, key): | ||
return self._cache[key] | ||
|
||
def __setitem__(self, key, value): | ||
self._cache[key] = value | ||
|
||
def save(self): | ||
self._save(self.filepath) | ||
|
||
def _load(self, path: Path): | ||
with open(path, "rb") as file: | ||
data = bz2.decompress(file.read()) | ||
|
||
self._cache.update(json.loads(data)) | ||
|
||
def _save(self, path: Path): | ||
byte_data = json.dumps(self._cache) | ||
compressed_byte_data = bz2.compress(byte_data.encode("utf8")) | ||
with open(path, "wb") as file: | ||
file.write(compressed_byte_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
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
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