cache

handle storing data to localStorage

How to use

import { Plugin } from '@root'

Plugin.cache({
    ...CACHE_PAYLOAD
})

Cache payload

Param

Typeof

Default

Required

Description

key

String

null

the cache key to be used to save/get/remove your data from localStorage.

data

Any

null

the data item to be stored.

expire

Number

0

when the data should be expired in localStorage

callback

Function

null

a callback will be called when the data is saved/removed from localStorage

method

String

GET

a method to save/get/remove available: GET, POST, DELETE

Save data to localStorage

import { Plugin } from '@root'

const item = {
    id: 'item-id',
    name: 'item name'
}

const onSave = () => {

}

Plugin.cache({
    key: 'my-cache-key',
    data: item,
    method: 'POST',
    callback: onSave
})

Get data from localStorage

import { Plugin } from '@root'

const onGet = cachedData => {
    console.log(cachedData)
    /*
        Output:
        {
            id: 'item-id',
            name: 'item name'
        }
    */
}

Plugin.cache({
    key: 'my-cache-key',
    method: 'GET', // can be ignored, as GET is the default method for cache
    callback: onGet
})

Remove data from localStorage

import { Plugin } from '@root'

Plugin.cache({
    key: 'my-cache-key',
    method: 'DELETE'
})

Last updated