feat: add environment variable that force updating of the cache db

This commit is contained in:
Benex254
2024-10-11 09:34:40 +03:00
parent 58c7427a47
commit b547b75f03
2 changed files with 12 additions and 9 deletions

View File

@@ -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:

View File

@@ -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,