Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flexible-storage

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flexible-storage

Cache on frontend using LocalStorage

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Local Cache

Build Status codecov

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";

// Using Session Storage and some string prefix
const sessionLocalCache = new LocalCache(
    window.sessionStorage,
    'some_prefix_' // this prefix will be used internally when working with storage
);

// Using Local Storage and function 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(); // also can be Moment.js instance

localCache.push('key', {
    someProperty: 2,
}, expires);

Getting values

import localCache, {arrayOrEmptyArray} from "flexible-cache";

// Value will be stored array or empty array if nothing stored
let value = localCache.pull('key', arrayOrEmptyArray);
// Just to pull value with validating only key expiring
value = localCache.pull('key');

Find more validators here

Other

import {LocalCache} from "flexible-cache";

const localCache = new LocalCache(localStorage, 'prefix_');
localCache.exists('key'); // will try to find and validate `prefix_key` in LocalStorage
localCache.remove('key'); // will remove `prefix_key` from LocalStorage

Testing

npm test

Author

Alexander Letnikow

Keywords

FAQs

Package last updated on 23 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc