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

@aadvdh/localstorage-cache

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aadvdh/localstorage-cache

A small package for using localstorage as a cache

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

localstorage-cache

A small library to add caching capabilities to localStorage.

Install

npm install @aadvdh/localstorage-cache

How to use

import the LocalStorageCache class

import LocalStorageCache from 'localstorage-cache.js'

Initialize a new instance

let ls = new LocalStorageCache();

Arguments: cacheExpiryTime - The amount of time to keep an item in the cache in seconds, standard at 3600 seconds, or 1 hour. prefix - a prefix added to every key. Standard value is "lsc"

Add an item to the cache:

ls.set(key,value,cacheExpiryTime);

Will automatically clean up the cache incase the maximum storage size is exceeded. returns true if succesful, false otherwise

Arguments: key - a key to retrieve the value by. Any non alphanumeric characters are stripped. value - the value to store. Can be any value cacheExpiryTime - in case you want to override the cacheExpiryTime.

Retrieve an item from the cache:

ls.get(key);

returns the value if it exists and the item hasn't expired yet. Null otherwise.

Arguments: key - the key of the value to be retrieved.

setBucket:

ls.setBucket(bucket);

Set the bucketname to store the items in. All non alphanumeric characters are removed from the string.

Arguments: bucket - name of the bucket to use

Flush:

ls.flush(expired);

Removes keys from storage without affecting all of the localStorage. If expired is set to false it will remove every key, otherwise it will only remove expired keys.

Arguments expired - Boolean value whether only expired items should be removed or all items.

Flush bucket

ls.flushBucket(expired, bucket)
Same as flush, but will only remove from a bucket.

Arguments: expired - Boolean value whether only expired items should be removed or all items. bucket - The bucket to remove from, otherwise it will use the bucket defined within the class.

FAQs

Package last updated on 18 Jun 2021

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