Skip to content

Commit 0354fd5

Browse files
committed
Modularize tasks code into versions and cache file to split up the big file a bit
1 parent 16307ad commit 0354fd5

File tree

6 files changed

+366
-218
lines changed

6 files changed

+366
-218
lines changed

.gitignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Cache directory
2+
.cache/
3+
4+
# Python
5+
__pycache__/
6+
*.pyc
7+
*.pyo
8+
*.pyd
9+
.Python
10+
env/
11+
venv/
12+
.venv/
13+
pip-log.txt
14+
pip-delete-this-directory.txt
15+
.tox/
16+
.coverage
17+
.coverage.*
18+
.cache
19+
nosetests.xml
20+
coverage.xml
21+
*.cover
22+
*.log
23+
.git
24+
.mypy_cache
25+
.pytest_cache
26+
.hypothesis
27+
28+
# Virtual environments
29+
.env
30+
.venv
31+
env/
32+
venv/
33+
ENV/
34+
env.bak/
35+
venv.bak/
36+
37+
# IDE
38+
.vscode/
39+
.idea/
40+
*.swp
41+
*.swo
42+
*~
43+
44+
# OS
45+
.DS_Store
46+
.DS_Store?
47+
._*
48+
.Spotlight-V100
49+
.Trashes
50+
ehthumbs.db
51+
Thumbs.db

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ help:
1515
@echo " push push Redis Docker images (use VERSION and CPU variables)"
1616
@echo " list list all available Redis versions"
1717
@echo " list-releases list Redis releases from GitHub"
18+
@echo ""
19+
@echo "Cache management tasks:"
20+
@echo " clear-cache clear the GitHub API cache"
1821

1922
# Default values
2023
CPU ?= 2
@@ -53,3 +56,9 @@ list:
5356

5457
list-releases:
5558
invoke list-releases
59+
60+
# Cache management
61+
clear-cache:
62+
rm -rf .cache/
63+
64+
.PHONY: clear-cache

cache.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
GitHub API caching functionality for Redis Docker Cluster Build Tool.
3+
4+
Provides persistent caching of GitHub API responses to reduce API calls
5+
and improve performance across multiple invocations.
6+
"""
7+
8+
import os
9+
import json
10+
import atexit
11+
from time import time
12+
from cachetools import TTLCache, cached
13+
14+
15+
# Cache configuration
16+
CACHE_FILE = ".cache/github_releases.json"
17+
cache = TTLCache(maxsize=128, ttl=1800) # 30-minute TTL
18+
19+
20+
def ensure_cache_dir():
21+
"""Ensure the cache directory exists."""
22+
os.makedirs(os.path.dirname(CACHE_FILE), exist_ok=True)
23+
24+
25+
def load_cache():
26+
"""Load cache from file if it exists."""
27+
try:
28+
with open(CACHE_FILE, 'r') as f:
29+
data = json.load(f)
30+
# Only load entries not expired
31+
now = time()
32+
for key, (value, expiry) in data.items():
33+
if now < expiry:
34+
cache[key] = (value, expiry)
35+
except (FileNotFoundError, json.JSONDecodeError, KeyError):
36+
pass
37+
38+
39+
def save_cache():
40+
"""Save cache to file."""
41+
ensure_cache_dir()
42+
data = {key: (value, expiry) for key, (value, expiry) in cache.items()}
43+
with open(CACHE_FILE, 'w') as f:
44+
json.dump(data, f)
45+
46+
47+
# Load cache on module import
48+
load_cache()
49+
50+
# Save cache on exit
51+
atexit.register(save_cache)
52+
53+
54+
@cached(cache)
55+
def fetch_github_releases_cached():
56+
"""
57+
Fetch Redis releases from GitHub API with caching.
58+
59+
This function is cached for 30 minutes to avoid excessive API calls.
60+
Returns list of valid semver release versions (excludes RCs, betas, etc.)
61+
Exits with error if GitHub cannot be reached or no versions found.
62+
"""
63+
# Import here to avoid circular imports
64+
from .versions import fetch_github_releases
65+
return fetch_github_releases()

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
invoke>=2.2.0
22
requests>=2.31.0
3+
cachetools>=5.3.0

0 commit comments

Comments
 (0)