Skip to content

perf: cache the country lookup and index files.subject_uuid - #291

Merged
roncodes merged 2 commits into
release/v1.6.69from
fix/country-lookup-cache
Oct 7, 2026
Merged

roncodes merged 2 commits into
release/v1.6.69from
fix/country-lookup-cache

Conversation

@roncodes

@roncodes roncodes commented Oct 6, 2026

Copy link
Copy Markdown
Member

Summary

Store listings in Storefront could take minutes because each store's resource calls Utils::getCountryCodeByCurrency(). Every call built a new PragmaRX\Countries instance, decoded the 4.7 MB countries dataset, hydrated flags, and mapped about 250 countries. So the cost was paid once per store, on every request. getCountryCodeByName() had the same pattern.

Changes

  • Utils::getCountryLookup() builds the name / ISO2 / currency rows once, stores them in the application cache (fleetbase:utils:country-lookup:v1), and memoizes them per process.
    • If the cache store is unavailable, the lookup is still built (once per process).
    • Utils::flushCountryLookup() resets the memo and, optionally, the cached copy.
  • getCountryCodeByCurrency() and getCountryCodeByName() use the cached lookup. Their behaviour is unchanged, including returning the first matching country (for example, USD still resolves to the first match in the dataset).
  • New migration: index on files.subject_uuid. Store media, product images and proofs of delivery are loaded by subject and previously scanned the whole files table.

Companion change: fleetbase/storefront fix/network-stores-performance (per-store queries in the store resource and network store listing).

Validation

  • pest tests/Unit/Support/UtilsTest.php: new tests cover a cache hit, memoization, rebuild after flush, flushing the memo only, and an unavailable cache store.
  • Full suite (PHP 8.4): 1952 passed, 1 failed. The failure is utils reads composer package keyword metadata from the lock file; it fails only because the run was from a checkout outside the monorepo layout, and passes from the normal packages/core-api checkout.
  • php-cs-fixer --dry-run on the changed files: clean.
  • PHPStan: nothing new apart from patterns already reported for the surrounding helpers (collect() / Countries::all() typing).

Risk

  • The cached lookup is static reference data. The cache key is versioned (:v1), so a future shape change can bump it.
  • The index migration checks for an existing index before adding or dropping it.

getCountryCodeByCurrency() and getCountryCodeByName() rebuilt the full
countries dataset (a 4.7 MB JSON file plus flag hydration) on every call.
Storefront serializes a country per store, so listing stores paid that cost
once per record and network store lists could take minutes.

The name/ISO2/currency rows are now built once, kept in the application
cache and memoized per process. Cache failures fall back to building the
lookup, and flushCountryLookup() resets it.
Store media, product images and proofs of delivery are looked up by
subject_uuid, which had no index, so each lookup scanned the whole files
table.
@codecov

codecov Bot commented Oct 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ab33100) to head (d569a22).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##                main      #291   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity      7931      7940    +9     
===========================================
  Files            438       438           
  Lines          25665     25670    +5     
===========================================
+ Hits           25665     25670    +5     
Flag Coverage Δ
backend 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@roncodes roncodes mentioned this pull request Oct 7, 2026
@roncodes
roncodes changed the base branch from main to release/v1.6.69 October 7, 2026 05:51
@roncodes
roncodes merged commit 8072783 into release/v1.6.69 Oct 7, 2026
7 checks passed
@roncodes
roncodes deleted the fix/country-lookup-cache branch October 7, 2026 06:09
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.

1 participant