Cache `attachment_url_to_postid()` lookups in `Optml_Lazyload_Replacer` to reduce database overhead

Aperta
#1,249 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php, wordpress
Ambito
performance

Direzione di ricerca

Start in the Optml_Lazyload_Replacer class (under the plugin's inc/ directory) and find every call site of attachment_url_to_postid() in its replacement pipeline, including the path used for /wp-json/wp/v2/... requests. Add a per-request static array keyed by URL, then a persistent layer via wp_cache_get/wp_cache_set or transients keyed by a hash of the URL, and confirm reuse by counting wp_postmeta queries (e.g. with Query Monitor) on a page with repeated images. Done looks like: repeated URL lookups hit the cache and postmeta query count drops for repeated content and REST hits.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

customer report feature-request-triage

Impact: 75 · Confidence: 90 · Complexity: 40

Description

Problem

When Optimole's Optml_Lazyload_Replacer processes content (including REST API endpoints like /wp-json/wp/v2/...), it repeatedly calls core WordPress attachment_url_to_postid() to resolve image URLs to attachment IDs. In WordPress core, attachment_url_to_postid() queries the wp_postmeta table directly without built-in persistent caching. On pages or API endpoints with multiple images, this results in dozen of expensive wp_postmeta DB queries, severely slowing down PHP request execution time (e.g., ~6.25 seconds spent on 28 postmeta queries in a single request).

Desired Behavior

Optimole should cache the mapping between image attachment URLs and attachment IDs using WordPress Object Cache / Transients, or optimize the replacer pipeline so attachment_url_to_postid() is only executed when strictly necessary and cached across requests.

Acceptance Criteria
  • Optml_Lazyload_Replacer caches URL-to-attachment ID resolutions in WP Object Cache / transient storage.
  • Duplicate image URL lookups within the same request lifecycle reuse in-memory cache.
  • Database query count for wp_postmeta during lazyload replacement drops significantly for repeated content/API hits.

Customer Context

A premium user noticed via New Relic profiling that Optml_Lazyload_Replacer is executing repeated attachment_url_to_postid() calls during REST API requests, causing 28 wp_postmeta database queries consuming ~6.25s of a 7.98s transaction.

Root Cause Analysis

The customer is asking to prevent or cache repeated URL-to-attachment-ID lookups. Underlying cause: attachment_url_to_postid() is uncached in WordPress core, and Optml_Lazyload_Replacer executes it on every image URL encountered during content replacement. This is an architecture performance issue in how existing replacement functionality interacts with WP database lookups. Improving existing behavior by introducing transient/object caching or in-memory static caching around attachment_url_to_postid() within Optimole solves the root cause without adding configuration surface.

Alternative Suggestions

Wrap attachment_url_to_postid() calls in Optml_Lazyload_Replacer with an internal static array cache for single-request scope and WP Object Cache / Transients (keyed by URL hash) across requests to eliminate repetitive wp_postmeta queries.


Source: HelpScout #3524196151
Generated by feature-request-triage workflow (ID: feature-request-triage_6b0599ff05a6a1.02374250)

Lingua principale
PHP
Stelle
74
Fork
14
Merge medio
1g 2h
PR unite (30g)
35

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Codeinwp/optimole-wp

Tutte le issue di Codeinwp/optimole-wp

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.