local-storage-es6 v1.0.28
Securely store JSON data and normal data
Used in PotatoDetection
Installation
npm install --save local-storage-es6Setup
- Import the package
path: Stringthe path to your cache folderkey: Stringsecret key as salt for encryptingmkdir: Booleanset totrueif the module should check if the cache folder exists, and if it doesn't it wil create the directoryencryptFileName: Booleanset totrueif the module should encrypt the file name of each cacheencryptFileContent: Booleanset totrueif the module should encrypt your file contents of each cache
ES6
import LocalStorage from 'local-storage-es6'
const ... = new LocalStorage({
path: './cache',
key: '1234',
mkdir: true,
encryptFileName: true,
encryptFileContent: false
})CommonJS
const LocalStorage = require('local-storage-es6')
const ... = new LocalStorage({
path: './cache',
key: '1234',
mkdir: true,
encryptFileName: true,
encryptFileContent: false
})API
write(key, data, callback)
Write data to cache asynchronously and callback a function with the given data written
throws error
returns data written
var obj = {name: 'KitKat', owner: 'Nestle'}
LocalStorage.write('chocolate', obj, (data) => {
...
})writeSync(key, data)
Write data synchronously and return the given data written
throws error
returns data written
var obj = {name: 'KitKat', age: 21}
var cached = LocalStorage.write('chocolate', obj)
console.log(cached)read(key, callback)
Read data asynchronously and callback a function with the data
throws error
returns data from cache
LocalStorage.read('chocolate', (data) => {
...
})readSync(key)
Read data synchronously and callback a function with the data
throws error
returns data from cache
const data = LocalStorage.readSync('chocolate')
...exists(key)
Checks if a cache exists
returns promise
LocalStorage.exists('chocolate')
.then(() => {
...
})
.catch(() => {
var obj = {name: 'KitKat', owner: 'Nestle'}
LocalStorage.writeSync('chocolate', obj)
})existsThenRead(key)
Checks if a cache exists, and if it does, it will return the data in the Promise resolve
returns promise and data
LocalStorage.existsThenRead('chocoloate')
.then(data => {
...
})
.catch(() => {
...
})isNotExpired(key, maxAge)
Checks if a cache exists and is not expired
maxAge checks if the cache is older than x minutes
- default value is 3 hours
returns promise
LocalStorage.isNotExpired('chocolate')
.then(() => {
...
})
.catch(res => {
console.log(res) // 'File is x minutes too old'
var obj = {name: 'KitKat', owner: 'Nestle'}
LocalStorage.writeSync('chocolate', obj)
})isNotExpiredThenRead(key, maxAge)
Checks if a cache exists and is not expired then reads the data and returns it
maxAge checks if the cache is older than x minutes
- default value is 3 hours
returns promise and data
LocalStorage.isNotExpiredThenRead('chocolate')
.then(data => {
...
})
.catch(res => {
console.log(res) // 'File is x minutes too old'
var obj = {name: 'KitKat', owner: 'Nestle'}
LocalStorage.writeSync('chocolate', obj)
})getPath(key)
throws error
returns the given path and hashed md5 filename for the cache
const path = LocalStorage.getPath('chocolate')purge(key)
Deletes a cache asynchronously
throws error
LocalStorage.purge('chocolate', () => {
...
})purgeSync(key)
Deletes a cache synchronously
throws error
LocalStorage.purgeSync('chocolate')trash()
Clears the entire cache folder asynchronously
throws error
LocalStorage.trash(() => {
...
})trashSync()
Clears the entire cache folder synchronously
throws error
LocalStorage.trashSync()getSize(callback)
returns the size of the cache folder in megabytes asynchronously
throws error
LocalStorage.getSize(size => {
...
})8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago