Skip to content

⚡ Bolt: [Cache Printify API variants]#51

Open
merg357 wants to merge 1 commit into
mainfrom
bolt-optimization-cache-variants-12653606150784020684
Open

⚡ Bolt: [Cache Printify API variants]#51
merg357 wants to merge 1 commit into
mainfrom
bolt-optimization-cache-variants-12653606150784020684

Conversation

@merg357

@merg357 merg357 commented May 29, 2026

Copy link
Copy Markdown
Owner

💡 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.


PR created automatically by Jules for task 12653606150784020684 started by @merg357

💡 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>
Copilot AI review requested due to automatic review settings May 29, 2026 09:09
@google-labs-jules

Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) to get_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.md documenting 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.

Comment thread printify_manager.py
Comment on lines 79 to +82
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]])
Comment thread merch_factory.py
Comment on lines +47 to +51
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]])
Comment thread merch_factory.py
Comment on lines 53 to 54
def create_product(title: str, blueprint_id: int, provider_id: int,
variant_ids: list, image_id: str) -> dict:
Comment thread create_merch.py
Comment on lines 62 to +67
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()
Comment thread create_merch.py
Comment on lines 69 to 70
def create_product(blueprint_id: int, variant_ids: List[int], product_name: str,
color: str, placement: Dict[str, Any]) -> Dict[str, Any]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants