Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joway/lsdis
KV storage based on LocalStorage.
https://github.com/joway/lsdis
cache javascript kv-storage local-cach localstorage redis-like typescript
Last synced: 14 days ago
JSON representation
KV storage based on LocalStorage.
- Host: GitHub
- URL: https://github.com/joway/lsdis
- Owner: joway
- Created: 2019-03-26T13:32:58.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-13T11:58:50.000Z (almost 2 years ago)
- Last Synced: 2024-10-14T15:33:10.041Z (28 days ago)
- Topics: cache, javascript, kv-storage, local-cach, localstorage, redis-like, typescript
- Language: TypeScript
- Homepage:
- Size: 839 KB
- Stars: 18
- Watchers: 3
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lsdis
[![npm](https://img.shields.io/npm/v/lsdis.svg)](https://www.npmjs.com/package/lsdis)
[![CircleCI](https://circleci.com/gh/joway/lsdis.svg?style=shield)](https://circleci.com/gh/joway/lsdis)KV storage based on [LocalStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
## Purpose
Cache requests with localStorage in the browser.
## Install
```bash
# nodejs
npm i -S lsdis
```Or
```html
```
## Feature
- Local storage API
- Cache wrapper
- Cache invalidate## Usage
### LocalStorage - Low Level API
```typescript
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
```typescript
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)
}
```