⚡ Bolt: [performance improvement] Prevent N+1 API Calls via LRU Cache#46
⚡ Bolt: [performance improvement] Prevent N+1 API Calls via LRU Cache#46merg357 wants to merge 1 commit into
Conversation
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 adds memoization to Printify catalog variant-fetch helpers to avoid repeated API calls during bulk product creation loops, reducing N+1 network traffic when the same blueprint/provider variants are requested multiple times.
Changes:
- Added
@functools.lru_cache(maxsize=None)to variant lookup functions in multiple scripts. - Switched cached return values from
listtotupleto avoid caching mutable list objects. - Added a short Bolt note documenting the caching approach.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| printify_manager.py | Memoizes get_variants and returns an immutable tuple of variant IDs. |
| merg_bridge.py | Memoizes get_blueprint_variants and returns a tuple instead of a list. |
| merch_factory.py | Memoizes get_variants and returns an immutable tuple of variant IDs. |
| create_merch.py | Memoizes get_variants and returns an immutable tuple of variant IDs. |
| .jules/bolt.md | Adds documentation/learning note about using LRU cache + immutable returns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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]) |
| def get_blueprint_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 resp.json().get("variants", []) | ||
| return tuple(resp.json().get("variants", [])) |
| 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 get_variants(blueprint_id: int, print_provider_id: int = 29) -> tuple: | ||
| 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() |
💡 What: Implemented
@functools.lru_cache(maxsize=None)onget_variantsandget_blueprint_variantsfunctions acrossprintify_manager.py,create_merch.py,merch_factory.py, andmerg_bridge.py. The return structures were changed from mutable lists to immutable tuples to guarantee thread safety and prevent downstream state mutations when using cached returns.🎯 Why: When creating merchandise in a loop, these functions repeatedly hit the Printify API for the exact same static blueprint variant data, causing severe N+1 network bottlenecks.
📊 Impact: Significantly reduces redundant network requests, speeding up bulk product creation loops by skipping unnecessary API latency overhead for identical variant lookups.
🔬 Measurement: Verify by running
python3 printify_manager.py run(or equivalent creation loops in other scripts) with multiple identical blueprint designs. Observe the network traffic or total execution time, which should drastically decrease on successive loops creating the same product type.PR created automatically by Jules for task 771303604281515876 started by @merg357