localforage-lru-driver
v1.0.4
Published
plugin for localforage to work with indexes in indexedDb
Downloads
7,737
Maintainers
Readme
localforage-lru-driver
Localforage driver that maintains cache size depending on least-recently-used(lru) principle. localForage.
Requirements
- localforage v1.4.0+
- localforage-removeitems v1.4.0+
- localforage-indexes v1.0.2+
Installation
npm i localforage-lru-driver
Usage
Inserting into your project
import { extendPrototypeResult as localforage } from 'localforage-indexes';
import { lruDriver } from 'localforage-lru-driver';
localforage.defineDriver(lruDriver)
.then(function() {
var lf = localforage.createInstance({
driver : 'lruStorage',
cacheSize : 100
});
return lf.ready();
})
.then(function(lf) {
// use localforage
})
When items count exceed cacheSize
, least recently used item is removed:
// .... set items with keys KEY_1 ... KEY_100
lf.setItem('KEY_101', 'value')
.then(() => lf.length())
.then(length => console.log(length)) // 100
);
Each time an entry is accessed through getItem
or setItem
it is updated:
// .... set items with keys KEY_1 ... KEY_100. KEY_1 is set first
lf.getItem('KEY_1') // last access time updated
.then(() => lf.setItem('KEY_101', 'value'))
.then(length => console.log(length)) // 100
.then(() => lf.getItem('KEY_1'))
.then((key1Value) => console.log(key1Value)) // still present in the localforage
Contructor options
cacheSize
Maximum number of entries allowed to store
Default value: 1000
lruKey
Name of the key that is used for keeping last access timestamp
Default value: ACCESS_TIME
lruIndex
Name of the index that is used for sorting items by last access time
Default value: lruIndex