From b547b75f03eeea34d16d9ccda15a1b4168733be8 Mon Sep 17 00:00:00 2001 From: Benex254 Date: Fri, 11 Oct 2024 09:34:40 +0300 Subject: [PATCH] feat: add environment variable that force updating of the cache db --- fastanime/cli/__init__.py | 8 ++++++++ fastanime/libs/common/requests_cacher.py | 13 ++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/fastanime/cli/__init__.py b/fastanime/cli/__init__.py index 04bcfea..43bffde 100644 --- a/fastanime/cli/__init__.py +++ b/fastanime/cli/__init__.py @@ -178,6 +178,9 @@ signal.signal(signal.SIGINT, handle_exit) help="the player to use when streaming", type=click.Choice(["mpv", "vlc"]), ) +@click.option( + "--fresh-requests", is_flag=True, help="Force the requests cache to be updated" +) @click.pass_context def run_cli( ctx: click.Context, @@ -212,7 +215,10 @@ def run_cli( use_python_mpv, sync_play, player, + fresh_requests, ): + import os + from .config import Config ctx.obj = Config() @@ -251,6 +257,8 @@ def run_cli( install() + if fresh_requests: + os.environ["FASTANIME_FRESH_REQUESTS"] = "1" if sync_play: ctx.obj.sync_play = sync_play if provider: diff --git a/fastanime/libs/common/requests_cacher.py b/fastanime/libs/common/requests_cacher.py index 87aa121..b20e9fa 100644 --- a/fastanime/libs/common/requests_cacher.py +++ b/fastanime/libs/common/requests_cacher.py @@ -1,5 +1,6 @@ import json import logging +import os import re import time from datetime import datetime @@ -49,7 +50,7 @@ class CachedRequestsSession(requests.Session): def __init__( self, cache_db_path: str, - max_lifetime: int = 604800, + max_lifetime: int = 259200, max_size: int = (1024**2) * 10, table_name: str = "fastanime_requests_cache", clean_db=False, @@ -89,16 +90,10 @@ class CachedRequestsSession(requests.Session): url, params=None, force_caching=False, - fresh=0, + fresh=int(os.environ.get("FASTANIME_FRESH_REQUESTS", 0)), *args, **kwargs, ): - # TODO: improve the caching functionality and add a layer to auto delete - # expired requests - if fresh: - logger.debug("Executing fresh request") - return super().request(method, url, params=params, *args, **kwargs) - if params: url += "?" + urlencode(params) @@ -128,7 +123,7 @@ class CachedRequestsSession(requests.Session): cached_request = cursor.fetchone() time_after_access_db = datetime.now() - if cached_request: + if cached_request and not fresh: logger.debug("Found existing request in cache") ( status_code,