A least-recently-used (LRU) cache for web applications based on IndexedDB.
npm install lru-cache-idbnpm install lru-cache-idb
javascript
import { createCacheIdb } from "lru-cache-idb";
const cache = createCacheIdb({maxItems: 1000, memoryConfig: {maxItemsInMemory: 100}});
await cache.set("entry1", {a: 1, description: "The first entry"});
await cache.set("entry2", {a: 2, description: "The 2nd entry"});
const obj = await cache.get("entry1");
console.log("Object found:", obj);
`
It is possible to instantiate multiple caches on one page, but the database names must differ:
`javascript
const cache = createCacheIdb({database: "MySpecialDb", maxItems: 1000, memoryConfig: {maxItemsInMemory: 100}});
`
This is the database name in IndexedDB, the default being "LruIdbItemsCache". The existing databases/caches on a page can be viewed in the browser developer console, e.g. under Web Storage->Indexed DB (Firefox) or Application->Storage->IndexedDB (Chrome).
API documentation: https://cnoelle.github.io/lru-cache-idb/docs/index.html
Develop
After cloning the repository, install the dev dependencies: npm install.
$3
`
npm run build
`
$3
This repository contains a sample HTML file index.html using the library. For instance, start a dev server using npx http-server, then the page will be available in the browser at http://localhost:8080 (check port in console output). Build first, index.html refers to the compiled Javascript files in the dist/ folder. The demo app can also be found at https://cnoelle.github.io/lru-cache-idb/.
$3
Tests run in NodeJS and hence cannot access an actual browser's IndexedDB implementation. Instead, they run against fake-indexeddb.
To run all tests, build first, then:
`
npm run test
`
To run tests in a single file:
`
npx ava ./test/testDefaultCache.js
`
In order to run a single test in a single file, replace test("...", async t => { at the beginning of the test by test.only("...", async t => {.
$3
Documentation is generated using typedoc. Run
`
npm run docs
``