⚡ Bolt: [Cache Printify API variants]#51
Conversation
💡 What: Used `@functools.lru_cache` to memoize the responses from the `get_variants` API call across `create_merch.py`, `merch_factory.py`, and `printify_manager.py`. Cast the results to immutable tuples to ensure thread safety and avoid side effects. 🎯 Why: Prevents redundant N+1 external network requests to Printify for static provider variants during batch bulk creation loops. 📊 Impact: Considerably faster execution in bulk merchandise generation loops by eliminating duplicated API round-trips. 🔬 Measurement: Compare API request logs (or network tab tracing) when bulk-creating products before and after the change; fewer `GET .../variants.json` calls should be observed. 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 improves bulk Printify product creation performance by memoizing get_variants() (variants.json) calls with functools.lru_cache, reducing repeated external network requests during batch loops.
Changes:
- Added
@functools.lru_cache(maxsize=None)toget_variants()in multiple scripts to eliminate redundant variants fetches. - Returned immutable tuples from cached variant lookups to prevent accidental downstream mutation of cached values.
- Added a short internal note in
.jules/bolt.mddocumenting the memoization pattern and immutability rationale.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| printify_manager.py | Memoizes variant lookups and returns tuples to reduce repeated Printify API calls. |
| merch_factory.py | Memoizes variant lookups and returns tuples for safe reuse in bulk factory runs. |
| create_merch.py | Memoizes variant lookups and updates typing to reflect tuple returns. |
| .jules/bolt.md | Documents the caching + immutability practice for future reference. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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_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: |
| 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 tuple() |
| def create_product(blueprint_id: int, variant_ids: List[int], product_name: str, | ||
| color: str, placement: Dict[str, Any]) -> Dict[str, Any]: |
💡 What: Used
@functools.lru_cacheto memoize the responses from theget_variantsAPI call acrosscreate_merch.py,merch_factory.py, andprintify_manager.py. Cast the results to immutable tuples to ensure thread safety and avoid side effects.🎯 Why: Prevents redundant N+1 external network requests to Printify for static provider variants during batch bulk creation loops.
📊 Impact: Considerably faster execution in bulk merchandise generation loops by eliminating duplicated API round-trips.
🔬 Measurement: Compare API request logs (or network tab tracing) when bulk-creating products before and after the change; fewer
GET .../variants.jsoncalls should be observed.PR created automatically by Jules for task 12653606150784020684 started by @merg357