cache-postgres
The default cache adapter. Implements query result caching via PostgreSQL materialized views.
This adapter is bundled with @pgshift/cache. You do not need to install it separately.
How it works
Section titled “How it works”register() creates a materialized view in your database. The view stores the pre-computed result of your query on disk.
get() reads directly from the view with no recalculation. If the view is stale based on refreshEvery, a background refresh is triggered using REFRESH MATERIALIZED VIEW CONCURRENTLY, which does not block reads.
The _pgshift_id convention
Section titled “The _pgshift_id convention”REFRESH CONCURRENTLY requires a unique index on the view. PgShift looks for a column aliased as _pgshift_id to create this index automatically.
SELECT p.id AS _pgshift_id, -- required for non-blocking refresh p.name, SUM(o.amount) AS totalFROM products pLEFT JOIN orders o ON o.product_id = p.idGROUP BY p.id, p.nameWithout _pgshift_id, refresh falls back to a blocking mode that locks the view during updates.
Requirements
Section titled “Requirements”- PostgreSQL 12 or later
Limitations
Section titled “Limitations”- Not suited for caching arbitrary objects by key. Use Redis for that use case.
- No per-entry TTL.
- Refresh granularity is per-view, not per-row.
When average read latency exceeds 50ms over 100 reads, PgShift will suggest migrating to a Redis adapter.
Internal tables
Section titled “Internal tables”| Table | Purpose |
|---|---|
_pgshift_cache_{name} | Materialized view storing pre-computed query results |
_pgshift_cache_config | Stores view configuration and refresh metadata |