Skip to content

Commit

Permalink
Disable cache with CLI option
Browse files Browse the repository at this point in the history
Introduce the `--no-cache` command-line option to disable caching for HTTP requests. This enables users to run the program without using the cache, ensuring fresh data retrieval on each run.
  • Loading branch information
dvershinin committed Sep 8, 2024
1 parent 17c5fe6 commit dde0707
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/lastversion/lastversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import yaml
from packaging.version import InvalidVersion

from lastversion.repo_holders.base import BaseProjectHolder
from lastversion.repo_holders.test import TestProjectHolder
from lastversion.repo_holders.github import TOKEN_PRO_TIP
from lastversion.holder_factory import HolderFactory
Expand Down Expand Up @@ -723,6 +724,12 @@ def main(argv=None):
action="store_true",
help="Automatically answer yes for all questions",
)
parser.add_argument(
"--no-cache",
dest="no_cache",
action="store_true",
help="Do not use cache for HTTP requests",
)
parser.add_argument("--version", action=VersionAction)
parser.set_defaults(
validate=True,
Expand All @@ -742,6 +749,8 @@ def main(argv=None):
)
args = parser.parse_args(argv)

BaseProjectHolder.CACHE_DISABLED = args.no_cache

if args.repo == "self":
args.repo = __self__

Expand Down
25 changes: 17 additions & 8 deletions src/lastversion/repo_holders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class BaseProjectHolder(requests.Session):
REPO_IS_HOLDER = False
DEFAULT_TIMEOUT = 30 # default timeout in seconds

CACHE_DISABLED = False

@property
def name(self):
"""Get project name, useful in URLs for assets, etc."""
Expand All @@ -102,13 +104,18 @@ def __init__(self, name=None, hostname=None):
self.mount("https://", requests.adapters.HTTPAdapter(max_retries=5))
app_name = __name__.split(".", maxsplit=1)[0]

self.cache_dir = user_cache_dir(app_name)
log.info("Using cache directory: %s.", self.cache_dir)
self.cache = FileCache(self.cache_dir)
cache_adapter = CacheControlAdapter(cache=self.cache)
# noinspection HttpUrlsUsage
self.mount("http://", cache_adapter)
self.mount("https://", cache_adapter)
self.cache_dir = None
self.cache = None
if not self.CACHE_DISABLED:
self.cache_dir = user_cache_dir(app_name)
log.info("Using cache directory: %s.", self.cache_dir)
self.cache = FileCache(self.cache_dir)
cache_adapter = CacheControlAdapter(cache=self.cache)
# noinspection HttpUrlsUsage
self.mount("http://", cache_adapter)
self.mount("https://", cache_adapter)
else:
log.info("Cache is disabled for this holder.")

self.names_cache_filename = f"{self.cache_dir}/repos.json"

Expand Down Expand Up @@ -136,7 +143,7 @@ def request(self, *args, **kwargs):

def get_name_cache(self):
"""Return name cache from file."""
if not os.path.exists(self.names_cache_filename):
if self.CACHE_DISABLED or not os.path.exists(self.names_cache_filename):
return {}
try:
with open(self.names_cache_filename, "r", encoding="utf-8") as reader:
Expand All @@ -148,6 +155,8 @@ def get_name_cache(self):

def update_name_cache(self, cache_data):
"""Update name cache file with new data."""
if self.CACHE_DISABLED:
return
try:
ensure_directory_exists(self.cache_dir)
with open(self.names_cache_filename, "w", encoding="utf-8") as writer:
Expand Down

0 comments on commit dde0707

Please sign in to comment.