@axtk/volatile-storage
v1.0.3
Published
A storage with a customizable item lifetime and capacity with a localStorage-like API
Downloads
1
Maintainers
Readme
A storage with a customizable item lifetime and capacity with a localStorage-like API
class VolatileStorage
const VolatileStorage = require('@axtk/volatile-storage');
const storage = new VolatileStorage({
storage: window.localStorage,
maxAge: 60000, // in milliseconds
capacity: 100
});
await storage.setItem('x', 1);
await storage.setItem('status', 'done', {maxAge: 120000});
let x = await storage.getItem('x');
new VolatileStorage(props?)
props?: object
props.storage?: Storage | async<Storage>
- An external storage instance with a localStorage-like API (like
window.localStorage
itself,window.sessionStorage
, orlocalForage
) where all values will actually be stored. - Default: in-memory storage.
- An external storage instance with a localStorage-like API (like
props.maxAge?: number
- A storage entry lifetime in milliseconds. (A stored value may linger on the storage after its lifetime elapses until the next interaction with the storage reveals the value has expired and removes it.)
- Default:
Infinity
.
props.capacity?: number
- A maximum number of entries on the storage. When the number of entries reaches the
capacity
value and a new item is added to the storage, the first added item is removed from the storage to maintain the capacity. - Default:
Infinity
.
- A maximum number of entries on the storage. When the number of entries reaches the
props.ns?: string
- A storage entry key namespace. It can be useful to isolate multiple instances of
VolatileStorage
sharing the same external storage (likewindow.localStorage
). - Default:
''
.
- A storage entry key namespace. It can be useful to isolate multiple instances of
props.version?: string | number
- A version, or a revision identifier, of the storage. Changing this value will expire values stored in an external storage (like
window.localStorage
) under a different version. - Default:
undefined
.
- A version, or a revision identifier, of the storage. Changing this value will expire values stored in an external storage (like
The methods of the VolatileStorage
class are asynchronous versions of the Storage
methods.