项目作者: joway

项目描述 :
KV storage based on LocalStorage.
高级语言: TypeScript
项目地址: git://github.com/joway/lsdis.git
创建时间: 2019-03-26T13:32:58Z
项目社区:https://github.com/joway/lsdis

开源协议:

下载


lsdis

npm
CircleCI

KV storage based on LocalStorage.

Purpose

Cache requests with localStorage in the browser.

Install

  1. # nodejs
  2. npm i -S lsdis

Or

  1. <!-- browser -->
  2. <script src='https://cdn.jsdelivr.net/npm/lsdis@latest/dist/lsdis.min.js'></script>

Feature

  • Local storage API
  • Cache wrapper
  • Cache invalidate

Usage

LocalStorage - Low Level API

  1. import LocalStorage from 'lsdis'
  2. const storage = new LocalStorage()
  3. const mykey = 'mykey'
  4. const myval = 'myval'
  5. const timeoutMs = 1000
  6. // set value with timeout(/ms)
  7. storage.set(mykey, myval, timeoutMs)
  8. // if not existed, return null else return string
  9. storage.get(mykey)
  10. // delete by key
  11. storage.del(mykey)
  12. // flush all localstorage
  13. storage.flush()

LocalCache - High Level API

  1. import { LocalCache } from 'lsdis'
  2. function getUser(username: string) {
  3. // fetch request data
  4. return { username }
  5. }
  6. const timeoutMs = 1000
  7. const cache = new LocalCache({ timeout: timeoutMs })
  8. const username = 'myname'
  9. async function main() {
  10. // wrapper by key with function and args
  11. const result = await cache.wrapper(`getUser:${username}`, getUser, username)
  12. console.log(result)
  13. // wrapper by key with function
  14. const resultAsync = await cache.wrapper(`getUser:${username}`, async () => getUser(username))
  15. console.log(resultAsync)
  16. }