lsdis
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

lsdis

npm CircleCI

KV storage based on LocalStorage.

Purpose

Cache requests with localStorage in the browser.

Feature

  • Local storage API
  • Cache wrapper
  • Cache invalidate

Usage

LocalStorage - Low Level API

import LocalStorage from 'lsdis'
 
const storage = new LocalStorage()
const mykey = 'mykey'
const myval = 'myval'
const timeoutMs = 1000
 
// set value with timeout(/ms)
storage.set(mykey, myval, timeoutMs)
 
// if not existed, return null else return string
storage.get(mykey)
 
// delete by key
storage.del(mykey)
 
// flush all localstorage
storage.flush()
 

LocalCache - High Level API

import { LocalCache } from 'lsdis'
 
function getUser(username: string) {
  // fetch request data
  return { username }
}
 
const timeoutMs = 1000
const cache = new LocalCache({ timeout: timeoutMs })
const username = 'myname'
 
async function main() {
  // wrapper by key with function and args
  const result = await cache.wrapper(
    `getUser:${username}`, 
    getUser, 
    username,
  )
  console.log(result)
 
  // wrapper by key with function
  const resultAsync = await cache.wrapper(
    `getUser:${username}`, 
    async () => (getUser(username)),
  )
  console.log(resultAsync)
}

Readme

Keywords

none

Package Sidebar

Install

npm i lsdis

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

19.8 kB

Total Files

33

Last publish

Collaborators

  • joway