Synchronous wasm cache implementation for js, compiled from Rust.
Warning
Do not use it in production! Only use it at your own risk.
pnpm i @duasfh/ts-rust-cacheOr
npm i @duasfh/ts-rust-cacheyarn add @duasfh/ts-rust-cacheIt has pretty standard cache api:
- set
- get
- delete
- clear
Additionally:
- getMemRaw
- getSizeRaw
Lifecycle:
- close
closeis typically only needed for cli applications or tests
See more at examples
Example of type usage:
type Cache = {
'a': number
'other-static-key': string
} & {
[K in `email-verification-${string}`]: string
} & {
[K in `other-dynamic-key-${number}-${string}`]: Date
}
export const cache = init<Cache>()And use that exported cache in your app files.
Type examples:
cache.set('a', '1', '15m') // -> Error: Argument of type 'string' is not assignable to parameter of type 'number'
cache.set('a', 1, '15m') // -> Correct
const a = cache.get('a')
// -> a: number | undefined
cache.set('email-verification', 1, '15m') // -> Error: Argument of type '"email-verification"' is not assignable to parameter of type '`email-verification-${string}` | ...
cache.set('email-verification-123', 1, '15m') // -> Error: Argument of type 'number' is not assignable to parameter of type 'string'
cache.set('email-verification-123', '123123', '15m') // -> Correct
const date = cache.get('other-dynamic-key-123-aaa')
// -> date: Date | undefinedSee more at examples
- When storing an object, nested dates (
{ date: new Date() }) will result in the returned date string ({ date: "0001-12-31T:01:01:01.000Z" })