Skip to content

⚡ Bolt: [Cache Printify API Variant Fetches]#43

Open
merg357 wants to merge 1 commit into
mainfrom
bolt-memoize-variant-requests-8619318353520766092
Open

⚡ Bolt: [Cache Printify API Variant Fetches]#43
merg357 wants to merge 1 commit into
mainfrom
bolt-memoize-variant-requests-8619318353520766092

Conversation

@merg357

@merg357 merg357 commented May 21, 2026

Copy link
Copy Markdown
Owner

💡 What: Added @functools.lru_cache to memoize the get_variants function in printify_manager.py, create_merch.py, and merch_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

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>
Copilot AI review requested due to automatic review settings May 21, 2026 09:50
@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 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) to get_variants in multiple scripts to eliminate repeated API calls for the same blueprint/provider combination.
  • Changed variant ID collections returned by get_variants from 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.

Comment thread merch_factory.py
@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)
Comment thread create_merch.py
Comment on lines +61 to +67
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 ()
Comment thread printify_manager.py
Comment on lines +74 to +82
# 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])
Comment thread merch_factory.py
Comment on lines +43 to 55
# 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"
Comment thread merch_factory.py
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.
Comment thread create_merch.py
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.
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