Local Cache
Module for front-end cache storage using LocalStorage (or any another Storage).
Main 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 LocalCache with Local
Instantiating
import {LocalCache} from "flexible-cache";
const sessionLocalCache = new LocalCache(
window.sessionStorage,
'some_prefix_'
);
const prefix = (key) => "<key>" + key + "</key>";
const localCache = new LocalCache(window.localStorage, prefix);
Note: prefix may be skipped
Caching values
import localCache from "flexible-cache";
const expires = new Date();
localCache.push('key', {
someProperty: 2,
}, expires);
Getting values
import localCache, {arrayOrEmptyArray} from "flexible-cache";
let value = localCache.pull('key', arrayOrEmptyArray);
value = localCache.pull('key');
Find more validators here
Other
import {LocalCache} from "flexible-cache";
const localCache = new LocalCache(localStorage, 'prefix_');
localCache.exists('key');
localCache.remove('key');
Testing
npm test
Author
Alexander Letnikow