@aadvdh/localstorage-cache

1.0.1 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @aadvdh/localstorage-cache

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

16.6 kB

Total Files

8

Last publish

Collaborators

  • aadvdh