⚡ Bolt: [Cache Printify API Variant Fetches]#43
Conversation
Added @functools.lru_cache to `get_variants` in `printify_manager.py`, `create_merch.py`, and `merch_factory.py`. Modified return types to `tuple` to ensure thread safety and immutability for cached items. Co-authored-by: merg357 <221854052+merg357@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes bulk Printify product-creation scripts by memoizing get_variants calls so repeated blueprint/provider variant lookups don’t repeatedly hit the Printify API during loops.
Changes:
- Added
@functools.lru_cache(maxsize=None)toget_variantsin multiple scripts to eliminate repeated API calls for the same blueprint/provider combination. - Changed variant ID collections returned by
get_variantsfrom mutable lists to immutable tuples (and updated relevant type hints). - Added a Bolt learning note documenting the caching convention for future bulk scripts.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| printify_manager.py | Memoizes get_variants and returns variant IDs as a tuple to avoid repeated Printify calls. |
| merch_factory.py | Memoizes get_variants, returns tuple variant IDs, and updates create_product signature accordingly. |
| create_merch.py | Memoizes get_variants, returns Tuple[int, ...], and updates create_product signature accordingly. |
| .jules/bolt.md | Documents the caching pattern as a repeatable guideline for bulk scripts. |
Comments suppressed due to low confidence (1)
printify_manager.py:77
- The comment says the "Returned list is cast to a tuple", but the implementation constructs a tuple directly (no intermediate list). Consider rewording to avoid implying an extra allocation (e.g., "Return IDs as an immutable tuple").
# Memoizing `get_variants` to prevent N+1 network bottlenecks during bulk creation loops.
# Impact: Eliminates duplicate network requests for same blueprint/provider combo.
# Safety: Returned list is cast to a tuple to ensure cache immutability and thread safety.
@functools.lru_cache(maxsize=None)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| resp = requests.get(url, headers=HEADERS) |
| def get_variants(blueprint_id: int, print_provider_id: int = 29) -> Tuple[int, ...]: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{print_provider_id}/variants.json" | ||
| resp = requests.get(url, headers=get_headers()) | ||
| resp.raise_for_status() | ||
| data = resp.json() | ||
| variants = data.get("variants", []) | ||
| return [v["id"] for v in variants[:3]] if variants else [] | ||
| return tuple(v["id"] for v in variants[:3]) if variants else () |
| # Memoizing `get_variants` to prevent N+1 network bottlenecks during bulk creation loops. | ||
| # Impact: Eliminates duplicate network requests for same blueprint/provider combo. | ||
| # Safety: Returned list is cast to a tuple to ensure cache immutability and thread safety. | ||
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| r = requests.get(url, headers=AUTH_H, timeout=15) | ||
| r.raise_for_status() | ||
| return [v["id"] for v in r.json().get("variants", [])[:4]] | ||
| return tuple(v["id"] for v in r.json().get("variants", [])[:4]) |
| # Memoizing `get_variants` to prevent N+1 network bottlenecks during bulk creation loops. | ||
| # Impact: Eliminates duplicate network requests for same blueprint/provider combo. | ||
| # Safety: Returned list is cast to a tuple to ensure cache immutability and thread safety. | ||
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| resp = requests.get(url, headers=HEADERS) | ||
| resp.raise_for_status() | ||
| return [v["id"] for v in resp.json().get("variants", [])[:4]] | ||
| return tuple(v["id"] for v in resp.json().get("variants", [])[:4]) | ||
|
|
||
| def create_product(title: str, blueprint_id: int, provider_id: int, | ||
| variant_ids: list, image_id: str) -> dict: | ||
| variant_ids: tuple, image_id: str) -> dict: | ||
| url = f"{BASE_URL}/shops/{SHOP_ID}/products.json" |
| def get_variants(blueprint_id: int, provider_id: int) -> list: | ||
| # Memoizing `get_variants` to prevent N+1 network bottlenecks during bulk creation loops. | ||
| # Impact: Eliminates duplicate network requests for same blueprint/provider combo. | ||
| # Safety: Returned list is cast to a tuple to ensure cache immutability and thread safety. |
| def get_variants(blueprint_id: int, print_provider_id: int = 29) -> List[int]: | ||
| # Memoizing `get_variants` to prevent N+1 network bottlenecks during bulk creation loops. | ||
| # Impact: Eliminates duplicate network requests for same blueprint/provider combo. | ||
| # Safety: Returned list is cast to a tuple to ensure cache immutability and thread safety. |
💡 What: Added
@functools.lru_cacheto memoize theget_variantsfunction inprintify_manager.py,create_merch.py, andmerch_factory.py. Changed the return type from a mutable list to an immutable tuple.🎯 Why: During bulk product creation, the scripts were repeatedly calling the Printify API for the same blueprint and provider combinations (N+1 query problem). This caused unnecessary network delays and rate-limiting risks for data that is static during execution.
📊 Impact: Reduces network requests to Printify for variant lookups to exactly one per unique blueprint/provider combination. Significantly speeds up execution during bulk creation loops. Using tuples ensures cached data cannot be accidentally mutated downstream.
🔬 Measurement: Run a bulk creation command (e.g.,
python3 printify_manager.py run) with multiple designs for the same blueprint. The first variant lookup will take normal network time; subsequent lookups will be instantaneous.PR created automatically by Jules for task 8619318353520766092 started by @merg357