Skip to content

maikkkko1/js-storage-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JS Storage Cache

Easily cache data, request returns, and other data.

Installation

npm install js-storage-cache@1.0.1

Usage examples

Insert an item into the cache.

import StorageCache from 'js-storage-cache';

const storageCache = new StorageCache();

// Insert a string that will expire in 10 minutes.
storageCache.set('testKey', 'foo', 10);

// Insert a object/array that will expire in 5 minutes..
storageCache.set('testKey', [{a: 'foo'}, {a: 'bee'}], 5);

Retrieve an item from the cache.

// Retrieve the item, if not found returns false.
storageCache.get('testKey');

Removes a key and its object from the cache.

// Remove the item if exists.
storageCache.removeKey('testKey');

Documentation

Cache data will always be saved to browser localStorage.

Every time the get method is executed, an expired key check is performed, and if any are found, the key is removed.

  • set(key, value, timeToExpire) - Inserts a value into the cache.
  • get(key) - Returns an object from the cache.
  • getKeys() - Returns the existing keys in the cache object.
  • isExpired(key) - Checks if the informed cache key is expired.
  • removeKey(key) - Removes a key and its object from the cache.
  • verifyExpiredKeys(key) - Checks if a key is expired, if it removes the key.
  • clear() - Removes all keys and objects from the cache.

About

Simple cache system using the localStorage.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published