typecache
v0.3.2
Published
Customizable caching library for TypeScript
Downloads
6
Readme
typecache
Customizable caching library for TypeScript
Install
npm i typecache
Usage
import LRU from 'lru-cache'
import { cache, LRUStore } from 'typecache'
const store = new LRUStore(new LRU<string, any>({ max: 10 }))
const { Store, Update, Remove } = cache(store)
class Service {
// Store the result in the cache and return that if it's called again
@Store((id: string) => `user-${id}`)
async getUser (id: string) {
const res = await fetch(`/user/${id}`)
return res.json()
}
// Replace the existing value in the cache
@Update((id: string) => `user-${id}`)
async updateUser (id: string) {
const res = await fetch(`/user/${id}/update`)
return res.json()
}
// Remove the keys from the cache
@Remove((id: string) => [`user-${id}`])
async deleteUser(id: string) {
const res = await fetch(`/user/${id}/delete`)
return res.json()
}
}