Flexible Storage
Module for front-end storage using LocalStorage (or any another Storage).
It introduces some features:
- allows choose between localStorage and sessionStorage
- define expiration date (using Moment or native Date instance)
- define storage key prefix or filter
- store any object as JSON
- written on TypeScript and includes TypeScript definition files
Installation
Using NPM:
npm i --save flexible-cache
Usage
You can use default import to use FlexibleStorage with Local
Instantiating
import { FlexibleStorage } from "flexible-cache";
const sessionFlexibleStorage = new FlexibleStorage(
window.sessionStorage,
'some_prefix_'
);
const prefix = (key) => "<key>" + key + "</key>";
const FlexibleStorage = new FlexibleStorage(window.localStorage, prefix);
Note: prefix may be skipped
Caching values
import { FlexibleStorage } from "flexible-cache";
const expires = new Date();
const flexibleStorage = new FlexibleStorage(localStorage);
flexibleStorage.push('key', {
someProperty: 2,
}, expires);
Getting values
import { FlexibleStorage }, { arrayOrEmptyArray } from "flexible-cache";
const flexibleStorage = new FlexibleStorage(localStorage);
let value = flexibleStorage.pull('key', arrayOrEmptyArray);
value = flexibleStorage.pull('key');
Find more validators here
Other
import { FlexibleStorage } from "flexible-cache";
const flexibleStorage = new FlexibleStorage(localStorage, 'prefix_');
flexibleStorage.exists('key');
flexibleStorage.remove('key');
Testing
npm test
Author
Alexander Letnikow