{"id":45760,"library":"pg-cache","title":"pg-cache","description":"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.","status":"active","version":"3.12.0","language":"javascript","source_language":"en","source_url":"https://github.com/constructive-io/constructive","tags":["javascript","postgresql","pg","pool","cache","lru","connection","constructive","typescript"],"install":[{"cmd":"npm install pg-cache","lang":"bash","label":"npm"},{"cmd":"yarn add pg-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-cache","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"underlying connection pooling","package":"pg-pool","optional":false},{"reason":"LRU cache implementation for results","package":"lru-cache","optional":false}],"imports":[{"note":"PGCache is a named export, not default. ESM only since v3.","wrong":"import PGCache from 'pg-cache'","symbol":"PGCache","correct":"import { PGCache } from 'pg-cache'"},{"note":"CJS require not supported in v3+. Use dynamic import() if needed.","wrong":"const createPool = require('pg-cache').createPool","symbol":"createPool","correct":"import { createPool } from 'pg-cache'"},{"note":"TypeScript type import, available since v3.10. Use import type for transpile-only.","wrong":"","symbol":"PGCacheOptions","correct":"import type { PGCacheOptions } from 'pg-cache'"}],"quickstart":{"code":"import { PGCache } from 'pg-cache';\n\nconst cache = new PGCache({\n  pool: {\n    connectionString: process.env.DATABASE_URL ?? 'postgres://user:pass@localhost/db',\n    max: 10\n  },\n  cache: {\n    max: 100,\n    ttl: 60_000\n  }\n});\n\nasync function main() {\n  const query = 'SELECT * FROM users WHERE id = $1';\n  const params = [1];\n  // First call - cache miss\n  const result1 = await cache.query(query, params);\n  console.log('First query result:', result1.rows);\n  // Second call - cache hit within TTL\n  const result2 = await cache.query(query, params);\n  console.log('Cached result:', result2.rows);\n  // Force evict specific key\n  cache.del(query, params);\n  // Clear entire cache\n  cache.clear();\n  // Close pool\n  await cache.end();\n}\nmain().catch(console.error);","lang":"typescript","description":"Creates a PGCache instance with connection pool and LRU cache, performs parameterized queries with automatic caching, demonstrates cache eviction and cleanup."},"warnings":[{"fix":"Change import { PGCache } from 'pg-cache' (was import PGCache from 'pg-cache').","message":"PGCache is now exported as a named export instead of default export.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use dynamic import: const { PGCache } = await import('pg-cache').","message":"CJS require() no longer supported; package is ESM-only.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Provide cache: { max: number, ttl: number }. Previously defaulted to infinite.","message":"The 'cache' option now requires explicit 'max' and 'ttl' properties; defaults changed.","severity":"breaking","affected_versions":">=2.5.0"},{"fix":"Pass pool configuration object (e.g., { connectionString, max }) instead of a Pool instance.","message":"PGCache constructor's 'pool' option as a pg.Pool instance is deprecated; use pool config object instead.","severity":"deprecated","affected_versions":">=3.2.0"},{"fix":"Ensure consistent parameter ordering for cache hits. Use named parameters with placeholder $1, $2, etc.","message":"Query cache key is generated from the query string and parameters. Order of parameters matters.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set max cache size to limit memory usage. Call evictStale() periodically if needed.","message":"TTL is checked on access; stale entries are not automatically purged until queried.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"search_vec":"'3.12.0':17 'activ':87 'add':66 'automat':46 'built':9 'built-in':8 'cach':3,12,22,32,65,69,71,82,96 'compar':57 'configur':40 'connect':5,26,98 'construct':99 'data':49 'db':76 'develop':88 'entri':44 'evict':47 'hit':77 'hook':73 'hybrid':20 'integr':54 'invalid':72 'javascript':92 'layer':23 'like':83 'lru':11,37,97 'maintain':25 'manag':13 'max':43 'month':90 'node.js':15 'parameter':51 'pg':2,29,61,64,94 'pg-cach':1,63 'pg-pool':28,60 'pool':6,21,30,62,95 'postgresql':4,93 'provid':18 'queri':33,52 'raw':59 'redi':84 'reduc':74 'releas':85 'repeat':75 'requir':79 'result':34,68 'separ':81 'stale':48 'strategi':38 'support':50 'transpar':67 'ttl':41 'typescript':56,100 'updat':91 'use':35 'version':16 'via':27 'without':78","created_at":"2026-06-07T12:56:35.667146+00:00","updated_at":"2026-06-07T12:56:35.667146+00:00","problems":[{"fix":"Use import { PGCache } from 'pg-cache'.","cause":"Trying to import PGCache as default export (import PGCache from 'pg-cache') instead of named export.","error":"TypeError: pg_cache_1.PGCache is not a constructor"},{"fix":"Use dynamic import() or switch project to ESM (e.g., set type:\"module\" in package.json).","cause":"Using require('pg-cache') in a CommonJS module; package is ESM-only since v3.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module not supported"},{"fix":"Call cache.evictStale() before query or rely on max size to automatically evict oldest entries.","cause":"Cache TTL is checked on access only; stale entries remain until an explicit get() or eviction.","error":"Expired cached entry returned despite clear TTL"},{"fix":"Run npm install lru-cache or update package.json to include it.","cause":"Missing lru-cache package in dependencies. pg-cache requires lru-cache as a peer or direct dependency.","error":"Peer dependency 'lru-cache' not found"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/constructive-io/constructive","github":"https://github.com/constructive-io/constructive","docs":null,"changelog":null,"pypi":null,"npm":"pg-cache","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}