PostgreSQL connection pool with built-in LRU cache management for Node.js. Version 3.12.0 provides a hybrid pool+cache layer that maintains connections via pg-pool while caching query results using an LRU strategy with configurable TTL and max entries. It automatically evicts stale data, supports parameterized queries, and integrates with TypeScript. Compared to raw pg-pool, pg-cache adds transparent result caching and cache invalidation hooks, reducing repeated DB hits without requiring a separate cache like Redis. Released under active development with monthly updates.
npm install pg-cacheNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates a PGCache instance with connection pool and LRU cache, performs parameterized queries with automatic caching, demonstrates cache eviction and cleanup.
Change import { PGCache } from 'pg-cache' (was import PGCache from 'pg-cache').Use dynamic import: const { PGCache } = await import('pg-cache').Provide cache: { max: number, ttl: number }. Previously defaulted to infinite.Pass pool configuration object (e.g., { connectionString, max }) instead of a Pool instance.Ensure consistent parameter ordering for cache hits. Use named parameters with placeholder $1, $2, etc.
Set max cache size to limit memory usage. Call evictStale() periodically if needed.
Use import { PGCache } from 'pg-cache'.Use dynamic import() or switch project to ESM (e.g., set type:"module" in package.json).
Call cache.evictStale() before query or rely on max size to automatically evict oldest entries.
Run npm install lru-cache or update package.json to include it.