[O] Cache only on success

This commit is contained in:
2026-03-09 18:44:08 -04:00
parent 8d917af8fd
commit 1fe6d1ef14
2 changed files with 24 additions and 14 deletions
+14 -11
View File
@@ -3,7 +3,7 @@ import hashlib
from pathlib import Path from pathlib import Path
from functools import wraps from functools import wraps
def _disk_cache_decorator(subdir_name: str, ext: str, read_func, write_func): def _disk_cache_decorator(subdir_name: str, ext: str, read_func, write_func, should_cache = None):
""" """
Generic internal caching decorator handling filename hashing and io abstraction. Generic internal caching decorator handling filename hashing and io abstraction.
""" """
@@ -30,19 +30,20 @@ def _disk_cache_decorator(subdir_name: str, ext: str, read_func, write_func):
result = func(*args, **kwargs) result = func(*args, **kwargs)
cache_p.parent.mkdir(parents=True, exist_ok=True) if should_cache is None or should_cache(result):
write_func(cache_p, result) cache_p.parent.mkdir(parents=True, exist_ok=True)
write_func(cache_p, result)
# Write arguments to a .txt file for easy lookup
txt_p = cache_p.with_suffix('.txt') # Write arguments to a .txt file for easy lookup
txt_p.write_text(f"Args: {args}\nKwargs: {kwargs}", encoding="utf-8") txt_p = cache_p.with_suffix('.txt')
txt_p.write_text(f"Args: {args}\nKwargs: {kwargs}", encoding="utf-8")
return result return result
return wrapper return wrapper
return decorator return decorator
def with_disk_cache(subdir_name: str): def with_disk_cache(subdir_name: str, should_cache = None):
""" """
A decorator to cache function results to a local JSON file. A decorator to cache function results to a local JSON file.
The cache file is stored in `data/<subdir_name>/<key>.json`. The cache file is stored in `data/<subdir_name>/<key>.json`.
@@ -51,11 +52,12 @@ def with_disk_cache(subdir_name: str):
subdir_name, subdir_name,
".json", ".json",
read_func=lambda p: json.loads(p.read_text(encoding="utf-8")), read_func=lambda p: json.loads(p.read_text(encoding="utf-8")),
write_func=lambda p, res: p.write_text(json.dumps(res, ensure_ascii=False, indent=2), encoding="utf-8") write_func=lambda p, res: p.write_text(json.dumps(res, ensure_ascii=False, indent=2), encoding="utf-8"),
should_cache=should_cache
) )
def with_binary_disk_cache(subdir_name: str, ext: str = ".bin"): def with_binary_disk_cache(subdir_name: str, ext: str = ".bin", should_cache = None):
""" """
A decorator to cache binary function results to a local file. A decorator to cache binary function results to a local file.
The cache file is stored in `data/<subdir_name>/<key><ext>`. The cache file is stored in `data/<subdir_name>/<key><ext>`.
@@ -64,5 +66,6 @@ def with_binary_disk_cache(subdir_name: str, ext: str = ".bin"):
subdir_name, subdir_name,
ext, ext,
read_func=lambda p: p.read_bytes(), read_func=lambda p: p.read_bytes(),
write_func=lambda p, res: p.write_bytes(res) write_func=lambda p, res: p.write_bytes(res),
should_cache=should_cache
) )
+10 -3
View File
@@ -22,7 +22,14 @@ def _get_mteam_headers() -> dict:
} }
@with_disk_cache('search_mteam_torrents') def is_mteam_success(res) -> bool:
"""Predicate to check if an M-Team API response is successful."""
if isinstance(res, dict):
code = str(res.get("code", ""))
return code == "0" or code == "200"
return False
@with_disk_cache('search_mteam_torrents', should_cache=is_mteam_success)
def search_mteam_torrents(imdb_url: str, page_number: int = 1, page_size: int = 100) -> dict: def search_mteam_torrents(imdb_url: str, page_number: int = 1, page_size: int = 100) -> dict:
""" """
Search M-Team for torrents using IMDb URL. Search M-Team for torrents using IMDb URL.
@@ -51,7 +58,7 @@ def search_mteam_torrents(imdb_url: str, page_number: int = 1, page_size: int =
return response.json() return response.json()
@with_disk_cache('mteam_imdb_info') @with_disk_cache('mteam_imdb_info', should_cache=is_mteam_success)
def mteam_imdb_info(id: str) -> dict: def mteam_imdb_info(id: str) -> dict:
""" """
Search M-Team for torrents using IMDb URL. Search M-Team for torrents using IMDb URL.
@@ -145,4 +152,4 @@ def format_mteam_torrent(t: dict) -> str:
if __name__ == "__main__": if __name__ == "__main__":
print(mteam_imdb_info('tt38872297')) print(mteam_imdb_info('tt0371724'))