Skip to content

⚡ Bolt: [performance improvement] Prevent N+1 API Calls via LRU Cache#46

Open
merg357 wants to merge 1 commit into
mainfrom
bolt-lru-cache-optimization-771303604281515876
Open

⚡ Bolt: [performance improvement] Prevent N+1 API Calls via LRU Cache#46
merg357 wants to merge 1 commit into
mainfrom
bolt-lru-cache-optimization-771303604281515876

Conversation

@merg357

@merg357 merg357 commented May 24, 2026

Copy link
Copy Markdown
Owner

💡 What: Implemented @functools.lru_cache(maxsize=None) on get_variants and get_blueprint_variants functions across printify_manager.py, create_merch.py, merch_factory.py, and merg_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

Co-authored-by: merg357 <221854052+merg357@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 24, 2026 09:25
@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 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 list to tuple to 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.

Comment thread printify_manager.py
Comment on lines +78 to +82
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 merg_bridge.py
Comment on lines +31 to +35
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", []))
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 create_merch.py
Comment on lines +61 to 65
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()
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