Registry / storage / pouchdb-lru-cache

pouchdb-lru-cache

JSON →
library1.1.4jsnpmunverified

An LRU (least recently used) cache plugin for PouchDB designed to store binary data (Blobs/Buffers) with a configurable maximum size limit. Current stable version is 1.1.4, with infrequent releases. It uses PouchDB's attachment API for storage, providing cross-browser support (IE 10+, modern browsers, Node.js). Key differentiators: leverage PouchDB's battle-tested binary storage, automatic eviction of least recently used items when size limit is exceeded, and simple API similar to node-lru-cache. Good for caching images or other binary assets in offline-first webapps.

npm install pouchdb-lru-cache
INSTALL
IMPORT
SIG · POUCHDB-LRU-CACHE
P
pouchdb-lru-cache
storagejavascriptv1.1.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import PouchDB from 'pouchdb'; import pouchLru from 'pouchdb-lru-cache'; PouchDB.plugin(pouchLru);
import PouchDB from 'pouchdb'; PouchDB.plugin(pouchdbLruCache); // missing import
ESM usage requires importing both PouchDB and the plugin, then calling PouchDB.plugin.
initLru
var db = new PouchDB('cache'); db.initLru(5000000); db.lru.put('key', blob);
var db = new PouchDB('cache'); db.lru.initLru(5000000); // initLru is on db, not db.lru
initLru must be called on the database instance before using db.lru methods.
require (CommonJS)
var PouchDB = require('pouchdb'); PouchDB.plugin(require('pouchdb-lru-cache')); var db = new PouchDB('cache'); db.initLru();
var PouchDB = require('pouchdb'); var db = new PouchDB('cache'); db.plugin('pouchdb-lru-cache'); // plugin() attaches to PouchDB, not db
Plugin must be attached to the PouchDB constructor, not on an instance.

Shows how to import, initialize the LRU cache, store a Blob, retrieve it, and delete it.

// Install: npm install pouchdb pouchdb-lru-cache import PouchDB from 'pouchdb'; import pouchLru from 'pouchdb-lru-cache'; PouchDB.plugin(pouchLru); const db = new PouchDB('lru_example'); db.initLru(1024 * 1024); // 1 MB max const blob = new Blob(['hello world'], { type: 'text/plain' }); await db.lru.put('doc1', blob); const fetched = await db.lru.get('doc1'); console.log('Fetched blob size:', fetched.size); // Clean up db.lru.del('doc1');
Debug
Known issues
gotchaYou MUST call db.initLru([maxSize]) before any db.lru methods. Forgetting this will result in TypeError: Cannot read property 'put' of undefined.
fix
Call db.initLru() immediately after creating the database.
affects: >=1.0.0
gotchaThe maxSize in initLru is calculated based on the sum of attachment sizes reported by PouchDB, which may differ from actual disk usage due to metadata overhead and PouchDB's internal storage format. The cache may use more disk space than specified.
fix
Set maxSize lower than your actual storage budget to account for overhead (e.g., 80% of available space).
affects: >=1.0.0
deprecatedThis plugin has not been updated since 2015. It may have compatibility issues with modern PouchDB versions (8.x+). The 'bower install' and 'dist' file approaches are outdated.
fix
Use npm and import/require. For modern PouchDB, consider alternative caching strategies or test thoroughly.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: db.lru is undefined
initLru() was not called before attempting to use db.lru methods.
fix
Ensure db.initLru(maxSize) is called after creating the database.
Error: initLru is not a function
The plugin was not properly attached to PouchDB via PouchDB.plugin().
fix
Call PouchDB.plugin(require('pouchdb-lru-cache')) or the equivalent import.
Uncaught (in promise) DOMException: Failed to execute 'put' on 'IDBObjectStore': The transaction has not been activated.
PouchDB transaction issues, possibly due to calling lru methods outside of a proper async context or after database destruction.
fix
Use await or .then() properly; avoid calling lru methods after db.close() or db.destroy().
Upgrade
Version history
1.1.4latest on npm
Audit
Dependencies
pouchdbrequiredRuntime peer dependency; plugin attaches to PouchDB instance
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
pouchdb-lru-cache — npm install pouchdb-lru-cache · libregistry